You’re not supposed to share the problem text or input according to the AoC ToS. Although that’s probably a lost cause by now
Enjoyed this one. Part 2 took me by surprise, but it was fun. I managed to reuse the functions from part 1, just updated motion function to keep track of the intermediary knots in a list, with map_reduce
to simultaneously enumerate the knots + track the previous one.
defp step_head({x, y}, "R"), do: {x + 1, y}
defp step_head({x, y}, "L"), do: {x - 1, y}
defp step_head({x, y}, "U"), do: {x, y + 1}
defp step_head({x, y}, "D"), do: {x, y - 1}
defp step_tail({hx, hy}, {tx, ty}) when abs(hx - tx) <= 1 and abs(hy - ty) <= 1, do: {tx, ty}
defp step_tail({tx, hy}, {tx, ty}) when hy - ty > 0, do: {tx, ty + 1}
defp step_tail({tx, hy}, {tx, ty}) when hy - ty < 0, do: {tx, ty - 1}
defp step_tail({hx, ty}, {tx, ty}) when hx - tx > 0, do: {tx + 1, ty}
defp step_tail({hx, ty}, {tx, ty}) when hx - tx < 0, do: {tx - 1, ty}
defp step_tail({hx, hy}, {tx, ty}) do
dx = if hx - tx > 0, do: 1, else: -1
dy = if hy - ty > 0, do: 1, else: -1
{tx + dx, ty + dy}
end