From a310f24d8e61f16252a1391be42d3b00310460f0 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 8 Dec 2023 08:23:16 +0000 Subject: [PATCH] 2023: d08: ex2: add solution --- 2023/d08/ex2/ex2.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 2023/d08/ex2/ex2.py diff --git a/2023/d08/ex2/ex2.py b/2023/d08/ex2/ex2.py new file mode 100755 index 0000000..8d9ec61 --- /dev/null +++ b/2023/d08/ex2/ex2.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +import itertools +import math +import sys + +Graph = dict[str, list[str]] + + +def solve(input: list[str]) -> int: + def parse_graph(input: list[str]) -> Graph: + res: Graph = {} + + for line in input: + start, dests = line.split(" = ") + res[start] = dests[1:-1].split(", ") + + return res + + def parse(input: list[str]) -> tuple[str, Graph]: + return input[0], parse_graph(input[2:]) + + def navigate(directions: str, graph: Graph, start: str) -> int: + pos = start + i = 0 + for dir in itertools.cycle(directions): + if pos[-1] == "Z": + break + pos = graph[pos][0 if dir == "L" else 1] + i += 1 + return i + + directions, graph = parse(input) + starts = set(node for node in graph if node[-1] == "A") + steps = {navigate(directions, graph, n) for n in starts} + return math.lcm(*steps) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()