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

2

u/newreddit0r Dec 13 '18

Elixir here too :)

defmodule AdventOfCode.Day13 do
  def run_part1(input) do
    input
    |> prepare_state
    |> tick_until_crashed
  end

  def run_part2(input) do
    input
    |> prepare_state
    |> tick_until_one_remaining
  end

  def prepare_state(input) do
    parsed_input = parse_input(input)

    map = parse_map(parsed_input)
    players = parse_players(parsed_input)

    {map, players}
  end

  def tick_until_crashed(state) do
    case crash_location(state) do
      {x, y} ->
        {x, y}

      nil ->
        state
        |> tick
        |> tick_until_crashed
    end
  end

  def tick_until_one_remaining(state) do
    new_state =
      state
      |> tick
      |> remove_crashed_players

    {_, new_players} = new_state

    case length(new_players) do
      1 ->
        new_players

      _ ->
        new_state
        |> tick_until_one_remaining
    end
  end

  def print_state({map, players}) do
    {xsize, ysize} = board_size(map) |> IO.inspect()

    empty_board =
      for x <- 0..xsize, y <- 0..ysize do
        {{x, y}, " "}
      end
      |> Map.new()

    board_players = Enum.map(players, fn {{x, y}, _, _} -> {{x, y}, "O"} end) |> Map.new()

    board =
      empty_board
      |> Map.merge(map)
      |> Map.merge(board_players)

    for y <- 0..ysize do
      for x <- 0..xsize do
        IO.write(Map.get(board, {x, y}))
      end

      IO.puts("")
    end

    {map, players}
  end

  def crash_location({_, players}) do
    crashed_group =
      players
      |> Enum.group_by(fn {c, _, _} -> c end)
      |> Enum.find(fn {_, p} -> length(p) > 1 end)

    case crashed_group do
      {{x, y}, _} -> {x, y}
      nil -> nil
    end
  end

  def remove_crashed_players({state, players}) do
    crashed_players =
      players
      |> Enum.group_by(fn {c, _, _} -> c end)
      |> Enum.filter(fn {_, p} -> length(p) > 1 end)
      |> Enum.flat_map(fn {_, p} -> p end)

    {state, players |> Enum.filter(fn player -> !Enum.member?(crashed_players, player) end)}
  end

  def board_size(map) do
    Enum.reduce(map, {0, 0}, fn {{nx, ny}, _}, {x, y} ->
      {max(nx, x), max(ny, y)}
    end)
  end

  def tick({map, players}) do
    players_ordered =
      players
      |> Enum.sort(fn {{x1, y1}, _, _}, {{x2, y2}, _, _} -> x2 > x1 && y2 > y1 end)

    new_players =
      players_ordered
      |> Enum.reduce([], fn {{x, y} = coordinates, {dx, dy}, [next_move | rest_moves] = moves} =
                              player,
                            acc ->
        is_at = Map.get(map, {x, y})

        is_crashed_by = Enum.any?(acc, fn {c, _, _} -> coordinates == c end)

        player =
          case {is_crashed_by, is_at} do
            {true, _} ->
              player

            {_, "-"} ->
              {{x + dx, y + dy}, {dx, dy}, moves}

            {_, "|"} ->
              {{x + dx, y + dy}, {dx, dy}, moves}

            {_, "/"} ->
              {{x - dy, y - dx}, {-dy, -dx}, moves}

            {_, "\\"} ->
              {{x + dy, y + dx}, {dy, dx}, moves}

            {_, "+"} ->
              case next_move do
                :left -> {{x + dy, y - dx}, {dy, -dx}, rest_moves ++ [next_move]}
                :straight -> {{x + dx, y + dy}, {dx, dy}, rest_moves ++ [next_move]}
                :right -> {{x - dy, y + dx}, {-dy, dx}, rest_moves ++ [next_move]}
              end
          end

        [player | acc]
      end)

    {map, new_players}
  end

  def parse_map(input) do
    input
    |> Enum.map(fn {coordinates, thing} ->
      {coordinates,
       case thing do
         ">" -> "-"
         "<" -> "-"
         "v" -> "|"
         "^" -> "|"
         _ -> thing
       end}
    end)
    |> Map.new()
  end

  def parse_players(input) do
    input
    |> Enum.filter(fn {_, t} -> t in [">", "<", "v", "^"] end)
    |> Enum.map(fn {c, t} ->
      {c, velocity_by_player_marker(t), [:left, :straight, :right]}
    end)
  end

  def velocity_by_player_marker(marker) do
    case marker do
      ">" -> {1, 0}
      "<" -> {-1, 0}
      "^" -> {0, -1}
      "v" -> {0, 1}
    end
  end

  def parse_input(input) do
    input
    |> String.split("\n")
    |> Enum.zip(0..9_999_999_999)
    |> Enum.map(fn {a, b} -> {b, a} end)
    |> Enum.map(fn {y, a} ->
      {y,
       String.split(a, "")
       |> Enum.filter(&(&1 != ""))
       |> Enum.zip(0..9_999_999_999)
       |> Enum.map(fn {a, b} -> {b, a} end)}
    end)
    |> Enum.map(fn {y, xs} ->
      Enum.map(xs, fn {x, thing} ->
        {{x, y}, thing}
      end)
    end)
    |> List.flatten()
  end
end

File.read!("13")
|> AdventOfCode.Day13.run_part1()
|> IO.inspect()

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