From 222eff07d2ad235078c3804cad22dccf07fae69c Mon Sep 17 00:00:00 2001 From: starnakin Date: Fri, 8 Dec 2023 10:21:20 +0100 Subject: [PATCH] add: part 2 --- 2023/day08/example3.txt | 10 ++++++++++ 2023/day08/src/part2.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 2023/day08/example3.txt create mode 100644 2023/day08/src/part2.py diff --git a/2023/day08/example3.txt b/2023/day08/example3.txt new file mode 100644 index 0000000..a8e2c98 --- /dev/null +++ b/2023/day08/example3.txt @@ -0,0 +1,10 @@ +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) \ No newline at end of file diff --git a/2023/day08/src/part2.py b/2023/day08/src/part2.py new file mode 100644 index 0000000..2a8c207 --- /dev/null +++ b/2023/day08/src/part2.py @@ -0,0 +1,36 @@ +import math + +text: str = open("input.txt", "r").read() + +_value: int = 0 + +lines = text.splitlines() + +deplacements = lines[0] +ground = {} + +starts = [] +for line in lines[2:]: + data = line.split(" = ") + if (data[0][2] == 'A'): + starts.append(data[0]) + data[1] = data[1][1:9] + ground.update({data[0]: data[1].split(", ")}) + +def resolve(ground: dict, deplacements: str, start: str): + nb_deplacement = 0 + pos = start + while True: + for deplacement in deplacements: + if (pos[2] == "Z"): + return (nb_deplacement) + nb_deplacement += 1 + pos = ground.get(pos)[deplacement == "R"] + +lst = [] +for start in starts: + lst.append(resolve(ground, deplacements, start)) + +_value = math.lcm(*lst) + +print(_value) \ No newline at end of file