-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5_part2.py
54 lines (46 loc) · 1.31 KB
/
day5_part2.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
with open('test.txt') as f:
lines = [line.rstrip() for line in f]
all_stacks = {
'1': [],
'2': [],
'3': [],
'4': [],
'5': [],
'6': [],
'7': [],
'8': [],
'9': []
}
stack_input = []
for line in lines:
if line == '':
break
stack_input.append(line)
for crate_line in stack_input:
if '1' in crate_line:
break
n = 4
broken_crate_line = [crate_line[i:i + n] for i in range(0, len(crate_line), n)]
for crate_number in range(len(broken_crate_line)):
index = crate_number + 1
if not broken_crate_line[crate_number] == ' ':
all_stacks[str(index)].append(broken_crate_line[crate_number])
for crate_number in all_stacks:
all_stacks[crate_number].reverse()
move_commands = []
for line in lines:
if 'move' in line:
move_commands.append(line)
for line in move_commands:
amount_moved, move_from_to = line.split(' from ')
amount_moved = amount_moved.split(' ')[1]
move_from, move_to = move_from_to.split(' to ')
amount_moved = range(int(amount_moved))
for i in reversed(amount_moved):
pop_index = i + 1
crate = all_stacks[move_from].pop(-pop_index)
all_stacks[move_to].append(crate)
part_2 = ''
for stack_number in all_stacks:
part_2 += all_stacks[stack_number][-1]
print(part_2)