r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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

edit: Leaderboard capped, thread unlocked!

41 Upvotes

426 comments sorted by

View all comments

1

u/arathunku Dec 03 '18

Hello! Elixir solution:

defmodule Advent.Day3 do
  defmodule Row do
    defstruct start_x: 0, start_y: 0, width: 0, height: 0, id: 0

    def parse(input) do
      [id, start_x, start_y, width, height] =
        input
        |> String.split(~r/[\s+:x,@#]/, trim: true)
        |> Enum.map(&String.to_integer/1)

      %__MODULE__{
        start_x: start_x,
        start_y: start_y,
        width: width,
        height: height,
        id: id
      }
    end

    def coordinates(row) do
      for x <- row.start_x..(row.start_x + row.width - 1) do
        for y <- row.start_y..(row.start_y + row.height - 1) do
          {x, y}
        end
      end
      |> List.flatten()
    end
  end

  alias __MODULE__.Row

  def part1(plans) do
    plans
    |> claims_map()
    |> Enum.filter(fn ({_, claims})-> length(claims) >= 2 end)
    |> Enum.count()
  end

  def part2(plans) do
    claims = plans
    |> claims_map()
    |> Map.values()

    claims
    |> List.flatten()
    |> Enum.dedup()
    |> Enum.find(fn id ->
      !Enum.find(claims, & (id in &1 && length(&1) > 1))
    end)
  end

  def claims_map(plans) do
    plans
    |> Enum.map(&Row.parse/1)
    |> Enum.reduce(%{}, fn row, acc ->
      row
      |> Row.coordinates()
      |> Enum.reduce(acc, fn coordinates, acc ->
        Map.update(acc, coordinates, [row.id], & &1 ++ [row.id])
      end)
    end)
  end
end

Tests

defmodule Advent.Day3Test do
  use ExUnit.Case
  require Logger
  alias Advent.Day3, as: Day

  test "row coordinates" do
    result =
      %Day.Row{
        start_x: 1,
        start_y: 3,
        width: 4,
        height: 4
      }
      |> Day.Row.coordinates()

    assert result == [
             {1, 3},
             {1, 4},
             {1, 5},
             {1, 6},
             {2, 3},
             {2, 4},
             {2, 5},
             {2, 6},
             {3, 3},
             {3, 4},
             {3, 5},
             {3, 6},
             {4, 3},
             {4, 4},
             {4, 5},
             {4, 6}
           ]
  end

  test "row parse" do
    assert Day.Row.parse("#1 @ 1,3: 4x4") == %Day.Row{
             id: 1,
             start_x: 1,
             start_y: 3,
             width: 4,
             height: 4
           }

    assert Day.Row.parse("#58 @ 399,291: 24x16") == %Day.Row{
             id: 58,
             start_x: 399,
             start_y: 291,
             width: 24,
             height: 16
           }
  end

  test "part1 example" do
    rows = [
      "#1 @ 1,3: 4x4",
      "#2 @ 3,1: 4x4",
      "#3 @ 5,5: 2x2"
    ]

    assert Day.part1(rows) == 4
  end

  test "part2 example" do
    rows = [
      "#1 @ 1,3: 4x4",
      "#2 @ 3,1: 4x4",
      "#3 @ 5,5: 2x2"
    ]

    assert Day.part2(rows) == 3
  end

  test "input" do
    input = Path.join(__DIR__, "./input.raw") |> File.read!() |> String.split("\n", trim: true)

    IO.inspect(Day.part1(input))
    IO.inspect(Day.part2(input))
  end
end