You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defmodulePathfinderdodefpsearch(steps,_edges,:end,_),do: [{Enum.reverse([:end|steps])}]defpsearch(steps,edges,current_cave,single_visit_twice)dopossible_paths=edges[current_cave]next_steps=[current_cave|steps]Enum.flat_map(possible_paths,fnpath->conddopath==:start->[nil]# we do not allow a small cave to be visited twicenotsingle_visit_twiceandmatch?({:small,_p},path)andpathinnext_steps->[nil]# we allow a small cave to be visited twice, but only# a specific onesingle_visit_twiceandmatch?({:small,_p},path)andpathinnext_stepsandEnum.frequencies(next_steps)|>Enum.any?(fn{cave,visitcount}->match?({:small,_},cave)andvisitcount>=2end)->[nil]true->search(next_steps,edges,path,single_visit_twice)endend)enddefsearch(edges,single_visit_twice\\false)dosearch([],edges,:start,single_visit_twice)|>Enum.reject(&is_nil/1)|>Enum.uniq()endend