Skip to content

Commit

Permalink
AoC 2023 Day 8 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 8, 2023
1 parent f932371 commit 573b17a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- @BEGIN:ImplementationsTable:2023@ -->
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | | | | | | | | | | | | | | | | | | |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | [](src/main/python/AoC2023_08.py) | | | | | | | | | | | | | | | | | |
| java | [](src/main/java/AoC2023_01.java) | [](src/main/java/AoC2023_02.java) | [](src/main/java/AoC2023_03.java) | [](src/main/java/AoC2023_04.java) | | [](src/main/java/AoC2023_06.java) | | | | | | | | | | | | | | | | | | | |
| bash | | | | | | | | | | | | | | | | | | | | | | | | | |
| c++ | | | | | | | | | | | | | | | | | | | | | | | | | |
Expand Down
39 changes: 29 additions & 10 deletions src/main/python/AoC2023_08.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
# Advent of Code 2023 Day 8
#

import itertools
import sys
from math import lcm
from typing import NamedTuple
import itertools

from aoc import my_aocd
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
# from aoc.common import log


TEST1 = """\
RL
Expand All @@ -32,6 +31,18 @@
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)
"""
TEST3 = """\
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)
"""


class Map(NamedTuple):
Expand All @@ -55,27 +66,35 @@ def parse_input(self, input_data: InputData) -> Map:
map = Map(instructions, network)
return map

def part_1(self, map: Map) -> Output1:
def _steps(self, map: Map, start_key: str) -> int:
inss = itertools.cycle(map.instructions)
node = map.network['AAA']
node = map.network[start_key]
ans = 1
while True:
ins = next(inss)
key = node[0 if ins == 'L' else 1]
if key == 'ZZZ':
key = node[0 if ins == "L" else 1]
if key[-1] == "Z":
break
node = map.network[key]
ans += 1
return ans

def part_2(self, input: Map) -> Output2:
return 0
def part_1(self, map: Map) -> Output1:
return self._steps(map, "AAA")

def part_2(self, map: Map) -> Output2:
keys = [k for k in map.network if k[-1] == "A"]
steps = [self._steps(map, key) for key in keys]
ans = steps[0]
for s in steps[1:]:
ans = lcm(ans, s)
return ans

@aoc_samples(
(
("part_1", TEST1, 2),
("part_1", TEST2, 6),
# ("part_2", TEST, "TODO"),
("part_2", TEST3, 6),
)
)
def samples(self) -> None:
Expand Down

0 comments on commit 573b17a

Please sign in to comment.