# frozen_string_literal: true
require 'set'
def count_hours(lights)
hours = Set.new
lights.each_line do |pair|
arr = pair.split(/\s+/).map(&:to_i)
hours.merge(arr.first...arr.last)
end
hours.count
end
ex = '1 3
2 3
4 5'
a = '2 4
3 6
1 3
6 8'
b = '6 8
5 8
8 9
5 7
4 7'
bonus = '15 18
13 16
9 12
3 4
17 20
9 11
17 18
4 5
5 6
4 5
5 6
13 16
2 3
15 17
13 14'
puts count_hours(ex)
puts count_hours(a)
puts count_hours(b)
puts count_hours(bonus)
IyBmcm96ZW5fc3RyaW5nX2xpdGVyYWw6IHRydWUKCnJlcXVpcmUgJ3NldCcKCmRlZiBjb3VudF9ob3VycyhsaWdodHMpCiAgaG91cnMgPSBTZXQubmV3CiAgbGlnaHRzLmVhY2hfbGluZSBkbyB8cGFpcnwKICAgIGFyciA9IHBhaXIuc3BsaXQoL1xzKy8pLm1hcCgmOnRvX2kpCiAgICBob3Vycy5tZXJnZShhcnIuZmlyc3QuLi5hcnIubGFzdCkKICBlbmQKICBob3Vycy5jb3VudAplbmQKCmV4ID0gJzEgMwoyIDMKNCA1JwoKYSA9ICcyIDQKMyA2CjEgMwo2IDgnCgpiID0gJzYgOAo1IDgKOCA5CjUgNwo0IDcnCgpib251cyA9ICcxNSAxOAoxMyAxNgo5IDEyCjMgNAoxNyAyMAo5IDExCjE3IDE4CjQgNQo1IDYKNCA1CjUgNgoxMyAxNgoyIDMKMTUgMTcKMTMgMTQnCgpwdXRzIGNvdW50X2hvdXJzKGV4KQpwdXRzIGNvdW50X2hvdXJzKGEpCnB1dHMgY291bnRfaG91cnMoYikKcHV0cyBjb3VudF9ob3Vycyhib251cyk=