Created
December 11, 2020 07:30
-
-
Save bossek/026b1c008e6356692547d0fa26e424f8 to your computer and use it in GitHub Desktop.
AoC 2020 Day 10 Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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