Skip to content

Commit

Permalink
Day 14: Parabolic Reflector Dish
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 14, 2023
1 parent a75d4cf commit 1e644e1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Development occurs in language-specific directories:
|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day11.kt)|[day11.py](py/aoc2023/day11.py)|[day11.rs](rs/src/day11.rs)|
|[Day12.hs](hs/src/Day12.hs)|[Day12.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day12.kt)|[day12.py](py/aoc2023/day12.py)|[day12.rs](rs/src/day12.rs)|
|[Day13.hs](hs/src/Day13.hs)|[Day13.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day13.kt)|[day13.py](py/aoc2023/day13.py)|[day13.rs](rs/src/day13.rs)|
|[Day14.hs](hs/src/Day14.hs)|[Day14.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day14.kt)|||
|[Day14.hs](hs/src/Day14.hs)|[Day14.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day14.kt)|[day14.py](py/aoc2023/day14.py)||
76 changes: 76 additions & 0 deletions py/aoc2023/day14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Day 14: Parabolic Reflector Dish
"""

from itertools import islice

SAMPLE_INPUT = """
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
"""


def _tilt(data):
data = list(map(list, data))
for x in range(max(map(len, data))):
y0 = 0
while y0 < len(data):
n, y1 = 0, y0
while y1 < len(data):
c = data[y1][x]
if c == "O":
n += 1
elif c == "#":
break
y1 += 1
for y in range(y0, y1):
data[y][x] = "O" if y < y0 + n else "."
y0 = y1 + 1
return data


def _spin(data):
for _ in range(3):
data = list(zip(*_tilt(data)[::-1]))
return tuple(map("".join, zip(*_tilt(data)[::-1])))


def _load(data):
return sum((len(line) - i) * line.count("O") for i, line in enumerate(data))


def part1(data):
"""
>>> part1(SAMPLE_INPUT)
136
"""
return _load(_tilt(filter(None, data.splitlines())))


def part2(data):
"""
>>> part2(SAMPLE_INPUT)
64
"""
n = 1000000000
data = tuple(filter(None, data.splitlines()))
cache = {data: 0}
for i in range(1, n + 1):
data = _spin(data)
if data in cache:
j = cache[data]
data = next(islice(cache.keys(), j + (n - i) % (i - j), None))
break
cache[data] = i
return _load(data)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ day10 = "aoc2023.day10:parts"
day11 = "aoc2023.day11:parts"
day12 = "aoc2023.day12:parts"
day13 = "aoc2023.day13:parts"
day14 = "aoc2023.day14:parts"

[tool.black]
target_version = ["py312"]
Expand Down

0 comments on commit 1e644e1

Please sign in to comment.