r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 4 Solutions -πŸŽ„-

--- Day 4: Repose Record ---


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 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


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!

38 Upvotes

340 comments sorted by

View all comments

1

u/Sentreen Dec 05 '18

Elixir

I decided to create a map which maps guard ids to a collection of how many times they were awake each minute. The reduction to create that map got pretty.... messy. However, once I had that map, it was fairly easy to get the answer to both parts.

Note that the aoc 2018, 4 is a simply macro that generates a module with some predefined functions (specifically the input, which loads the input file into a stream of strings).

aoc 2018, 4 do
  def get do
    input()
    |> Enum.sort()
    |> Stream.map(&Regex.scan(~r/\[1518-(\d\d)-(\d\d) (\d\d):(\d\d)\] (.*)/, &1))
    |> Stream.map(&tl(hd(&1)))
    |> Stream.map(&parse_line/1)
    |> Enum.reduce({-1, -1, %{}}, fn
      %{entry: {:guard, id}}, {_, _, m} -> {id, -1, m}
      %{entry: :sleep, minute: min}, {id, _, m} -> {id, min, m}
      %{entry: :wake, minute: min}, {id, start, m} ->
        m = Enum.reduce(
          start .. min - 1,
          m,
          &Map.update(&2, id, %{}, fn times -> Map.update(times, &1, 1, fn cur -> cur + 1 end) end))
        {id, -1, m}
    end)
    |> elem(2)
  end

  def parse_line([month, day, hour, minute, entry]) do
    %{
      entry: parse_entry(entry),
      month: String.to_integer(month),
      day: String.to_integer(day),
      hour: String.to_integer(hour),
      minute: String.to_integer(minute)
    }
  end

  def parse_entry("falls asleep"), do: :sleep
  def parse_entry("wakes up"), do: :wake

  def parse_entry(entry) do
    [[_, id]] = Regex.scan(~r/Guard #(\d+) begins shift/, entry)
    {:guard, String.to_integer(id)}
  end

  def p1 do
    id =
      get()
      |> Enum.map(fn {id, map} -> {id, Enum.sum(Map.values(map))} end)
      |> Enum.max_by(&elem(&1, 1))
      |> elem(0)
    min =
      get()[id]
      |> Enum.max_by(&elem(&1, 1))
      |> elem(0)
    id * min
  end

  def p2 do
    id =
      get()
      |> Enum.map(fn {id, map} -> {id, Enum.max_by(map, &elem(&1, 1))} end)
      |> Enum.max_by(fn {_, {_, x}} -> x end)
      |> elem(0)
    min =
      get()[id]
      |> Enum.max_by(&elem(&1, 1))
      |> elem(0)
    id * min
  end
end

1

u/aoc-fan Dec 05 '18 edited Dec 05 '18

Check my solution

get_content = fn path ->
  with {:ok, file} = File.open(path),
    content = IO.read(file, :all),
    :ok = File.close(file) do
      content
  end
end

test_01 = get_content.("../inputs/04-test.txt")
puzzle_input = get_content.("../inputs/04.txt")

line_to_log = fn
  ([_, ts, m, a]) -> [a, ts, String.to_integer(m)]
  ([_, ts, _, _, g]) -> ["begins", ts, String.to_integer(g)]
end

update_log = fn (l, k, from, to) ->
  with r = Enum.to_list(from..to-1) do
    Map.update(l, k, r, &(&1 ++ r))
  end
end

build_log = fn [a, _, ap], [lg, from, log] ->
  case a do
    "begins" -> [ap, from, log]
    "falls" -> [lg, ap, log]
    "wakes" -> [lg, from, update_log.(log, lg, from, ap)]
  end
end

build_report = fn sl ->
  sl
  |> Enum.reduce(%{}, fn x, acc -> Map.update(acc, x, 1, &(&1 + 1)) end)
  |> Enum.reduce([0, 0], fn {ik, iv}, [mk, mv] -> if iv > mv, do: [ik, iv], else: [mk, mv] end)
end

solve = fn l, s ->
  l
  |> Enum.reduce(fn mg, g -> if Enum.at(mg, s) > Enum.at(g, s), do: mg, else: g end)
  |> (&(Enum.at(&1, 0) * Enum.at(&1, 2))).()
end

response_record = fn ip ->
  ip
  |> (&Regex.scan(~r/\[(\d+-\d+-\d+\s\d+:(\d+))]\s(falls|wakes|Guard #(\d+))/, &1)).()
  |> Enum.map(line_to_log)
  |> Enum.sort_by(&(Enum.at(&1, 1)))
  |> Enum.reduce([0, 0, %{}], build_log)
  |> Enum.at(2)
  |> Enum.to_list()
  |> Enum.map(fn {g, sl} -> [g, Enum.count(sl)] ++ build_report.(sl) end)
  |> (&([solve.(&1, 1), solve.(&1, 3)])).()
end

[240, 4455] = response_record.(test_01)
[138280, 89347] = response_record.(puzzle_input)

IO.puts("Done")

1

u/Sentreen Dec 05 '18

Could you fix your formatting? It's pretty impossible to read right now.