Skip to content

Commit 4de92d8

Browse files
committed
Part 2 complete, simple fill
1 parent 58c19b4 commit 4de92d8

File tree

7 files changed

+144
-80
lines changed

7 files changed

+144
-80
lines changed

2019/15/input2.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#########################################
2+
#.....#.............#.#.....#.........#O#
3+
###.#.###.#########.#.#.#.#.#####.###.#.#
4+
#...#.....#...#...#.#...#.#.....#...#...#
5+
#.#########.#.#.#.#.#####.#####.###.#####
6+
#...#.......#.#.#.#.....#...#.#.#...#...#
7+
#.#.#.#######.###.#####.###.#.#.#.###.#.#
8+
#.#...#.....#...#.....#...#.#...#.#...#.#
9+
#.#######.#.###.###.#.###.#.#.###.#.###.#
10+
#.........#...#...#.#.....#.#.....#.#...#
11+
#############.###.#.#######.#######.#.###
12+
#...............#.#.....#...#.......#...#
13+
#.###############.#####.#.###.#########.#
14+
#.#.......#.#.....#...#...#...#.......#.#
15+
#.#####.#.#.#.#####.#.###.#.###.#####.#.#
16+
#.......#...#...#...#.....#.#.#...#...#.#
17+
###########.###.###########.#.#.#.#####.#
18+
#...#.#.....#.#...#...#.....#...#.....#.#
19+
#.#.#.#.#####.###.#.#.#.#####.#######.#.#
20+
#.#...#.#.....#...#.#...#...#...#...#.#.#
21+
#.#####.#.#.#.#.#.#.#######.###.#.#.#.#.#
22+
#.#.....#.#.#.#.#.#...#.......#.#.#.#...#
23+
#.#.#######.###.#######.#.###.#.#.#.#####
24+
#.#.#.....#.#...#.....#.#.#...#...#...#.#
25+
#.#.#.###.#.#.###.#.#.###.###########.#.#
26+
#.#...#...#.#...#.#.#.....#.........#...#
27+
#.#####.###.###.#.#.#######.#######.###.#
28+
#.#...#.#.....#.#.#.....#...#.....#.....#
29+
#.#.#.#.#####.#.#.#####.#.###.###.#######
30+
#...#.#.#...#.#...#...#...#.....#.......#
31+
#.###.#.#.#.#.#######.#####.#######.#.#.#
32+
#.#...#...#...#.........#...#.#...#.#.#.#
33+
###.#########.#####.###.#.###.#.#.#.#.#.#
34+
#...#.........#.....#...#.#...#.#.#.#.#.#
35+
#.#.#.#########.#####.###.###.#.#.###.#.#
36+
#.#.#.#.......#.....#.#.......#.#.....#.#
37+
#.###.#######.#.###.#.###.#####.#######.#
38+
#.....#.....#...#...#...#.#.....#.......#
39+
#.#####.###.#####.#####.###.#####.#######
40+
#.......#.........#.........#...........#
41+
#########################################

2019/15/lib/aoc/board.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ defmodule AOC.Board do
1010

1111
{min_x, min_y, max_x, max_y} = get_dimensions(board)
1212

13-
Enum.map(min_y..max_y, &(get_row(&1, board, min_x, max_x)))
13+
b = Enum.map(min_y..max_y, &(get_row(&1, board, min_x, max_x)))
1414
|> Enum.join("\n")
15-
|> IO.puts
15+
IO.puts("\n\n#{b}\n\n")
1616
end
1717

1818
board

2019/15/lib/aoc/intcode_agent.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ defmodule AOC.IntcodeAgent do
4242
defp execute({:input, {address}}, state) do
4343
receive do
4444
{:input, value } ->
45-
IO.puts("just received input: #{value}")
4645
state
4746
|> update_program(address, value)
4847
|> advance_pointer(2)

