diff --git a/2015/README.md b/2015/README.md index 6574c8c..1bbb4bf 100644 --- a/2015/README.md +++ b/2015/README.md @@ -17,7 +17,7 @@ | | | [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 | | +| [20] | 21 | 22 | 23 | 24 | 25 | | [1]: ./lib/2015/1.ex [2]: ./lib/2015/2.ex @@ -38,3 +38,4 @@ [17]: ./lib/2015/17.ex [18]: ./lib/2015/18.ex [19]: ./lib/2015/19.ex +[20]: ./lib/2015/20.ex diff --git a/2015/lib/2015/20.ex b/2015/lib/2015/20.ex new file mode 100644 index 0000000..ebe9da3 --- /dev/null +++ b/2015/lib/2015/20.ex @@ -0,0 +1,56 @@ +import AOC + +aoc 2015, 20 do + def p1 do + goal = input() + + Stream.iterate(1, &(&1 + 1)) + |> Enum.find(fn house -> + gifts_for_house_1(house) >= goal + end) + end + + def p2 do + goal = input() + + Stream.iterate(1, &(&1 + 1)) + |> Enum.find(fn house -> + gifts_for_house_2(house) >= goal + end) + end + + def gifts_for_house_1(house) do + elves = + house + |> divisors() + |> Enum.sum() + + elves * 10 + end + + defp gifts_for_house_2(house) do + elves = + house + |> divisors() + |> Enum.filter(&(div(house, &1) <= 50)) + |> Enum.sum() + + elves * 11 + end + + defp divisors(n) do + e = n |> :math.sqrt() |> trunc + + Enum.flat_map(1..e, fn + x when rem(n, x) != 0 -> [] + x when x != div(n, x) -> [x, div(n, x)] + x -> [x] + end) + end + + defp input do + input_string() + |> String.trim() + |> String.to_integer() + end +end diff --git a/README.md b/README.md index f11f9c4..8b652f5 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ | Year | Stars | Languages | | - | - | - | -| [2015] | **38/50** 🌟 | Elixir | +| [2015] | **40/50** 🌟 | Elixir | | 2016 | | | | [2017] | **18/50** 🌟 | Haskell | | 2018 | | |