-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcafetaria.jl
More file actions
47 lines (43 loc) · 1.28 KB
/
Copy pathcafetaria.jl
File metadata and controls
47 lines (43 loc) · 1.28 KB
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
function parse_input(filename::String)
f = open(filename, "r")
lines = readlines(f)
idx = findfirst(item -> item == "", lines)
# println(idx)
ranges = [parse.(Int, split(line, "-")) for line in lines[begin:idx-1]]
# println(ls)
ingredient = parse.(Int, lines[idx+1:end])
# println(ingredient)
return ranges, ingredient
end
function is_valid(ranges, ig)
for (a,b) ∈ ranges
if ig ∈ a:b
return 1
end
end
return 0
end
function partII(ranges)
sort!(ranges, lt=(x,y)->x[1] < y[1]) # we sort the range so they are in increasing order
nb_ranges = length(ranges)
idx = 1
n = 0
while idx < nb_ranges
entry = ranges[idx][1]
exit = ranges[idx][2]
idx += 1
while idx <= nb_ranges && exit >= ranges[idx][1] # we check if the next range overlap the current one (and that we are stil inbound)
exit = max(exit, ranges[idx][2])
idx += 1
end
# println(entry, " - ", exit, " (", idx, ")")
n += exit - entry + 1 # Théorème de Mickey
end
return n
end
# ranges, ingredient = parse_input("example.txt")
ranges, ingredient = parse_input("input.txt")
I = sum(is_valid.((ranges,), ingredient))
println(I)
II = partII(ranges)
println(II)