Skip to content

Instantly share code, notes, and snippets.

@bossek
Created December 11, 2020 07:30
Show Gist options
  • Select an option

  • Save bossek/026b1c008e6356692547d0fa26e424f8 to your computer and use it in GitHub Desktop.

Select an option

Save bossek/026b1c008e6356692547d0fa26e424f8 to your computer and use it in GitHub Desktop.
AoC 2020 Day 10 Part 2
defmodule Day10P2 do
def run do
data = "data/10" |> File.read!() |> String.split() |> Enum.map(&String.to_integer/1)
[0 | data]
|> Enum.sort(:desc)
|> Enum.reduce([{hd(data) + 3, 1}], &[{&1, sum3(&1, &2)} | &2])
|> hd()
|> elem(1)
end
defp sum3(i, [{_, ac}, {_, bc}, {c, cc} | _]) when c == i + 3, do: ac + bc + cc
defp sum3(i, [{_, ac}, {b, bc} | _]) when b == i + 2, do: ac + bc
defp sum3(_, [{_, ac} | _]), do: ac
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment