Skip to content

Commit

Permalink
Merge pull request #100 from wimglenn/2023/14
Browse files Browse the repository at this point in the history
day 14
  • Loading branch information
wimglenn authored Dec 14, 2023
2 parents ce74c0d + 2a03428 commit e41ed53
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
52 changes: 52 additions & 0 deletions aoc_wim/aoc2023/q14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
--- Day 14: Parabolic Reflector Dish ---
https://adventofcode.com/2023/day/14
"""
from aocd import data


def tilt_north(data):
rows = data.splitlines()
cols = [''.join(col) for col in list(zip(*rows))]
newcols = []
for col in cols:
segments = col.split("#")
segments = [''.join(sorted(s, reverse=True)) for s in segments]
newcol = "#".join(segments)
newcols.append(newcol)
newrows = [''.join(row) for row in list(zip(*newcols))]
newdata = "\n".join(newrows)
return newdata


def rotate_right(data):
rows = data.splitlines()
cols = [''.join(col) for col in list(zip(*rows))]
return "\n".join([col[::-1] for col in cols])


def one_cycle(data):
for _ in "NWSE":
data = tilt_north(data)
data = rotate_right(data)
return data


def load_on_north_support_beams(rows):
return sum(i*row.count("O") for i, row in enumerate(rows.splitlines()[::-1], 1))


print("answer_a:", load_on_north_support_beams(tilt_north(data)))
cycles = 1_000_000_000
seen = {}
period = None
i = 0
while i < cycles:
data = one_cycle(data)
i += 1
# print(f"After {i+1} cycle:\n{data}\n\n")
if period is None and data in seen:
period = i - seen[data]
i += period * ((cycles - i) // period)
seen[data] = i
print("answer_b:", load_on_north_support_beams(data))
12 changes: 12 additions & 0 deletions tests/2023/14/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
136
64

0 comments on commit e41ed53

Please sign in to comment.