r/adventofcode • • Dec 10 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 10 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 12 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 10: Adapter Array ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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

EDIT: Global leaderboard gold cap reached at 00:08:42, megathread unlocked!

69 Upvotes

1.1k comments sorted by

View all comments

1

u/Mayalabielle Dec 10 '20 edited Dec 10 '20

Elixir

I find the solution in the weirdest possible way, for a reason I can't see yet, my solution counts everything twice, so if I divide the result by 2 at the end, it just work.

defmodule AOC.D10 do
  import AOC.Helper.Input

  def run1(test \\ false) do
    {_, %{1 => ones, 3 => threes}} =
      get_input("D10", test)
      |> split_input()
      |> Enum.map(&String.to_integer/1)
      |> Enum.sort()
      |> find_jolts_deltas()

    ones * threes
  end

  def run2(test \\ false) do
    get_input("D10", test)
    |> split_input()
    |> Enum.map(&String.to_integer/1)
    |> Enum.sort()
    |> complete_adapters_chain()
    |> count_combinations(:start_cache)
  end

  defmodule Memo do
    use Agent

    def start_link() do
      Agent.start_link(fn -> %{} end, name: __MODULE__)
    end

    def get(key) do
      Agent.get(__MODULE__, &Map.get(&1, key))
    end

    def update(key, value) do
      Agent.update(__MODULE__, &Map.put(&1, key, value))
    end
  end

  def complete_adapters_chain(adapters) do
    [0 | adapters] ++ [List.last(adapters) + 3]
  end

  def count_combinations([h | t], :start_cache) do
    {:ok, pid} = Memo.start_link()
    res = count_combinations(h, t)
    Agent.stop(pid)
    trunc(res / 2)
  end

  def count_combinations(_, []), do: 1

  def count_combinations(current, [h | t]) do
    case Memo.get(current) do
      nil ->
        if h - current <= 3 do
          value = count_combinations(current, t) + count_combinations(h, t)
          Memo.update(current, value)
          value
        else
          0
        end

      v ->
        v
    end
  end

  def find_jolts_deltas(inputs) do
    counters = %{0 => 0, 1 => 1, 2 => 1, 3 => 1}

    Enum.reduce(inputs, {List.first(inputs), counters}, fn v, {previous, counters} ->
      {v, Map.put(counters, v - previous, Map.get(counters, v - previous) + 1)}
    end)
  end
end

Code is available on github

If you have any clue, I spend to much time on the computer today and my brain is fucked.

1

u/daggerdragon Dec 10 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to remove your oversized code and just use the GitHub link. Thanks!