Skip to content

Instantly share code, notes, and snippets.

@cjavdev
Created December 4, 2022 16:44
Show Gist options
  • Save cjavdev/2637d3d898554e8b82a834c25cc80339 to your computer and use it in GitHub Desktop.
Save cjavdev/2637d3d898554e8b82a834c25cc80339 to your computer and use it in GitHub Desktop.
require 'set'
if ARGV.empty?
data = DATA.readlines
else
data = File.readlines(ARGV[0])
end
results = data
.map(&:chomp)
.map { _1.split(/[,-]/) }
.map { _1.map(&:to_i) }
.map { _1.each_slice(2) }
.map { _1.map { |a, b| (a..b).to_set } }
.reduce(0) {|count, (a, b)| a.intersect?(b) ? count + 1 : count }
# Part 1 .reduce(0) {|count, (a, b)| a.subset?(b) || b.subset?(a) ? count + 1 : count }
p results
__END__
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment