File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,21 +15,17 @@ public object PartOne(string input) {
1515 }
1616
1717 public object PartTwo ( string input ) {
18- var ( ranges , _) = Parse ( input ) ;
18+ // Merge overlapping ranges into disjoint intervals. First sort ranges
19+ // by start so that any range that can extend the current one appears at
20+ // a higher index. Then we walk the list once extending ranges[i] with any
21+ // later range that overlaps with it.
22+ var ranges = Parse ( input ) . ranges . OrderBy ( x => x . start ) . ToList ( ) ;
1923
20- // Merge overlapping ranges into the disjoint intervals.
21- // First sort ranges by start so that any range that can extend
22- // the current one appears at a higher index. Then we walk the list
23- // once extending ranges[i] with any later range that overlaps with it.
24- ranges = ranges . OrderBy ( x => x . start ) . ToList ( ) ;
2524 for ( var i = 0 ; i < ranges . Count ; i ++ ) {
2625 int j = i + 1 ;
2726 while ( j < ranges . Count ) {
28- var rangeI = ranges [ i ] ;
29- var rangeJ = ranges [ j ] ;
30-
31- if ( rangeJ . start <= rangeI . end ) {
32- ranges [ i ] = new Range ( rangeI . start , Math . Max ( rangeI . end , rangeJ . end ) ) ;
27+ if ( ranges [ j ] . start <= ranges [ i ] . end ) {
28+ ranges [ i ] = new Range ( ranges [ i ] . start , Math . Max ( ranges [ i ] . end , ranges [ j ] . end ) ) ;
3329 ranges . RemoveAt ( j ) ;
3430 } else {
3531 j ++ ;
You can’t perform that action at this time.
0 commit comments