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.coordinate import Coordinate
|
||||
from tools.grid import Grid
|
||||
from typing import Any
|
||||
|
||||
|
||||
def load_after_roll(platform: Grid, direction: Coordinate) -> int:
|
||||
load = 0
|
||||
sort_order = {
|
||||
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),
|
||||
SORT_KEYS = {
|
||||
(0, -1): lambda c: c.y,
|
||||
(-1, 0): None,
|
||||
(0, 1): lambda c: -c.y,
|
||||
(1, 0): lambda c: -c.x,
|
||||
}
|
||||
for c in sort_order[direction]:
|
||||
|
||||
|
||||
def load_after_roll(platform: Grid, direction: tuple[int, int]) -> int:
|
||||
load = 0
|
||||
for c in sorted(platform.getActiveCells(), key=SORT_KEYS[direction]):
|
||||
if platform.get(c) == "#":
|
||||
continue
|
||||
|
||||
@ -43,13 +44,13 @@ class Day(AOCDay):
|
||||
|
||||
def part1(self) -> Any:
|
||||
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:
|
||||
platform = Grid.from_data(self.getInput(), translate={".": False})
|
||||
DP = {}
|
||||
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
|
||||
cycle = 0
|
||||
|
||||
@ -60,13 +61,12 @@ class Day(AOCDay):
|
||||
load = load_after_roll(platform, d)
|
||||
cycle_loads[cycle] = load
|
||||
|
||||
fingerprint = tuple(sorted(platform.getActiveCells()))
|
||||
if (load, fingerprint) in DP:
|
||||
repeat_start = DP[(load, fingerprint)]
|
||||
if (load, platform) in DP:
|
||||
repeat_start = DP[(load, platform)]
|
||||
lookup_cycle = (max_cycles - repeat_start) % (cycle - repeat_start) + repeat_start
|
||||
return cycle_loads[lookup_cycle]
|
||||
else:
|
||||
DP[(load, fingerprint)] = cycle
|
||||
DP[(load, platform)] = cycle
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user