From b3bddd521ffc961d9df6a34bcf313b1de8b6f9db Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Fri, 8 Dec 2023 07:10:34 +0100 Subject: [PATCH] Day 8 - simpler and (much) faster --- day08.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/day08.py b/day08.py index 6036651..1613592 100644 --- a/day08.py +++ b/day08.py @@ -46,20 +46,20 @@ class Day(AOCDay): inst, nodes = self.parse_input() i_len = len(inst) check_nodes = [x for x in nodes if x.endswith("A")] - nodes_dp = defaultdict(dict) + z_steps = [] for node in check_nodes: cur_step = 0 cur_node = node - while (cur_node, cur_step % i_len) not in nodes_dp[node]: - nodes_dp[node][(cur_node, cur_step % i_len)] = cur_step + while not cur_node.endswith("Z"): if inst[cur_step % i_len] == "L": cur_node = nodes[cur_node][0] else: cur_node = nodes[cur_node][1] cur_step += 1 + z_steps.append(cur_step) - return math.lcm(*[v for node in check_nodes for k, v in nodes_dp[node].items() if k[0].endswith("Z")]) + return math.lcm(*z_steps) if __name__ == "__main__":