-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday25.py
70 lines (52 loc) · 1.46 KB
/
day25.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from collections import defaultdict
initial_state = '''v...>>.vv>
.vv>>.vv..
>>.>v>...v
>>v>>.>.v.
v>v.vv.v..
>.>>..v...
.vv..>.>v.
v.v..>>v.v
....v..v.>'''
def read_input():
area = {}
x, y = 0, 0
lines = [line.strip() for line in open('2021/input/day25.txt').readlines()]
for y, line in enumerate(lines):
for x, char in enumerate(line):
area[(x, y)] = char
return area, x + 1, y + 1
def process_step(area, width, height):
have_moved = False
# move east-cucumbers
moves = []
for pos, char in area.items():
new_pos = ((pos[0] + 1) % width, pos[1])
if char == '>' and area[new_pos] == '.':
moves.append((pos, new_pos))
for pos, new_pos in moves:
have_moved = True
area[new_pos] = '>'
area[pos] = '.'
# move down-cucumbers
moves = []
for pos, char in area.items():
new_pos = (pos[0], (pos[1] + 1) % height)
if char == 'v' and area[new_pos] == '.':
moves.append((pos, new_pos))
for pos, new_pos in moves:
have_moved = True
area[new_pos] = 'v'
area[pos] = '.'
return have_moved
def print_area(area, width, height):
for y in range(height):
for x in range(width):
print(area[(x, y)], end='')
print()
area, width, height = read_input()
# print_area(area, width, height)
num_steps = 1
while process_step(area, width, height):
num_steps += 1
print("Part 1:", num_steps)