From 573b17af92045ed8c768defa2fb25f78cb26e722 Mon Sep 17 00:00:00 2001 From: pareronia <49491686+pareronia@users.noreply.github.com> Date: Fri, 8 Dec 2023 07:23:30 +0100 Subject: [PATCH] AoC 2023 Day 8 Part 2 --- README.md | 2 +- src/main/python/AoC2023_08.py | 39 ++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a28d0014..adcdcb2e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ | | 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++ | | | | | | | | | | | | | | | | | | | | | | | | | | diff --git a/src/main/python/AoC2023_08.py b/src/main/python/AoC2023_08.py index 1f77fd62..f25decda 100644 --- a/src/main/python/AoC2023_08.py +++ b/src/main/python/AoC2023_08.py @@ -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 @@ -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): @@ -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: