1
available, fresh =
2
File.read('5.input')
3
.split("\n")
4
.lazy
5
.map { |e| e.split('-').map(&:to_i) }
6
.reject(&:empty?)
7
.partition { |ns| ns.length == 1 }
8
9
fresh.sort_by!(&:first)
10
p fresh
11
fresh = fresh.reduce([]) do |acc, (from, to)|
12
if (last = acc.last) && from <= last[1]
13
if to > last[1]
14
last[1] = to
15
acc
16
else
17
acc
18
end
19
else
20
acc << [from, to]
21
end
22
end
23
24
p fresh.any? { |(from, to)|
25
(fresh - [[from, to]]).any? { |(ofrom, oto)|
26
ofrom <= to && from <= oto
27
}
28
}
29
30
# p available.count { |(a)| fresh.any? { |(f, t)| a >= f && a <= t } }
31
p fresh.sum { |(f, t)| t - f + 1 }
32