-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.py
65 lines (43 loc) · 1.33 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
# Advent of Code 2021, Day 25
# markusremplbauer
from itertools import count
import numpy as np
from aocd.models import Puzzle
from funcy import print_calls
# identifies '.', '>', 'v'
FREE, EAST, SOUTH = 0, 1, 2
@print_calls
def part1(grid):
for i in count(1):
n1 = move_sea_cuke(grid, EAST)
n2 = move_sea_cuke(grid, SOUTH)
# stop if no sea cuke moved
if n1 + n2 == 0:
return i
def move_sea_cuke(grid, sea_cuke):
lookahead = east if sea_cuke == EAST else south
moves = []
# check if the sea cuke can move
for xy, val in np.ndenumerate(grid):
if val == sea_cuke:
px, py = lookahead(*xy, grid.shape)
if grid[px, py] == FREE:
moves.append((*xy, px, py))
# perform the moves
for x, y, px, py in moves:
grid[x, y] = FREE
grid[px, py] = sea_cuke
return len(moves)
def east(x, y, shape):
return x, (y + 1) % shape[1]
def south(x, y, shape):
return (x + 1) % shape[0], y
def load(data):
data = data.replace(".", "0").replace(">", "1").replace("v", "2")
return np.fromiter(data.replace("\n", ""), dtype=int).reshape(-1, data.index("\n"))
def main():
puzzle = Puzzle(year=2021, day=25)
ans1 = part1(load(puzzle.input_data))
# puzzle.answer_a = ans1
if __name__ == "__main__":
main()