generated from public/aoc_template
Day 8 - simpler and (much) faster
This commit is contained in:
parent
fdfa8a02b2
commit
b3bddd521f
8
day08.py
8
day08.py
@ -46,20 +46,20 @@ class Day(AOCDay):
|
|||||||
inst, nodes = self.parse_input()
|
inst, nodes = self.parse_input()
|
||||||
i_len = len(inst)
|
i_len = len(inst)
|
||||||
check_nodes = [x for x in nodes if x.endswith("A")]
|
check_nodes = [x for x in nodes if x.endswith("A")]
|
||||||
nodes_dp = defaultdict(dict)
|
z_steps = []
|
||||||
for node in check_nodes:
|
for node in check_nodes:
|
||||||
cur_step = 0
|
cur_step = 0
|
||||||
cur_node = node
|
cur_node = node
|
||||||
while (cur_node, cur_step % i_len) not in nodes_dp[node]:
|
while not cur_node.endswith("Z"):
|
||||||
nodes_dp[node][(cur_node, cur_step % i_len)] = cur_step
|
|
||||||
if inst[cur_step % i_len] == "L":
|
if inst[cur_step % i_len] == "L":
|
||||||
cur_node = nodes[cur_node][0]
|
cur_node = nodes[cur_node][0]
|
||||||
else:
|
else:
|
||||||
cur_node = nodes[cur_node][1]
|
cur_node = nodes[cur_node][1]
|
||||||
|
|
||||||
cur_step += 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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user