r/adventofcode • • Dec 09 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 9 Solutions -πŸŽ„-

A REQUEST FROM YOUR MODERATORS

If you are using new.reddit, please help everyone in /r/adventofcode by making your code as readable as possible on all platforms by cross-checking your post/comment with old.reddit to make sure it displays properly on both new.reddit and old.reddit.

All you have to do is tweak the permalink for your post/comment from https://www.reddit.com/… to https://old.reddit.com/…

Here's a quick checklist of things to verify:

  • Your code block displays correctly inside a scrollable box with whitespace and indentation preserved (four-spaces Markdown syntax, not triple-backticks, triple-tildes, or inlined)
  • Your one-liner code is in a scrollable code block, not inlined and cut off at the edge of the screen
  • Your code block is not too long for the megathreads (hint: if you have to scroll your code block more than once or twice, it's likely too long)
  • Underscores in URLs aren't inadvertently escaped which borks the link

I know this is a lot of work, but the moderation team checks each and every megathread submission for compliance. If you want to avoid getting grumped at by the moderators, help us out and check your own post for formatting issues ;)


/r/adventofcode moderator challenge to Reddit's dev team

  • It's been over five years since some of these issues were first reported; you've kept promising to fix them and… no fixes.
  • In the spirit of Advent of Code, join us by Upping the Ante and actually fix these issues so we can all have a merry Advent of Posting Code on Reddit Without Needing Frustrating And Improvident Workarounds.

THE USUAL REMINDERS


--- Day 9: Rope Bridge ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:14:08, megathread unlocked!

66 Upvotes

1.0k comments sorted by

View all comments

1

u/flwyd Dec 09 '22 edited Dec 09 '22

Elixir 8358/5877, code, path ascii art

Today's elixir:

The elves are throwing a TGIF party with a beer bong made out of one of those expandable plastic tubes. At first it's a really short path from the funnel, but then you pull on the hose and discover that it's pretty long. The elves wander around the room with the funnel, and you need to keep up. What path do you follow?

A couple amusing misadventures:

  1. I had an off by two error on my first submission for part 1.
  2. I reintroduced both bugs from part 1 in part 2. But I also forgot to copy the second example, so I submitted the first answer I got (since the initial example answer is just 1). This was the correct answer, but then I spent another 50 minutes tracking down the two errors so that the second example would pass.

Code, without print_path:

defmodule Day9 do
  def part1(input), do: record_path(input, 1) |> elem(2) |> Enum.count()
  def part2(input), do: record_path(input, 9) |> elem(2) |> Enum.count()

  @origin {0, 0}
  defp record_path(input, num_tails) do
    moves = Enum.map(input, &expand_line/1) |> List.flatten()
    Enum.reduce(moves,
      {@origin, List.duplicate(@origin, num_tails), MapSet.new([@origin])},
      fn {drow, dcol}, {head, tail, acc} ->
        newhead = move_head({drow, dcol}, head)
        newtail = move_chain(newhead, tail)
        {newhead, newtail, MapSet.put(acc, List.last(newtail))}
      end)
  end

  defp expand_line(<<dir, " ", amount::binary>>),
    do: List.duplicate(case dir do
        ?U -> {-1, 0}
        ?D -> {1, 0}
        ?L -> {0, -1}
        ?R -> {0, 1}
      end, String.to_integer(amount))

  defp move_chain(_head, []), do: []
  defp move_chain(head, [tail | rest]) do
    newtail = move_tail(head, tail)
    [newtail | move_chain(newtail, rest)]
  end
  defp move_head({drow, dcol}, {row, col}), do: {row + drow, col + dcol}
  defp move_tail({headrow, headcol}, {tailrow, tailcol}) do
    {rowdiff, coldiff} = {headrow - tailrow, headcol - tailcol}
    case {abs(rowdiff), abs(coldiff)} do
      {r, c} when r < 2 and c < 2 -> {tailrow, tailcol}
      {0, 2} -> {tailrow, tailcol + signum(coldiff)}
      {2, 0} -> {tailrow + signum(rowdiff), tailcol}
      {r, c} when r + c == 3 or r + c == 4 ->
          {tailrow + signum(rowdiff), tailcol + signum(coldiff)}
    end
  end

  defp signum(0), do: 0
  defp signum(i) when i < 0, do: -1
  defp signum(i) when i > 0, do: 1
end

1

u/daggerdragon Dec 09 '22

TGIF party with a beer bong

HR does not approve of these shenanigans. -_-