fork download
  1. # frozen_string_literal: true
  2.  
  3. require 'set'
  4.  
  5. def count_hours(lights)
  6. hours = Set.new
  7. lights.each_line do |pair|
  8. arr = pair.split(/\s+/).map(&:to_i)
  9. hours.merge(arr.first...arr.last)
  10. end
  11. hours.count
  12. end
  13.  
  14. ex = '1 3
  15. 2 3
  16. 4 5'
  17.  
  18. c1 = '2 4
  19. 3 6
  20. 1 3
  21. 6 8'
  22.  
  23. c2 = '6 8
  24. 5 8
  25. 8 9
  26. 5 7
  27. 4 7'
  28.  
  29. bonus = '15 18
  30. 13 16
  31. 9 12
  32. 3 4
  33. 17 20
  34. 9 11
  35. 17 18
  36. 4 5
  37. 5 6
  38. 4 5
  39. 5 6
  40. 13 16
  41. 2 3
  42. 15 17
  43. 13 14'
  44.  
  45. puts count_hours(ex)
  46. puts count_hours(c1)
  47. puts count_hours(c2)
  48. puts count_hours(bonus)
Success #stdin #stdout 0s 28688KB
stdin
Standard input is empty
stdout
3
7
5
14