-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import fileinput | ||
|
||
type Point = tuple[int, int] | ||
|
||
round_rocks: dict[Point, str] = {} | ||
cube_rocks: dict[Point, str] = {} | ||
height = 0 | ||
|
||
for r, line in enumerate(fileinput.input()): | ||
for c, val in enumerate(line.strip()): | ||
if val == ".": | ||
continue | ||
elif val == "O": | ||
round_rocks[(r, c)] = val | ||
elif val == "#": | ||
cube_rocks[(r, c)] = val | ||
else: | ||
assert False | ||
height = r + 1 | ||
del r, line, c, val | ||
|
||
for rock in round_rocks: | ||
new_r = rock[0] | ||
for r in range(rock[0] - 1, -1, -1): | ||
if (r, rock[1]) not in cube_rocks: | ||
new_r = r | ||
else: | ||
break | ||
|
||
cube_rocks[(new_r, rock[1])] = "O" | ||
|
||
print(sum([height - rock[0] for rock in cube_rocks if cube_rocks[rock] == "O"])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import fileinput | ||
import pprint | ||
import sys | ||
from collections import OrderedDict | ||
from functools import cache, lru_cache | ||
from typing import TypeAlias | ||
|
||
Point: TypeAlias = tuple[int, int] | ||
|
||
|
||
class Platform: | ||
def __init__(self, round_rocks: set[Point], cube_rocks: set[Point], height: int, width: int): | ||
self.round_rocks = round_rocks | ||
self.cube_rocks = cube_rocks | ||
self.height = height | ||
self.width = width | ||
|
||
def calc_load(self) -> int: | ||
total_load = 0 | ||
for rock in self.round_rocks: | ||
load = self.height - rock[0] | ||
total_load += load | ||
return total_load | ||
|
||
@cache | ||
def north(self): | ||
new_round_rocks: set[Point] = set() | ||
|
||
for rock in sorted(self.round_rocks): | ||
new_r = rock[0] | ||
for r in range(rock[0] - 1, -1, -1): | ||
if (r, rock[1]) not in self.cube_rocks and (r, rock[1]) not in new_round_rocks: | ||
new_r = r | ||
else: | ||
break | ||
|
||
new_round_rocks.add((new_r, rock[1])) | ||
|
||
return new_round_rocks | ||
|
||
@cache | ||
def south(self): | ||
new_round_rocks: set[Point] = set() | ||
|
||
for rock in sorted(self.round_rocks, reverse=True): | ||
new_r = rock[0] | ||
for r in range(rock[0] + 1, self.height): | ||
if (r, rock[1]) not in self.cube_rocks and (r, rock[1]) not in new_round_rocks: | ||
new_r = r | ||
else: | ||
break | ||
|
||
new_round_rocks.add((new_r, rock[1])) | ||
|
||
return new_round_rocks | ||
|
||
@cache | ||
def west(self): | ||
new_round_rocks: set[Point] = set() | ||
|
||
for rock in sorted(self.round_rocks, key=lambda x: x[1]): | ||
new_c = rock[1] | ||
for c in range(rock[1] - 1, -1, -1): | ||
if (rock[0], c) not in self.cube_rocks and (rock[0], c) not in new_round_rocks: | ||
new_c = c | ||
else: | ||
break | ||
|
||
new_round_rocks.add((rock[0], new_c)) | ||
|
||
return new_round_rocks | ||
|
||
@cache | ||
def east(self): | ||
new_round_rocks: set[Point] = set() | ||
|
||
for rock in sorted(self.round_rocks, key=lambda x: x[1], reverse=True): | ||
new_c = rock[1] | ||
for c in range(rock[1] + 1, self.width): | ||
if (rock[0], c) not in self.cube_rocks and (rock[0], c) not in new_round_rocks: | ||
new_c = c | ||
else: | ||
break | ||
|
||
new_round_rocks.add((rock[0], new_c)) | ||
|
||
return new_round_rocks | ||
|
||
def cycle(self): | ||
self.round_rocks = self.north() | ||
self.round_rocks = self.west() | ||
self.round_rocks = self.south() | ||
self.round_rocks = self.east() | ||
|
||
def __eq__(self, other): | ||
return self.round_rocks == other.round_rocks | ||
|
||
def __hash__(self): | ||
return hash(frozenset(sorted(self.round_rocks))) | ||
|
||
def toGrid(self): | ||
grid = [] | ||
for r in range(self.height): | ||
grid.append([]) | ||
for c in range(self.width): | ||
p = (r, c) | ||
grid[r].append("#" if p in self.cube_rocks else ("O" if p in self.round_rocks else ".")) | ||
return grid | ||
|
||
def printGrid(self): | ||
grid = self.toGrid() | ||
for r in grid: | ||
print(" ".join(r)) | ||
print("") | ||
|
||
|
||
def main(): | ||
round_rocks: set[Point] = set() | ||
cube_rocks: set[Point] = set() | ||
height = 0 | ||
width = 0 | ||
for r, line in enumerate(fileinput.input()): | ||
line = line.strip() | ||
for c, val in enumerate(line): | ||
if val == ".": | ||
continue | ||
elif val == "O": | ||
round_rocks.add((r, c)) | ||
elif val == "#": | ||
cube_rocks.add((r, c)) | ||
else: | ||
assert False | ||
height = r + 1 | ||
width = len(line) | ||
del r, line, c, val | ||
plat = Platform(round_rocks, cube_rocks, height, width) | ||
del round_rocks, cube_rocks, height | ||
|
||
CYCLES = 1_000_000_000 | ||
hashes: OrderedDict[int, int] = OrderedDict() | ||
|
||
offset = None | ||
interval = None | ||
for i in range(CYCLES): | ||
plat.cycle() | ||
h = hash(plat) | ||
# print(h) | ||
if h in hashes: | ||
if not offset: | ||
offset = list(hashes).index(h) | ||
interval = i - offset | ||
|
||
# print(hashes.values()) | ||
# print(plat.calc_load()) | ||
m = (CYCLES - 1 - offset) % (interval) | ||
print(list(hashes.values())[m + offset]) | ||
sys.exit() | ||
else: | ||
hashes[h] = plat.calc_load() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Day 14: Parabolic Reflector Dish | ||
|
||
[https://adventofcode.com/2023/day/14](https://adventofcode.com/2023/day/14) | ||
|
||
## Description | ||
|
||
### Part One | ||
|
||
You reach the place where all of the mirrors were pointing: a massive [parabolic reflector dish](https://en.wikipedia.org/wiki/Parabolic_reflector) <span title="Why, where do you attach YOUR massive parabolic reflector dishes?">attached</span> to the side of another large mountain. | ||
|
||
The dish is made up of many small mirrors, but while the mirrors themselves are roughly in the shape of a parabolic reflector dish, each individual mirror seems to be pointing in slightly the wrong direction. If the dish is meant to focus light, all it's doing right now is sending it in a vague direction. | ||
|
||
This system must be what provides the energy for the lava! If you focus the reflector dish, maybe you can go where it's pointing and use the light to fix the lava production. | ||
|
||
Upon closer inspection, the individual mirrors each appear to be connected via an elaborate system of ropes and pulleys to a large metal platform below the dish. The platform is covered in large rocks of various shapes. Depending on their position, the weight of the rocks deforms the platform, and the shape of the platform controls which ropes move and ultimately the focus of the dish. | ||
|
||
In short: if you move the rocks, you can focus the dish. The platform even has a control panel on the side that lets you _tilt_ it in one of four directions! The rounded rocks (`O`) will roll when the platform is tilted, while the cube-shaped rocks (`#`) will stay in place. You note the positions of all of the empty spaces (`.`) and rocks (your puzzle input). For example: | ||
|
||
O....#.... | ||
O.OO#....# | ||
.....##... | ||
OO.#O....O | ||
.O.....O#. | ||
O.#..O.#.# | ||
..O..#O..O | ||
.......O.. | ||
#....###.. | ||
#OO..#.... | ||
|
||
|
||
Start by tilting the lever so all of the rocks will slide _north_ as far as they will go: | ||
|
||
OOOO.#.O.. | ||
OO..#....# | ||
OO..O##..O | ||
O..#.OO... | ||
........#. | ||
..#....#.# | ||
..O..#.O.O | ||
..O....... | ||
#....###.. | ||
#....#.... | ||
|
||
|
||
You notice that the support beams along the north side of the platform are _damaged_; to ensure the platform doesn't collapse, you should calculate the _total load_ on the north support beams. | ||
|
||
The amount of load caused by a single rounded rock (`O`) is equal to the number of rows from the rock to the south edge of the platform, including the row the rock is on. (Cube-shaped rocks (`#`) don't contribute to load.) So, the amount of load caused by each rock in each row is as follows: | ||
|
||
OOOO.#.O.. 10 | ||
OO..#....# 9 | ||
OO..O##..O 8 | ||
O..#.OO... 7 | ||
........#. 6 | ||
..#....#.# 5 | ||
..O..#.O.O 4 | ||
..O....... 3 | ||
#....###.. 2 | ||
#....#.... 1 | ||
|
||
|
||
The total load is the sum of the load caused by all of the _rounded rocks_. In this example, the total load is _`136`_. | ||
|
||
Tilt the platform so that the rounded rocks all roll north. Afterward, _what is the total load on the north support beams?_ | ||
|
||
### Part Two | ||
|
||
The parabolic reflector dish deforms, but not in a way that focuses the beam. To do that, you'll need to move the rocks to the edges of the platform. Fortunately, a button on the side of the control panel labeled "_spin cycle_" attempts to do just that! | ||
|
||
Each _cycle_ tilts the platform four times so that the rounded rocks roll _north_, then _west_, then _south_, then _east_. After each tilt, the rounded rocks roll as far as they can before the platform tilts in the next direction. After one cycle, the platform will have finished rolling the rounded rocks in those four directions in that order. | ||
|
||
Here's what happens in the example above after each of the first few cycles: | ||
|
||
After 1 cycle: | ||
.....#.... | ||
....#...O# | ||
...OO##... | ||
.OO#...... | ||
.....OOO#. | ||
.O#...O#.# | ||
....O#.... | ||
......OOOO | ||
#...O###.. | ||
#..OO#.... | ||
|
||
After 2 cycles: | ||
.....#.... | ||
....#...O# | ||
.....##... | ||
..O#...... | ||
.....OOO#. | ||
.O#...O#.# | ||
....O#...O | ||
.......OOO | ||
#..OO###.. | ||
#.OOO#...O | ||
|
||
After 3 cycles: | ||
.....#.... | ||
....#...O# | ||
.....##... | ||
..O#...... | ||
.....OOO#. | ||
.O#...O#.# | ||
....O#...O | ||
.......OOO | ||
#...O###.O | ||
#.OOO#...O | ||
|
||
|
||
This process should work if you leave it running long enough, but you're still worried about the north support beams. To make sure they'll survive for a while, you need to calculate the _total load_ on the north support beams after `1000000000` cycles. | ||
|
||
In the above example, after `1000000000` cycles, the total load on the north support beams is _`64`_. | ||
|
||
Run the spin cycle for `1000000000` cycles. Afterward, _what is the total load on the north support beams?_ |