diff --git a/2023/d8/python/solution.py b/2023/d8/python/solution.py new file mode 100644 index 0000000..f99157c --- /dev/null +++ b/2023/d8/python/solution.py @@ -0,0 +1,28 @@ +with open("../input", "r") as t: + data = t.read() + lines = data.split("\n") + instructions = lines[0] + elements = {} + e = lines[2:] + e = e[:-1] + for v in e: + key = v.split(" = (") + k = key[1].split(", ") + elements[key[0]] = {"L": k[0].strip(), "R": k[1][:-1]} + + answer = 1 + cur_el = elements["AAA"] + instructions = instructions * 30000 + print(instructions) + for c in instructions: + if cur_el[c] == "ZZZ": + break + else: + next_el = elements[cur_el[c]] + answer += 1 + cur_el = next_el + print(answer) + + + + diff --git a/2023/d8/test b/2023/d8/test new file mode 100644 index 0000000..9029a1b --- /dev/null +++ b/2023/d8/test @@ -0,0 +1,9 @@ +RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ) diff --git a/2023/d8/test2 b/2023/d8/test2 new file mode 100644 index 0000000..7d1b58d --- /dev/null +++ b/2023/d8/test2 @@ -0,0 +1,5 @@ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ)