-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.py
82 lines (55 loc) · 2.03 KB
/
day14.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
71
72
73
74
75
76
77
78
79
80
81
82
"""
Advent of Code 2023, Day 14: Parabolic Reflector Dish.
"""
import sys
from functools import reduce
from typing import TextIO
from aoc_2023.grid import Grid, NORTH, WEST, SOUTH, EAST
ROUND_ROCK = "O"
CUBE_ROCK = "#"
CYCLES = 1_000_000_000
def tilt(grid: Grid, direction: complex) -> Grid:
tilted = Grid({p: v for p, v in grid.items() if v == CUBE_ROCK})
for p in sorted(
(p for p, v in grid.items() if v == ROUND_ROCK),
key=lambda pos: pos.real if direction in [EAST, WEST] else pos.imag,
reverse=direction in [SOUTH, EAST],
):
while (
(next_pos := p + direction) not in tilted
and 0 <= next_pos.real < grid.width
and 0 <= next_pos.imag < grid.height
):
p = next_pos
tilted[p] = ROUND_ROCK
return tilted
def tilt_cycle(grid: Grid) -> Grid:
return reduce(tilt, [NORTH, WEST, SOUTH, EAST], grid)
def north_beam_load(grid: Grid) -> int:
return int(sum(grid.height - p.imag for p, v in grid.items() if v == ROUND_ROCK))
def part_one(file: TextIO) -> int:
platform = Grid.from_ascii_grid(file)
platform = tilt(platform, NORTH)
return north_beam_load(platform)
def part_two(file: TextIO) -> int:
platform = Grid.from_ascii_grid(file)
cache = {}
for step in range(CYCLES):
platform = tilt_cycle(platform)
key = frozenset(p for p, v in platform.items() if v == ROUND_ROCK)
if key in cache:
loop_length = step - cache[key]
steps_remaining = (CYCLES - step - 1) % loop_length
for _ in range(steps_remaining):
platform = tilt_cycle(platform)
return north_beam_load(platform)
cache[key] = step
return north_beam_load(platform)
def main():
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()