1
0
Fork 0

solve 2015 day 20

This commit is contained in:
sloane 2024-11-26 16:00:32 -05:00
parent 46f3ae87dc
commit 9b99d9446a
Signed by: sloanelybutsurely
SSH key fingerprint: SHA256:8SBnwhl+RY3oEyQxy1a9wByPzxWM0x+/Ejc+sIlY5qQ
3 changed files with 59 additions and 2 deletions

View file

@ -17,7 +17,7 @@
| | | [1] | [2] | [3] | [4] | [5] | | | | [1] | [2] | [3] | [4] | [5] |
| [6] | [7] | [8] | [9] | [10] | [11] | [12] | | [6] | [7] | [8] | [9] | [10] | [11] | [12] |
| [13] | [14] | [15] | [16]| [17] | [18] | [19] | | [13] | [14] | [15] | [16]| [17] | [18] | [19] |
| 20 | 21 | 22 | 23 | 24 | 25 | | | [20] | 21 | 22 | 23 | 24 | 25 | |
[1]: ./lib/2015/1.ex [1]: ./lib/2015/1.ex
[2]: ./lib/2015/2.ex [2]: ./lib/2015/2.ex
@ -38,3 +38,4 @@
[17]: ./lib/2015/17.ex [17]: ./lib/2015/17.ex
[18]: ./lib/2015/18.ex [18]: ./lib/2015/18.ex
[19]: ./lib/2015/19.ex [19]: ./lib/2015/19.ex
[20]: ./lib/2015/20.ex

56
2015/lib/2015/20.ex Normal file
View file

@ -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

View file

@ -6,7 +6,7 @@
| Year | Stars | Languages | | Year | Stars | Languages |
| - | - | - | | - | - | - |
| [2015] | **38/50** 🌟 | Elixir | | [2015] | **40/50** 🌟 | Elixir |
| 2016 | | | | 2016 | | |
| [2017] | **18/50** 🌟 | Haskell | | [2017] | **18/50** 🌟 | Haskell |
| 2018 | | | | 2018 | | |