Advent of Code Day 13

part 1:

part 2:

1 Like

I wonder if anyone here could help me out to position doctest outputs better. Right now I have

@doc """

      iex> part2(example_input())
      "#####
      #...#
      #...#
      #...#
      #####"

      iex> part2(my_input())
      "###..###...##..#..#.####.###..#....###.
      #..#.#..#.#..#.#.#..#....#..#.#....#..#
      #..#.#..#.#....##...###..###..#....#..#
      ###..###..#....#.#..#....#..#.#....###.
      #.#..#....#..#.#.#..#....#..#.#....#.#.
      #..#.#.....##..#..#.#....###..####.#..#"

  """

And I don’t like that the first line is misplaced by one ". I’d like the output to be more like

@doc """

      iex> part2(example_input())
      #####
      #...#
      #...#
      #...#
      #####

      iex> part2(my_input())
      ###..###...##..#..#.####.###..#....###.
      #..#.#..#.#..#.#.#..#....#..#.#....#..#
      #..#.#..#.#....##...###..###..#....#..#
      ###..###..#....#.#..#....#..#.#....###.
      #.#..#....#..#.#.#..#....#..#.#....#.#.
      #..#.#.....##..#..#.#....###..####.#..#

  """

I’d be very thankful :slight_smile:

The following should work:

iex> part2(my_input()) |> IO.puts
1 Like
mix test
Compiling 1 file (.ex)
.....................#####
#...#
#...#
#...#
#####


  1) doctest Day13.part2/1 (3) (Day13Test)
     test/days_test.exs:6
     Doctest failed
     doctest:
       iex> part2(example_input()) |> IO.puts
       "#####
       #...#
       #...#
       #...#
       #####"
     code:  part2(example_input()) |> IO.puts === "#####
            #...#
            #...#
            #...#
            #####"
     left:  :ok
     right: "#####\n#...#\n#...#\n#...#\n#####"
     stacktrace:
       lib/day13.ex:119: Day13 (module)

.................................

Finished in 0.5 seconds (0.5s async, 0.00s sync)
55 doctests, 1 failure

Randomized with seed 958985

Sorry, I should’ve been clearer that these are doctests, not commands in iex repl. Using IO.puts in doctests results in :ok being returned instead of the value that’s been printed.

1 Like

Here’s my solution.

This advent of code (not day13 specifically) is making me fall in love with comprehensions, I never realized they were so convenient and powerful.

Not spending a lot of time on this one.

This one was surprisingly straightforward for a Day 13. I am really curious how the next ones will be. I have never got this far in the AoC before. :slight_smile:

1 Like

Looks similar to others, and not particularly pretty. But had a little fun and added a function that would print the code in color. E.g.

aoc-2021-13-print-2

Sure you could make it fancier with some box-drawing chars.

LiveBook solution:

1 Like

y2021/day-13.livemd

Outputs something like this :grinning_face_with_smiling_eyes:

⬛️⬛️⬛️⬜️⬜️⬜️⬛️⬛️⬜️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬛️⬛️⬜️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬜️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬜️
⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬜️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬜️⬜️
⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬜️⬜️⬛️⬛️⬛️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬛️⬜️⬜️⬜️⬛️⬜️⬜️⬜️⬜️⬛️⬛️⬜️⬜️⬜️⬛️⬜️⬜️⬜️
⬛️⬛️⬛️⬜️⬜️⬛️⬜️⬛️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬛️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬜️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬜️⬜️
⬛️⬜️⬜️⬜️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬜️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬜️⬜️
⬛️⬜️⬜️⬜️⬜️⬜️⬛️⬛️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬛️⬛️⬛️⬜️⬛️⬜️⬜️⬛️⬜️⬛️⬛️⬛️⬛️

Also used NimbleParsec just for fun.

1 Like

Part 2 solved in Livebook with VegaLite :slight_smile:

(Full solution)

5 Likes

Implemented part 2 before even realizing it is already the solution for part 1 :grinning_face_with_smiling_eyes:

I used this function to map over the coordinates:

def fold_coord({x, y}, {n, axis}) do
  case axis do
    :x -> if x < n, do: {x, y}, else: {2 * n - x, y}
    :y -> if y < n, do: {x, y}, else: {x, 2 * n - y}
  end
end

Full solution here

I optimized my solution to use MapSet after watching @josevalim 's video

dots =
  commands
  |> Enum.take(1)
  |> Enum.reduce(dots, fn
    {"x", at}, dots ->
      for {x, y} <- dots, into: MapSet.new(), do: {at - abs(at - x), y}

    {"y", at}, dots ->
      for {x, y} <- dots, into: MapSet.new(), do: {x, at - abs(at - y)}
  end)

Enum.count(dots)

full solution

1 Like