From 3fb8a5ec483f79f6acb4473819a688c93f67a01b Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Thu, 10 Dec 2020 01:01:27 -0800 Subject: [PATCH] Day 10 Part 2 --- 2020/day10/day10.ex | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/2020/day10/day10.ex b/2020/day10/day10.ex index 6f4a67e..7ee65ab 100644 --- a/2020/day10/day10.ex +++ b/2020/day10/day10.ex @@ -1,3 +1,56 @@ +defmodule Cache do + use GenServer + + def start_link(cache) do + GenServer.start_link(__MODULE__, cache) + end + + def put(pid, key, val) do + GenServer.call(pid, {:put, key, val}) + end + + def get(pid, key) do + GenServer.call(pid, {:get, key}) + end + + @impl true + def init(cache) do + {:ok, cache} + end + + @impl true + def handle_call({:get, key}, _from, cache) do + {:reply, Map.get(cache, key), cache} + end + + @impl true + def handle_call({:put, key, val}, _from, cache) do + {:reply, val, Map.put(cache, key, val)} + end +end + +defmodule Search do + defp count_paths_iter(start, neighbors, cache) do + cached = Cache.get(cache, start) + + if cached do + cached + else + count = + neighbors.(start) + |> Enum.map(&count_paths_iter(&1, neighbors, cache)) + |> Enum.sum() + + Cache.put(cache, start, count) + end + end + + def count_paths(start, neighbors, goal) do + {:ok, cache_pid} = Cache.start_link(%{goal => 1}) + count_paths_iter(start, neighbors, cache_pid) + end +end + defmodule Day10 do defp read_input do Path.expand('input', Path.dirname(__ENV__.file)) @@ -43,6 +96,15 @@ defmodule Day10 do counts[3] * counts[1] end + + def part2 do + adapters = read_input() |> parse_adapters() + device = Enum.max(adapters) + + neighbors = fn a1 -> Enum.filter(adapters, &can_stack?(a1, &1)) end + Search.count_paths(0, neighbors, device) + end end Day10.part1() |> IO.inspect() +Day10.part2() |> IO.inspect()