4.rb 474 B

1234567891011121314151617181920212223242526272829
  1. require 'set'
  2. @input = File.read("4.input").split("\n")
  3. def set_from_range(range)
  4. (from, to) = range.split("-")
  5. return Set.new((from.to_i..to.to_i).to_a)
  6. end
  7. score_1 = 0
  8. score_2 = 0
  9. @input.each do |line|
  10. (left, right) = line.split(",")
  11. left = set_from_range(left)
  12. right = set_from_range(right)
  13. if left.subset?(right) or right.subset?(left) then
  14. score_1 += 1
  15. end
  16. if left.intersect?(right) then
  17. score_2 += 1
  18. end
  19. end
  20. puts score_1
  21. puts score_2