add day9 part 2
This commit is contained in:
parent
70d3b9cce8
commit
9ad7062bf0
3 changed files with 44 additions and 33 deletions
|
@ -17,7 +17,7 @@ defmodule AOCRunner do
|
||||||
run_until_exit = fn line, acc ->
|
run_until_exit = fn line, acc ->
|
||||||
{_, state} = acc
|
{_, state} = acc
|
||||||
|
|
||||||
put_elem(acc, 1, advent.execute(line, state)) |>
|
put_elem(acc, 1, advent.execute(line |> String.trim(), state)) |>
|
||||||
then(fn {last_line, s} ->
|
then(fn {last_line, s} ->
|
||||||
{(if both_empty?(line, last_line), do: :halt, else: :cont), {String.trim(line), s}}
|
{(if both_empty?(line, last_line), do: :halt, else: :cont), {String.trim(line), s}}
|
||||||
end)
|
end)
|
||||||
|
|
59
lib/day9.ex
59
lib/day9.ex
|
@ -1,39 +1,56 @@
|
||||||
defmodule Day9 do
|
defmodule Day9 do
|
||||||
def init_state(p) when p == :part1 do
|
def init_state(p) when p == :part1 do
|
||||||
{ {0,0}, {0,0}, MapSet.new() }
|
{ [ {0,0}, {0,0} ], MapSet.new() }
|
||||||
|
end
|
||||||
|
|
||||||
|
def init_state(p) when p == :part2 do
|
||||||
|
{ [ {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}], MapSet.new() }
|
||||||
|
end
|
||||||
|
|
||||||
|
def execute(line, state) when line == "" do
|
||||||
|
state
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute(line, state) do
|
def execute(line, state) do
|
||||||
if String.trim(line) == "" do
|
|
||||||
state
|
|
||||||
else
|
|
||||||
[command, count] = line |> String.split(" ");
|
[command, count] = line |> String.split(" ");
|
||||||
{ count, _ } = Integer.parse(count);
|
{ count, _ } = Integer.parse(count);
|
||||||
|
|
||||||
move_rope = fn (movement, {head, tail, tail_positions}) ->
|
|
||||||
head = movement |> get_movement_tuple() |> add_2_tuple(head);
|
|
||||||
tail = tail |> keep_min_distance(head, 1)
|
|
||||||
tail_positions = MapSet.put(tail_positions, tail)
|
|
||||||
{head, tail, tail_positions}
|
|
||||||
end
|
|
||||||
|
|
||||||
Enum.reduce(
|
Enum.reduce(
|
||||||
1..count,
|
1..count,
|
||||||
state,
|
state,
|
||||||
fn (_, state) ->
|
fn (_, state) ->
|
||||||
#IO.puts(command)
|
{ rope, tail_positions } = state
|
||||||
#IO.puts(inspect(state))
|
rope = command
|
||||||
new_state = move_rope.(command, state)
|
|> get_movement_direction()
|
||||||
#IO.puts(inspect(new_state))
|
|> move_head(rope)
|
||||||
new_state
|
{ rope, MapSet.put(tail_positions, List.last(rope)) }
|
||||||
end
|
end
|
||||||
);
|
);
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_answer(state) do
|
||||||
|
{_, tail_positions } = state
|
||||||
|
MapSet.size(tail_positions)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp move_head(direction, rope) do
|
||||||
|
[head | tail ] = rope
|
||||||
|
move_rope([add_2_tuple(head, direction)], tail)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp move_rope(good, bad) when bad == [] do
|
||||||
|
good
|
||||||
|
end
|
||||||
|
|
||||||
|
defp move_rope(good, bad) do
|
||||||
|
leader = good |> List.last()
|
||||||
|
[ follower | tail ] = bad
|
||||||
|
|
||||||
|
move_rope(good ++ [keep_min_distance(follower, leader, 1)], tail)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp keep_min_distance(tail, head, min_distance) do
|
defp keep_min_distance(tail, head, min_distance) do
|
||||||
difference = distance_vector(tail, head)
|
difference = distance_vector(tail, head)
|
||||||
#IO.puts(inspect(difference))
|
|
||||||
|
|
||||||
if max_coord_difference(tail, head) <= min_distance do
|
if max_coord_difference(tail, head) <= min_distance do
|
||||||
tail
|
tail
|
||||||
|
@ -49,12 +66,7 @@ defmodule Day9 do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_answer(state) do
|
defp get_movement_direction(direction) do
|
||||||
{_, _, tail_positions } = state
|
|
||||||
MapSet.size(tail_positions)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp get_movement_tuple(direction) do
|
|
||||||
case direction do
|
case direction do
|
||||||
"R" -> {1, 0}
|
"R" -> {1, 0}
|
||||||
"L" -> {-1, 0}
|
"L" -> {-1, 0}
|
||||||
|
@ -73,7 +85,6 @@ defmodule Day9 do
|
||||||
|
|
||||||
defp max_coord_difference(x, y) do
|
defp max_coord_difference(x, y) do
|
||||||
{a, b} = { abs(elem(x,0)-elem(y,0)), abs(elem(x,1)-elem(y,1)) }
|
{a, b} = { abs(elem(x,0)-elem(y,0)), abs(elem(x,1)-elem(y,1)) }
|
||||||
#IO.puts("max_coord_difference: " <> inspect({a,b}))
|
|
||||||
max(a, b)
|
max(a, b)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
2
main.exs
2
main.exs
|
@ -1 +1 @@
|
||||||
AOCRunner.run(Day9, :part1)
|
AOCRunner.run(Day9, :part2)
|
||||||
|
|
Loading…
Add table
Reference in a new issue