diff options
-rw-r--r-- | 2015/day20.exs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/2015/day20.exs b/2015/day20.exs new file mode 100644 index 0000000..331eda9 --- /dev/null +++ b/2015/day20.exs @@ -0,0 +1,62 @@ +{presentTarget, ""} = + File.read!("inputs/day20.txt") + |> String.trim() + |> Integer.parse() + +countPresents1 = fn houseNumber -> + sqrt = floor(:math.sqrt(houseNumber)) + + Enum.filter(1..sqrt, &(rem(houseNumber, &1) == 0)) + |> Enum.map(fn lower -> + higher = div(houseNumber, lower) + + if higher != lower do + (higher + lower) * 10 + else + lower * 10 + end + end) + |> Enum.sum() +end + +[firstHouse1] = + Stream.filter(1..div(presentTarget, 10), &(countPresents1.(&1) >= presentTarget)) + |> Enum.take(1) + +IO.puts("First house part 1: #{firstHouse1}") + +countPresents2 = fn houseNumber -> + sqrt = floor(:math.sqrt(houseNumber)) + + Enum.filter(1..sqrt, &(rem(houseNumber, &1) == 0)) + |> Enum.map(fn lower -> + higher = div(houseNumber, lower) + + lowerDeliveries = + if higher < 50 do + lower + else + 0 + end + + higherDeliveries = + if lower < 50 do + higher + else + 0 + end + + if higher != lower do + (lowerDeliveries + higherDeliveries) * 11 + else + lowerDeliveries * 11 + end + end) + |> Enum.sum() +end + +[firstHouse2] = + Stream.filter(1..div(presentTarget, 10), &(countPresents2.(&1) >= presentTarget)) + |> Enum.take(1) + +IO.puts("First house part 2: #{firstHouse2}") |