generated from public/aoc_template
Day 14 - make use of new lib features
This commit is contained in:
parent
78ca0f1e3f
commit
44e6b3e827
30
day14.py
30
day14.py
@ -1,18 +1,19 @@
|
|||||||
from tools.aoc import AOCDay
|
from tools.aoc import AOCDay
|
||||||
from tools.coordinate import Coordinate
|
|
||||||
from tools.grid import Grid
|
from tools.grid import Grid
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def load_after_roll(platform: Grid, direction: Coordinate) -> int:
|
SORT_KEYS = {
|
||||||
|
(0, -1): lambda c: c.y,
|
||||||
|
(-1, 0): None,
|
||||||
|
(0, 1): lambda c: -c.y,
|
||||||
|
(1, 0): lambda c: -c.x,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def load_after_roll(platform: Grid, direction: tuple[int, int]) -> int:
|
||||||
load = 0
|
load = 0
|
||||||
sort_order = {
|
for c in sorted(platform.getActiveCells(), key=SORT_KEYS[direction]):
|
||||||
Coordinate(0, -1): sorted(platform.getActiveCells(), key=lambda c: c.y),
|
|
||||||
Coordinate(-1, 0): sorted(platform.getActiveCells()),
|
|
||||||
Coordinate(0, 1): sorted(platform.getActiveCells(), key=lambda c: -c.y),
|
|
||||||
Coordinate(1, 0): sorted(platform.getActiveCells(), key=lambda c: -c.x),
|
|
||||||
}
|
|
||||||
for c in sort_order[direction]:
|
|
||||||
if platform.get(c) == "#":
|
if platform.get(c) == "#":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -43,13 +44,13 @@ class Day(AOCDay):
|
|||||||
|
|
||||||
def part1(self) -> Any:
|
def part1(self) -> Any:
|
||||||
platform = Grid.from_data(self.getInput(), translate={".": False})
|
platform = Grid.from_data(self.getInput(), translate={".": False})
|
||||||
return load_after_roll(platform, Coordinate(0, -1))
|
return load_after_roll(platform, (0, -1))
|
||||||
|
|
||||||
def part2(self) -> Any:
|
def part2(self) -> Any:
|
||||||
platform = Grid.from_data(self.getInput(), translate={".": False})
|
platform = Grid.from_data(self.getInput(), translate={".": False})
|
||||||
DP = {}
|
DP = {}
|
||||||
cycle_loads = {}
|
cycle_loads = {}
|
||||||
directions = [Coordinate(0, -1), Coordinate(-1, 0), Coordinate(0, 1), Coordinate(1, 0)]
|
directions = [(0, -1), (-1, 0), (0, 1), (1, 0)]
|
||||||
max_cycles = 1_000_000_000
|
max_cycles = 1_000_000_000
|
||||||
cycle = 0
|
cycle = 0
|
||||||
|
|
||||||
@ -60,13 +61,12 @@ class Day(AOCDay):
|
|||||||
load = load_after_roll(platform, d)
|
load = load_after_roll(platform, d)
|
||||||
cycle_loads[cycle] = load
|
cycle_loads[cycle] = load
|
||||||
|
|
||||||
fingerprint = tuple(sorted(platform.getActiveCells()))
|
if (load, platform) in DP:
|
||||||
if (load, fingerprint) in DP:
|
repeat_start = DP[(load, platform)]
|
||||||
repeat_start = DP[(load, fingerprint)]
|
|
||||||
lookup_cycle = (max_cycles - repeat_start) % (cycle - repeat_start) + repeat_start
|
lookup_cycle = (max_cycles - repeat_start) % (cycle - repeat_start) + repeat_start
|
||||||
return cycle_loads[lookup_cycle]
|
return cycle_loads[lookup_cycle]
|
||||||
else:
|
else:
|
||||||
DP[(load, fingerprint)] = cycle
|
DP[(load, platform)] = cycle
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user