-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent13.exs
38 lines (32 loc) · 1 KB
/
advent13.exs
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
defmodule Advent13 do
def part1(rows),
do:
parse(rows)
|> Enum.filter(fn [level, range] -> is_caught?(level, range, 0) end)
|> Enum.map(fn [level, range] -> level * range end)
|> Enum.sum()
|> IO.inspect()
def part2(rows), do: recurse(0, parse(rows)) |> IO.inspect()
def recurse(delay, rows) do
case Enum.any?(rows, fn [level, range] -> is_caught?(level, range, delay) end) do
true -> recurse(delay + 1, rows)
false -> delay
end
end
def is_caught?(level, range, delay), do: rem(delay + level, 2 * (range - 1)) == 0
def parse(rows),
do:
rows
|> Enum.map(fn [a, b] -> [String.to_integer(a), String.to_integer(b)] end)
|> Enum.sort(fn [_, r1], [_, r2] -> r1 < r2 end)
end
File.read!("input13")
|> String.trim()
|> String.split("\n")
|> Enum.map(fn row -> String.split(row, ": ") end)
|> Advent13.part1()
File.read!("input13")
|> String.trim()
|> String.split("\n")
|> Enum.map(fn row -> String.split(row, ": ") end)
|> Advent13.part2()