@@ -4,9 +4,9 @@ defmodule AOC.Runner do
44 @ west 3
55 @ east 4
66
7- @ hit_wall 0
8- @ moved 1
9- @ found_oxygen 2
7+ @ wall 0
8+ @ empty_space 1
9+ @ oxygen 2
1010
1111 alias AOC . { IntcodeAgent , Board }
1212
@@ -25,21 +25,41 @@ defmodule AOC.Runner do
2525 AOC.Parser . parse
2626 end
2727
28+ defp maze_data do
29+ AOC.Parser . parse_2
30+ end
31+
32+ defp wait_loop ( board , repair_droid , [ ] , { 0 , 0 } , [ ] ) do
33+ Board . print ( board )
34+ end
35+
2836 defp wait_loop ( board , repair_droid , path_remainder = [ path | t ] , current_position , current_path ) do
2937 [ move | rest ] = path
3038 send ( :repair_droid , { :input , move } )
3139 current_position = new_position ( current_position , move )
3240
33- Board . print ( board )
3441 receive do
3542 { :terminating , ^ repair_droid } ->
3643 board
37- { :input , @ hit_wall , ^ repair_droid } ->
44+ { :input , @ wall , ^ repair_droid } ->
3845 #ignore this entire path
3946 send ( repair_droid , { :reset , self ( ) , structured_data ( ) } )
40- wait_loop ( Map . put ( board , current_position , @ hit_wall ) , repair_droid , t , { 0 , 0 } , [ ] )
41- { :input , @ moved , ^ repair_droid } ->
42- board = Map . put ( board , current_position , @ moved )
47+ wait_loop ( Map . put ( board , current_position , @ wall ) , repair_droid , t , { 0 , 0 } , [ ] )
48+ { :input , @ empty_space , ^ repair_droid } ->
49+ board = Map . put ( board , current_position , @ empty_space )
50+ cond do
51+ rest == [ ] ->
52+ # means we have reached the end of this track, add routes onto tail
53+ # and continue _from start_
54+ new_paths = add_paths ( t , move , current_path ++ [ move ] )
55+ send ( repair_droid , { :reset , self ( ) , structured_data ( ) } )
56+ wait_loop ( board , repair_droid , new_paths , { 0 , 0 } , [ ] )
57+ true ->
58+ wait_loop ( board , repair_droid , [ rest ] ++ t , current_position , current_path ++ [ move ] )
59+ end
60+ { :input , @ oxygen , ^ repair_droid } ->
61+ board = Map . put_new ( board , current_position , @ oxygen )
62+ IO . puts ( current_path |> length |> Kernel . + ( 1 ) )
4363 cond do
4464 rest == [ ] ->
4565 # means we have reached the end of this track, add routes onto tail
@@ -50,8 +70,6 @@ defmodule AOC.Runner do
5070 true ->
5171 wait_loop ( board , repair_droid , [ rest ] ++ t , current_position , current_path ++ [ move ] )
5272 end
53- { :input , @ found_oxygen , ^ repair_droid } ->
54- current_path |> length |> Kernel . + ( 1 )
5573 end
5674 end
5775
@@ -84,4 +102,38 @@ defmodule AOC.Runner do
84102 def new_position ( { x , y } , @ south ) , do: { x , y + 1 }
85103 def new_position ( { x , y } , @ east ) , do: { x + 1 , y }
86104 def new_position ( { x , y } , @ west ) , do: { x - 1 , y }
105+
106+
107+ # part 2 functions
108+ def part_2 ( maze \\ maze_data ( ) ) do
109+ Board . print ( maze )
110+
111+ fill_maze ( maze , 0 )
112+ end
113+
114+ defp fill_maze ( maze , steps ) do
115+ if Map . values ( maze ) |> Enum . uniq |> length == 1 do
116+ steps - 1
117+ else
118+ Board . print ( maze )
119+ maze
120+ |> Enum . filter ( fn ( { point , v } ) -> v == 2 end )
121+ |> Enum . reduce ( maze , & fill_oxygen / 2 )
122+ |> fill_maze ( steps + 1 )
123+ end
124+ end
125+
126+ defp fill_oxygen ( { { x , y } , _oxygen } , maze ) do
127+ maze = Map . put ( maze , { x , y } , @ wall )
128+ maze = fill_maze ( maze , { x - 1 , y } , Map . get ( maze , { x - 1 , y } , @ wall ) )
129+ maze = fill_maze ( maze , { x + 1 , y } , Map . get ( maze , { x + 1 , y } , @ wall ) )
130+ maze = fill_maze ( maze , { x , y - 1 } , Map . get ( maze , { x , y - 1 } , @ wall ) )
131+ fill_maze ( maze , { x , y + 1 } , Map . get ( maze , { x , y + 1 } , @ wall ) )
132+ end
133+
134+ def fill_maze ( maze , point , @ wall ) , do: maze
135+ def fill_maze ( maze , point , @ oxygen ) , do: maze
136+ def fill_maze ( maze , point , @ empty_space ) do
137+ Map . put ( maze , point , @ oxygen )
138+ end
87139end
0 commit comments