-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc_25.py
59 lines (47 loc) · 1.38 KB
/
aoc_25.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
def load_region(filename):
with open(filename, 'r') as file:
return [[x for x in line.strip()] for line in file.readlines()]
def move_east(region):
moved = False
xmax = len(region[0])
ymax = len(region)
for y in range(ymax):
moves = []
for x in range(xmax):
if region[y][x] == '>':
x1 = (x + 1) % xmax
if region[y][x1] == '.':
moves.append((x, x1))
for (x0, x1) in moves:
region[y][x0] = '.'
region[y][x1] = '>'
moved = True
return moved
def move_south(region):
moved = False
xmax = len(region[0])
ymax = len(region)
for x in range(xmax):
moves = []
for y in range(ymax):
if region[y][x] == 'v':
y1 = (y + 1) % ymax
if region[y1][x] == '.':
moves.append((y, y1))
for (y0, y1) in moves:
region[y0][x] = '.'
region[y1][x] = 'v'
moved = True
return moved
def move_cucumbers(region):
moved = move_east(region)
moved |= move_south(region)
return moved
def find_first_step_with_no_moves(region):
steps = 0
while move_cucumbers(region):
steps += 1
return steps + 1
def part_a(filename):
region = load_region(filename)
return find_first_step_with_no_moves(region)