-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.py
executable file
·39 lines (35 loc) · 1.01 KB
/
day5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
indatafile = open('day5_input')
indata = indatafile.readlines()
indatafile.close()
numstacks = 0
stacks = {}
for line in indata:
allnums = True
for x in line.strip().split():
try:
int(x)
except ValueError:
allnums = False
if allnums:
numstacks = len(line.strip().split())
break
for i in range(0,numstacks):
stacks[i] = []
for line in indata:
if '[' in line:
for step in range(0,numstacks*4,4):
stacknum = step // 4
if line[step] == '[':
stacks[stacknum].append(line[step+1])
if line.startswith('move'):
num = int(line.split()[1])
fromstack = int(line.split()[3])-1
tostack = int(line.split()[5])-1
for crate in range(0,num):
stacks[tostack] = stacks[fromstack][:1]+stacks[tostack]
stacks[fromstack] = stacks[fromstack][1:]
res = []
for stack in stacks.keys():
res.append(stacks[stack][0])
print(''.join(res))