Day 8 - simpler and (much) faster

This commit is contained in:
Stefan Harmuth 2023-12-08 07:10:34 +01:00
parent fdfa8a02b2
commit b3bddd521f

View File

@ -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__":