File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 3-5
2+ 10-14
3+ 16-20
4+ 12-18
5+
6+ 1
7+ 5
8+ 8
9+ 11
10+ 17
11+ 32
Original file line number Diff line number Diff line change 11a , b = File
2- . read ( '../ input-real05 .txt' )
2+ . read ( 'input-05 .txt' )
33 . split ( "\n \n " )
44
5- ranges = a . lines . map { it . scan ( /\d +/ ) . map ( &:to_i ) . then { Range . new * it } }
5+ ranges = a . lines . map { Range . new * it . scan ( /\d +/ ) . map ( &:to_i ) }
66ids = b . lines . map ( &:to_i )
77
8- p ids . count { |id | ranges . any? { |range | range . cover? id } }
8+
9+ print "Part 1: "
10+ puts ids . count { |id | ranges . any? { |range | range . cover? id } }
11+
12+
13+ print "Part 2: "
14+
15+ without_overlap = ranges
16+ . sort_by { [ it . min , -it . size ] }
17+ . reduce ( [ ] ) { |rs , r |
18+ next [ r ] if rs . empty?
19+ next rs if rs . last . cover? r
20+ next rs if r . min < rs . last . min && r . max <= rs . last . max
21+
22+ start = [ r . min , rs . last . max +1 ] . max
23+ next_range = Range . new ( start , r . max )
24+ next rs if rs . last . cover? next_range
25+
26+ [ *rs , next_range ]
27+ }
28+
29+ puts without_overlap . sum { it . size }
You can’t perform that action at this time.
0 commit comments