r/adventofcode • • Dec 13 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 13 Solutions -🎄-

--- Day 13: Mine Cart Madness ---


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

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

Card prompt: Day 13

Transcript:

Elven chronomancy: for when you absolutely, positively have 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 at 00:44:25!

24 Upvotes

148 comments sorted by

View all comments

Show parent comment

1

u/lasseebert Dec 13 '18

Another one in Elixir :) I inlo included stuff relevant for part 2. Full solution here: https://github.com/lasseebert/advent_of_code_2018/blob/master/lib/advent/day13.ex

defmodule Advent.Day13 do
  @doc "Part 2"
  def last_cart_location(input) do
    {tracks, carts} = parse(input)
    run_to_one(tracks, carts)
  end

  defp run_to_one(tracks, carts) do
    carts = tick_remove_crash(tracks, carts)

    if carts |> Map.keys() |> length() == 1 do
      carts |> Map.keys() |> hd()
    else
      run_to_one(tracks, carts)
    end
  end

  defp tick_remove_crash(tracks, carts) do
    carts
    |> Map.keys()
    |> Enum.sort_by(fn {x, y} -> {y, x} end)
    |> Enum.reduce(carts, fn cart_point, carts ->
      case Map.fetch(carts, cart_point) do
        {:ok, cart} ->
          carts = Map.delete(carts, cart_point)

          case move_cart(tracks, carts, {cart_point, cart}) do
            {:ok, {new_point, new_cart}} ->
              Map.put(carts, new_point, new_cart)

            {:crash, point} ->
              Map.delete(carts, point)
          end

        :error ->
          carts
      end
    end)
  end

  def move_cart(tracks, other_carts, {point, cart}) do
    new_point = move_forward(point, cart)

    if Map.has_key?(other_carts, new_point) do
      {:crash, new_point}
    else
      new_cart = turn(tracks, new_point, cart)
      {:ok, {new_point, new_cart}}
    end
  end

  defp move_forward({x, y}, {:north, _}), do: {x, y - 1}
  defp move_forward({x, y}, {:south, _}), do: {x, y + 1}
  defp move_forward({x, y}, {:west, _}), do: {x - 1, y}
  defp move_forward({x, y}, {:east, _}), do: {x + 1, y}

  defp turn(tracks, point, cart) do
    track = Map.fetch!(tracks, point)

    case {track, cart} do
      {:horizontal, cart} -> cart
      {:vertical, cart} -> cart
      {:slash_curve, {:east, turn}} -> {:north, turn}
      {:slash_curve, {:west, turn}} -> {:south, turn}
      {:slash_curve, {:north, turn}} -> {:east, turn}
      {:slash_curve, {:south, turn}} -> {:west, turn}
      {:backslash_curve, {:east, turn}} -> {:south, turn}
      {:backslash_curve, {:west, turn}} -> {:north, turn}
      {:backslash_curve, {:north, turn}} -> {:west, turn}
      {:backslash_curve, {:south, turn}} -> {:east, turn}
      {:intersection, {:south, :left}} -> {:east, :straight}
      {:intersection, {:south, :straight}} -> {:south, :right}
      {:intersection, {:south, :right}} -> {:west, :left}
      {:intersection, {:north, :left}} -> {:west, :straight}
      {:intersection, {:north, :straight}} -> {:north, :right}
      {:intersection, {:north, :right}} -> {:east, :left}
      {:intersection, {:east, :left}} -> {:north, :straight}
      {:intersection, {:east, :straight}} -> {:east, :right}
      {:intersection, {:east, :right}} -> {:south, :left}
      {:intersection, {:west, :left}} -> {:south, :straight}
      {:intersection, {:west, :straight}} -> {:west, :right}
      {:intersection, {:west, :right}} -> {:north, :left}
    end
  end

  defp parse(input) do
    input
    |> String.split("\n", trim: true)
    |> Enum.with_index()
    |> Enum.flat_map(fn {line, y} ->
      line
      |> String.graphemes()
      |> Enum.with_index()
      |> Enum.map(fn {char, x} -> {{x, y}, char} end)
    end)
    |> Enum.reduce({%{}, %{}}, fn {point, char}, {tracks, carts} ->
      case char do
        "|" -> {Map.put(tracks, point, :vertical), carts}
        "-" -> {Map.put(tracks, point, :horizontal), carts}
        "/" -> {Map.put(tracks, point, :slash_curve), carts}
        "\\" -> {Map.put(tracks, point, :backslash_curve), carts}
        "+" -> {Map.put(tracks, point, :intersection), carts}
        "v" -> {Map.put(tracks, point, :vertical), Map.put(carts, point, {:south, :left})}
        "^" -> {Map.put(tracks, point, :vertical), Map.put(carts, point, {:north, :left})}
        "<" -> {Map.put(tracks, point, :horizontal), Map.put(carts, point, {:west, :left})}
        ">" -> {Map.put(tracks, point, :horizontal), Map.put(carts, point, {:east, :left})}
        " " -> {tracks, carts}
      end
    end)
  end
end