2019/15/lib/aoc/parser.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,30 @@ defmodule AOC.Parser do
1717
|> Enum.with_index
1818
|> Enum.into(%{}, fn { i, j } -> { j, i } end)
1919
end
20+
21+
def parse_2(binary) do
22+
foo = binary
23+
|> String.trim
24+
|> String.split("\n")
25+
|> Enum.with_index
26+
|> Enum.map(&formatted/1)
27+
|> List.flatten
28+
|> Enum.into(%{})
29+
end
30+
31+
def formatted({line, y}) do
32+
line
33+
|> String.graphemes
34+
|> Enum.with_index
35+
|> Enum.map(&(to_tuple(&1, y)))
36+
end
37+
38+
def parse_2 do
39+
{:ok, binary} = File.read("./input2.txt")
40+
parse_2(binary)
41+
end
42+
43+
defp to_tuple({".", x}, y), do: {{x, y}, 1}
44+
defp to_tuple({"#", x}, y), do: {{x, y}, 0}
45+
defp to_tuple({"O", x}, y), do: {{x, y}, 2}
2046
end

2019/15/lib/aoc/runner.ex

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
87139
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+
# IO.puts "part1 : #{ AOC.Runner.part_1 }"
22
IO.puts "part2: #{ AOC.Runner.part_2 }"

2019/15/test/aoc/runner_test.exs

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,16 @@
11
defmodule AOCRunnerTest do
22
use ExUnit.Case
3-
alias AOC.{IntcodeAgent, Parser}
4-
5-
test "first sample works" do
6-
supervisor_pid = self()
7-
computer = spawn_link(fn -> IntcodeAgent.init(%{supervisor: supervisor_pid}) end)
8-
send(computer, {:set_initial, self(), first_program()})
9-
send(computer, :run)
10-
11-
assert(input_loop([], computer) == [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99])
12-
end
13-
14-
test "second sample works" do
15-
supervisor_pid = self()
16-
computer = spawn_link(fn -> IntcodeAgent.init(%{supervisor: supervisor_pid}) end)
17-
send(computer, {:set_initial, self(), second_program()})
18-
send(computer, :run)
19-
20-
assert(input_loop([], computer) == [1219070632396864])
21-
end
22-
23-
test "third sample works" do
24-
supervisor_pid = self()
25-
computer = spawn_link(fn -> IntcodeAgent.init(%{supervisor: supervisor_pid}) end)
26-
send(computer, {:set_initial, self(), third_program()})
27-
send(computer, :run)
28-
29-
assert(input_loop([], computer) == [1125899906842624])
30-
end
31-
32-
defp first_program do
33-
Parser.parse("109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99")
34-
end
35-
36-
defp second_program do
37-
Parser.parse("1102,34915192,34915192,7,4,7,99,0")
38-
end
39-
40-
defp third_program do
41-
Parser.parse("104,1125899906842624,99")
42-
end
43-
44-
defp input_loop(acc, amp) do
45-
receive do
46-
{:terminating, ^amp} ->
47-
IO.puts("terminating")
48-
acc
49-
{:input, value, ^amp} ->
50-
input_loop(acc ++ [value], amp)
51-
end
3+
alias AOC.{Runner, Parser}
4+
5+
test "first maze fill" do
6+
s = """
7+
######
8+
#..###
9+
#.#..#
10+
#.O.##
11+
######
12+
"""
13+
14+
assert(Runner.part_2(Parser.parse_2(s)) == 4)
5215
end
5316
end
54-
55-
56-
57-
# Input.start_link([])
58-
# end
59-
60-
# test "sample best works" do
61-
# Input.start_link([])
62-
# assert(Runner.part_1(Parser.parse("3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")) == 43210)
63-
# end
64-
65-
# test "sample feedback works" do
66-
# assert(Runner.part_2(Parser.parse("3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5")) == 139629729)
67-
# end
68-
# end
69-
70-

0 commit comments

Comments
 (0)