-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.ex
60 lines (53 loc) · 1.14 KB
/
day4.ex
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
defmodule Day4 do
def part1(input) do
input
|> parse_input()
|> find_fully_contains_pair()
|> Enum.count()
|> IO.inspect(label: "part1")
end
def part2(input) do
input
|> parse_input()
|> find_overlaps_pair()
|> Enum.count()
|> IO.inspect(label: "part2")
end
defp parse_input(input) do
input
|> String.split("\n")
|> Enum.map(fn pair ->
pair
|> String.split(",")
|> Enum.map(fn range ->
range
|> String.split("-")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end)
|> List.to_tuple()
end)
end
defp find_fully_contains_pair(pairs) do
Enum.filter(pairs, &fully_contains?/1)
end
defp fully_contains?({{l1, r1}, {l2, r2}}) do
cond do
l1 <= l2 and r2 <= r1 -> true
l2 <= l1 and r1 <= r2 -> true
true -> false
end
end
defp find_overlaps_pair(pairs) do
Enum.filter(pairs, &overlaps?/1)
end
defp overlaps?({{l1, r1}, {l2, r2}}) do
cond do
l1 > r2 or l2 > r1 -> false
true -> true
end
end
end
File.read!("input.txt")
|> tap(&Day4.part1/1)
|> tap(&Day4.part2/1)