Skip to content

Commit

Permalink
Add solution for tests that runs too slow
Browse files Browse the repository at this point in the history
  • Loading branch information
mjalkio committed Dec 27, 2023
1 parent 1715ac1 commit 280327e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion year_2023/day08/haunted_wasteland.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
END = "ZZZ"
START = "AAA"

END_CHAR = "Z"
START_CHAR = "A"


def get_num_steps(puzzle_input):
lines = puzzle_input.split("\n")
Expand All @@ -28,7 +31,22 @@ def get_num_steps(puzzle_input):


def get_num_steps_ghost(puzzle_input):
return 0
lines = puzzle_input.split("\n")
instructions = lines[0]
node_inputs = lines[2:]
nodes = {}
for ni in node_inputs:
node, connections = ni.split(" = ")
left, right = connections[1:-1].split(", ")
nodes[node] = {LEFT: left, RIGHT: right}

current_nodes = [n for n in nodes if n.endswith(START_CHAR)]
num_steps = 0
while any(not n.endswith(END_CHAR) for n in current_nodes):
next_instruction = instructions[num_steps % len(instructions)]
current_nodes = [nodes[n][next_instruction] for n in current_nodes]
num_steps += 1
return num_steps


if __name__ == "__main__":
Expand Down

0 comments on commit 280327e

Please sign in to comment.