1
0
Fork 0

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
This commit is contained in:
sloane 2024-12-06 15:31:34 -05:00
parent db20c89051
commit 951f6c51b8
Signed by: sloanelybutsurely
SSH key fingerprint: SHA256:8SBnwhl+RY3oEyQxy1a9wByPzxWM0x+/Ejc+sIlY5qQ

View file

@ -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)