-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.py
69 lines (54 loc) · 1.86 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
from aocd import submit, lines
from helper_functions import *
def parse(input):
return [list(line) for line in input]
def east_move(line):
points_to_swap = []
for idx, char in enumerate(line):
if char == '.' and line[idx - 1] == '>':
points_to_swap += [(idx, idx - 1)]
for swaps in points_to_swap:
line[swaps[0]], line[swaps[1]] = line[swaps[1]], line[swaps[0]]
return bool(len(points_to_swap))
def south_move(line, prev_line):
points_to_swap = []
for idx, char in enumerate(line):
if char == '.' and prev_line[idx] == 'v':
points_to_swap += [idx]
return points_to_swap, bool(len(points_to_swap))
def solve(input, should_submit=False):
grid = parse(input)
step = 1
while True:
has_moved = False
# Do eastbound moves first
for idx, line in enumerate(grid):
has_moved_east = east_move(line)
has_moved = has_moved or has_moved_east
# Do southbound moves second
points_to_swap = []
for idx, line in enumerate(grid):
new_swaps, has_moved_south = south_move(line, grid[idx - 1])
points_to_swap.extend([((idx, swap_idx), (idx - 1, swap_idx)) for swap_idx in new_swaps])
has_moved = has_moved or has_moved_south
for swaps in points_to_swap:
line_1, char_1 = swaps[0]
line_2, char_2 = swaps[1]
grid[line_1][char_1], grid[line_2][char_2] = grid[line_2][char_2], grid[line_1][char_1]
if not has_moved:
print(f'The sea cucumbers have stopped moving after {step} steps.')
if should_submit:
submit(step, part='a')
break
step += 1
example1 = '''v...>>.vv>
.vv>>.vv..
>>.>v>...v
>>v>>.>.v.
v>v.vv.v..
>.>>..v...
.vv..>.>v.
v.v..>>v.v
....v..v.>'''
# solve(example1.splitlines())
solve(lines, True)