-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabb_advent4.ex
79 lines (68 loc) · 1.67 KB
/
labb_advent4.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
defmodule Advent4 do
def readFile(path) do
rows = File.stream!(path)
rows
end
def parse(path) do
rows = File.stream!(path)
Enum.reduce(rows, [],
fn(row, acc) ->
#Splitta upp raden
[elf1, elf2] = String.split(row, ",")
[e1_s, e1_e] = String.split(elf1, "-")
[e2_s, e2_e] = String.split(elf2, "-")
# Parsa till integers
{e1_start, _} = Integer.parse(e1_s)
{e1_end, _} = Integer.parse(e1_e)
{e2_start, _} = Integer.parse(e2_s)
{e2_end, _} = Integer.parse(e2_e)
# Return
[{e1_start, e1_end, e2_start, e2_end} |acc]
end
)
end
# Del 1
def count_fully_contained(pairs) do
Enum.reduce(pairs, 0,
fn(pair, acc) ->
{e1_s, e1_e, e2_s, e2_e} = pair
e1_contains_e2 = a_fully_contains_b({e1_s, e1_e}, {e2_s, e2_e})
e2_contains_e1 = a_fully_contains_b({e2_s, e2_e}, {e1_s, e1_e})
if e1_contains_e2 || e2_contains_e1 do
1+acc
else
acc
end
end
)
end
# Del 2
def count_overlapping(pairs) do
Enum.reduce(pairs, 0,
fn(pair, acc) ->
if overlaps(pair) do
1+acc
else
acc
end
end
)
end
def a_fully_contains_b({e1_s, e1_e}, {e2_s, e2_e}) do
if(e2_s>=e1_s && e2_e<=e1_e) do
true
else
false
end
end
def fully_contains(_, _) do
:error
end
def in_range(num, {min, max}) do
num>=min && num<=max
end
def overlaps({e1_s, e1_e, e2_s, e2_e}) do
in_range(e1_s, {e2_s, e2_e}) || in_range(e1_e, {e2_s, e2_e})
|| in_range(e2_s, {e1_s, e1_e}) || in_range(e2_e, {e1_s, e1_e})
end
end