From 951f6c51b8283a0368a1c13229f300d3429762e0 Mon Sep 17 00:00:00 2001 From: sloane Date: Fri, 6 Dec 2024 15:31:34 -0500 Subject: [PATCH] speed up 2024 day 6 pt 2 a bit limiting the candidates for new obstacle positions to positions the guard can reach cuts part 2 runtime from ~47s to ~10s --- 2024/lib/2024/6.ex | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/2024/lib/2024/6.ex b/2024/lib/2024/6.ex index 5b37556..4d05a4d 100644 --- a/2024/lib/2024/6.ex +++ b/2024/lib/2024/6.ex @@ -21,7 +21,12 @@ aoc 2024, 6 do in_front_of_guard = forward(state).pos - for pos <- Map.keys(state.grid), pos != in_front_of_guard, not blocked?(state, pos), reduce: 0 do + # by first simulating the regular board i can limit my search to just + # positions that are reachable. this cuts the runtime of pt 2 from about + # 47s to about 10s + %State{visited: visited} = simulate(state) + + for pos <- visited, pos != in_front_of_guard, not blocked?(state, pos), reduce: 0 do sum -> if (state |> insert_obstacle(pos)