2023.14
This commit is contained in:
parent
cffc191f4d
commit
f7eb29eb14
6 changed files with 124 additions and 1 deletions
1
challenges/2023/14-parabolicReflectorDish/README.md
Normal file
1
challenges/2023/14-parabolicReflectorDish/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 14: Parabolic Reflector Dish](https://adventofcode.com/2023/day/14)
|
105
challenges/2023/14-parabolicReflectorDish/main.py
Normal file
105
challenges/2023/14-parabolicReflectorDish/main.py
Normal file
|
@ -0,0 +1,105 @@
|
|||
import sys
|
||||
from enum import Enum, auto
|
||||
|
||||
|
||||
def parse(instr: str) -> tuple[str]:
|
||||
return tuple(instr.splitlines())
|
||||
|
||||
|
||||
class TiltDirection(Enum):
|
||||
North = auto()
|
||||
East = auto()
|
||||
South = auto()
|
||||
West = auto()
|
||||
|
||||
|
||||
def flip_platform(p: tuple[str]) -> tuple[str]:
|
||||
return tuple(map(lambda x: "".join(x), zip(*p)))
|
||||
|
||||
|
||||
def tilt_platform(platform: tuple[str], direction: TiltDirection) -> tuple[str]:
|
||||
if direction == TiltDirection.North or direction == TiltDirection.South:
|
||||
platform = flip_platform(platform)
|
||||
|
||||
if direction == TiltDirection.North or direction == TiltDirection.West:
|
||||
transformation = lambda x: x.replace(".O", "O.")
|
||||
else:
|
||||
transformation = lambda x: x.replace("O.", ".O")
|
||||
|
||||
res = []
|
||||
|
||||
for line in platform:
|
||||
prev = ""
|
||||
l = line
|
||||
while prev != l:
|
||||
prev = l
|
||||
l = transformation(l)
|
||||
|
||||
res.append(l)
|
||||
|
||||
if direction == TiltDirection.North or direction == TiltDirection.South:
|
||||
res = flip_platform(res)
|
||||
|
||||
return tuple(res)
|
||||
|
||||
|
||||
def calc_north_load(platform: tuple[str]) -> int:
|
||||
acc = 0
|
||||
for y, line in enumerate(platform):
|
||||
for x, char in enumerate(line):
|
||||
if char != "O":
|
||||
continue
|
||||
acc += len(platform) - y
|
||||
return acc
|
||||
|
||||
|
||||
def one(instr: str):
|
||||
platform = parse(instr)
|
||||
platform = tilt_platform(platform, TiltDirection.North)
|
||||
return calc_north_load(platform)
|
||||
|
||||
|
||||
def two(instr: str):
|
||||
platform = parse(instr)
|
||||
|
||||
ITERS = 1_000_000_000
|
||||
|
||||
i = 0
|
||||
known = {}
|
||||
jumped = False
|
||||
while i < ITERS:
|
||||
for direction in [
|
||||
TiltDirection.North,
|
||||
TiltDirection.West,
|
||||
TiltDirection.South,
|
||||
TiltDirection.East,
|
||||
]:
|
||||
platform = tilt_platform(platform, direction)
|
||||
|
||||
if not jumped:
|
||||
if platform in known:
|
||||
last_occurrence = known[platform]
|
||||
period = i - last_occurrence
|
||||
i += ((ITERS - i) // period) * period
|
||||
jumped = True
|
||||
else:
|
||||
known[platform] = i
|
||||
i += 1
|
||||
|
||||
return calc_north_load(platform)
|
||||
|
||||
|
||||
def _debug(*args, **kwargs):
|
||||
kwargs["file"] = sys.stderr
|
||||
print(*args, **kwargs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]:
|
||||
print("Missing day argument", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
inp = sys.stdin.read().strip()
|
||||
if sys.argv[1] == "1":
|
||||
print(one(inp))
|
||||
else:
|
||||
print(two(inp))
|
14
challenges/2023/14-parabolicReflectorDish/tests.json
Normal file
14
challenges/2023/14-parabolicReflectorDish/tests.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"1": [
|
||||
{
|
||||
"is": "136",
|
||||
"input": "O....#....\nO.OO#....#\n.....##...\nOO.#O....O\n.O.....O#.\nO.#..O.#.#\n..O..#O..O\n.......O..\n#....###..\n#OO..#....\n\n"
|
||||
}
|
||||
],
|
||||
"2": [
|
||||
{
|
||||
"is": "64",
|
||||
"input": "O....#....\nO.OO#....#\n.....##...\nOO.#O....O\n.O.....O#.\nO.#..O.#.#\n..O..#O..O\n.......O..\n#....###..\n#OO..#....\n\n"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -24,4 +24,5 @@ Solutions to the [2023 Advent of Code](https://adventofcode.com/2023).
|
|||
| 10 - Pipe Maze | ★ ★ | Python | Thoroughly barked up two wrong trees then Googled an algo based on a thought and oops it just works? |
|
||||
| 11 - Cosmic Expansion | ★ ★ | Python | Djikstra's and A* are the wrong way to do this (I tried both before engaging my brain) and then had to optimise various things for part 2 but nothing was horrendous. |
|
||||
| 12 - Hot Springs | ★ ★ | Python | Some hints from the subreddit were needed but they got me well on my way to completing this. Memoisation is king, long live memoisation. |
|
||||
| 13 - Point of Incidence | ★ ★ | Python | This one made me feel slightly intelligent. |
|
||||
| 13 - Point of Incidence | ★ ★ | Python | This one made me feel slightly intelligent. |
|
||||
| 14 - Parabolic Reflector Dish | ★ ★ | Python | Why do I always overcomplicate cycle detection?! |
|
Binary file not shown.
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 45 KiB |
|
@ -23,3 +23,5 @@
|
|||
{"day": 12, "part": 2, "runner": "py", "min": 0.904543399810791, "max": 2.621077299118042, "avg": 1.316542136669159, "n": 100}
|
||||
{"day": 13, "part": 1, "runner": "py", "min": 0.04506516456604004, "max": 0.1567397117614746, "avg": 0.059773361682891844, "n": 100}
|
||||
{"day": 13, "part": 2, "runner": "py", "min": 0.06211209297180176, "max": 0.12923550605773926, "avg": 0.07918766498565674, "n": 100}
|
||||
{"day": 14, "part": 1, "runner": "py", "min": 0.01713418960571289, "max": 0.02086782455444336, "avg": 0.01824690341949463, "n": 50}
|
||||
{"day": 14, "part": 2, "runner": "py", "min": 0.48926782608032227, "max": 0.6927425861358643, "avg": 0.5653452634811401, "n": 50}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue