This commit is contained in:
Caleb Webber 2023-11-11 12:26:23 -05:00
parent 9ad7062bf0
commit 323afa6c4b

View file

@ -12,7 +12,7 @@ defmodule Day9 do
end end
def execute(line, state) do def execute(line, state) do
[command, count] = line |> String.split(" "); [ movement, count] = line |> String.split(" ");
{ count, _ } = Integer.parse(count); { count, _ } = Integer.parse(count);
Enum.reduce( Enum.reduce(
@ -20,9 +20,9 @@ defmodule Day9 do
state, state,
fn (_, state) -> fn (_, state) ->
{ rope, tail_positions } = state { rope, tail_positions } = state
rope = command rope = movement
|> get_movement_direction() |> string_to_movement_direction()
|> move_head(rope) |> move_rope_by_head(rope)
{ rope, MapSet.put(tail_positions, List.last(rope)) } { rope, MapSet.put(tail_positions, List.last(rope)) }
end end
); );
@ -33,20 +33,20 @@ defmodule Day9 do
MapSet.size(tail_positions) MapSet.size(tail_positions)
end end
defp move_head(direction, rope) do defp move_rope_by_head(direction, rope) do
[head | tail ] = rope [head | tail ] = rope
move_rope([add_2_tuple(head, direction)], tail) reconcile_tail([add_2_tuple(head, direction)], tail)
end end
defp move_rope(good, bad) when bad == [] do defp reconcile_tail(_, tail) when tail == [] do
good tail
end end
defp move_rope(good, bad) do defp reconcile_tail(head, tail) do
leader = good |> List.last() leader = head |> List.last()
[ follower | tail ] = bad [ follower | tail ] = tail
move_rope(good ++ [keep_min_distance(follower, leader, 1)], tail) reconcile_tail(head ++ [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
@ -56,17 +56,24 @@ defmodule Day9 do
tail tail
else else
{a, b} = difference {a, b} = difference
megan = fn (x) -> if x != 0 do
if x > 0 do 1 else -1 end add_2_tuple(tail, {megan(a, 0, -1, 1), megan(b, 0, -1, 1)})
else
0
end
end
add_2_tuple(tail, {megan.(a), megan.(b)})
end end
end end
defp get_movement_direction(direction) do defp megan(x, _, _, pos) when x > 0 do
pos
end
defp megan(x, _, neg, _) when x < 0 do
neg
end
defp megan(0, zero, _, _) do
zero
end
defp string_to_movement_direction(direction) do
case direction do case direction do
"R" -> {1, 0} "R" -> {1, 0}
"L" -> {-1, 0} "L" -> {-1, 0}