Skip to content

Commit 648e16f

Browse files
committed
Small refactor to tidy up runner
Still horrible. I should really clone the state of the process. If I need to do this again I definitely will.
1 parent 4de92d8 commit 648e16f

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

2019/15/lib/aoc/runner.ex

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,11 @@ defmodule AOC.Runner do
4545
#ignore this entire path
4646
send(repair_droid, {:reset, self(), structured_data()})
4747
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])
48+
{:input, value, ^repair_droid} ->
49+
board = Map.put(board, current_position, value)
50+
if value == @oxygen do
51+
IO.puts("oxygen found at #{current_path |> length |> Kernel.+(1)}")
5952
end
60-
{:input, @oxygen, ^repair_droid} ->
61-
board = Map.put_new(board, current_position, @oxygen)
62-
IO.puts(current_path |> length |> Kernel.+(1))
6353
cond do
6454
rest == [] ->
6555
# means we have reached the end of this track, add routes onto tail
@@ -115,7 +105,6 @@ defmodule AOC.Runner do
115105
if Map.values(maze) |> Enum.uniq |> length == 1 do
116106
steps - 1
117107
else
118-
Board.print(maze)
119108
maze
120109
|> Enum.filter(fn({point, v}) -> v == 2 end)
121110
|> Enum.reduce(maze, &fill_oxygen/2)
@@ -124,16 +113,19 @@ defmodule AOC.Runner do
124113
end
125114

126115
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))
116+
maze
117+
|> Map.put({x, y}, @wall)
118+
|> expand_oxygen({x - 1, y})
119+
|> expand_oxygen({x + 1, y})
120+
|> expand_oxygen({x, y - 1})
121+
|> expand_oxygen({x, y + 1})
132122
end
133123

134124
def fill_maze(maze, point, @wall), do: maze
135125
def fill_maze(maze, point, @oxygen), do: maze
136-
def fill_maze(maze, point, @empty_space) do
137-
Map.put(maze, point, @oxygen)
126+
def fill_maze(maze, point, @empty_space), do: Map.put(maze, point, @oxygen)
127+
128+
defp expand_oxygen(maze, point) do
129+
fill_maze(maze, point, Map.get(maze, point, @wall))
138130
end
139131
end

2019/15/run.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# IO.puts "part1 : #{ AOC.Runner.part_1 }"
1+
AOC.Runner.part_1
22
IO.puts "part2: #{ AOC.Runner.part_2 }"

0 commit comments

Comments
 (0)