-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.jl
50 lines (43 loc) · 1.07 KB
/
04.jl
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
"""
Day 4: Camp Cleanup
https://adventofcode.com/2022/day/4
"""
function count_overlaps(lines)
fully_contains = 0
overlaps = 0
for line in lines
m = match(r"(\d+)-(\d+),(\d+)-(\d+)", line)
a = parse(Int, m[1])
b = parse(Int, m[2])
c = parse(Int, m[3])
d = parse(Int, m[4])
if a <= c && d <= b
fully_contains += 1
overlaps += 1
elseif c <= a && b <= d
fully_contains += 1
overlaps += 1
elseif a <= c && c <= b
overlaps += 1
elseif a <= d && d <= b
overlaps += 1
elseif c <= a && a <= d
overlaps += 1
elseif c <= b && b <= d
overlaps += 1
end
end
return fully_contains, overlaps
end
function part1(lines)
return count_overlaps(lines)[1]
end
function part2(lines)
return count_overlaps(lines)[2]
end
example = readlines("examples/04.txt")
input = readlines("inputs/04.txt")
@assert part1(example) == 2
println(part1(input))
@assert part2(example) == 4
println(part2(input))