summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2024-10-15 09:32:36 +0200
committerJustin Wernick <justin@worthe-it.co.za>2024-10-15 09:32:36 +0200
commit20a94aef1e36a2819e36dd8b99865055ca058ef1 (patch)
treea78446c683f2d6f200036fecb5eb6116882a6595
parent7e11a79a17a48e0f9a24fe8bb703476e30845968 (diff)
Day 20
-rw-r--r--2015/day20.exs62
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}")