summaryrefslogtreecommitdiff
path: root/2015/day20.exs
blob: 331eda91c793e524715e1fc3d58b8f66d607639a (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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}")