From 20a94aef1e36a2819e36dd8b99865055ca058ef1 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 15 Oct 2024 09:32:36 +0200 Subject: Day 20 --- 2015/day20.exs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 2015/day20.exs (limited to '2015') 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}") -- cgit v1.2.3