-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_15.ex
28 lines (24 loc) · 824 Bytes
/
day_15.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
defmodule AdventOfCode.Y2020.Day15 do
@moduledoc """
--- Day 15: Rambunctious Recitation ---
Problem Link: https://adventofcode.com/2020/day/15
"""
def run_1, do: [6, 19, 0, 5, 7, 13, 1] |> play(2020)
def run_2, do: [6, 19, 0, 5, 7, 13, 1] |> play(30_000_000)
def play(input, stop) do
tab = :ets.new(:tab, [])
true = :ets.insert(tab, Enum.with_index(input, 1))
speak({List.last(input), tab, length(input)}, stop)
end
defp speak({last, _hist, stop}, stop), do: last
defp speak({last, hist, turn}, stop) do
case :ets.lookup(hist, last) do
[] ->
true = :ets.insert(hist, {last, turn})
speak({0, hist, turn + 1}, stop)
[{_, prev_turn}] ->
true = :ets.insert(hist, {last, turn})
speak({turn - prev_turn, hist, turn + 1}, stop)
end
end
end