@@ -5,26 +5,30 @@ namespace AdventOfCode.Y2022.Day04;
5
5
6
6
[ ProblemName ( "Camp Cleanup" ) ]
7
7
class Solution : Solver {
8
+
9
+ // Each line of the input represents two ranges - job done by two elves.
10
+ // We need to find those lines where the elves did some work twice.
11
+ // Part 1 and 2 differs in how we define 'duplicated work'.
12
+ record struct Range ( int from , int to ) ;
13
+
8
14
public object PartOne ( string input ) => DuplicatedWorkCount ( input , Contains ) ;
9
15
public object PartTwo ( string input ) => DuplicatedWorkCount ( input , Overlaps ) ;
10
16
11
- record struct Range ( int from , int to ) ;
12
-
13
17
// True if r1 contains r2 [ { } ]
14
18
bool Contains ( Range r1 , Range r2 ) => r1 . from <= r2 . from && r2 . to <= r1 . to ;
15
19
16
20
// True if r1 overlaps r2 { [ } ], the other direction is not checked.
17
21
bool Overlaps ( Range r1 , Range r2 ) => r1 . to >= r2 . from && r1 . from <= r2 . to ;
18
22
19
- // DuplicatedWorkCount goes over the lines in the input, converts them to
20
- // ranges A and B, and counts how many times rangeCheck(A,B) or
21
- // rangeCheck(B, A) is true. The check is applied in both ways so that the
22
- // Contains and Overlaps functions don't have to check each directions .
23
+ // DuplicatedWorkCount parses each input line into ranges and applies
24
+ // rangeCheck on them to find duplicated work. RangeCheck doesnt have to be
25
+ // symmetrical in its arguments, but DuplicatedWorkCount makes it so calling
26
+ // it twice with the arguments swapped .
23
27
private int DuplicatedWorkCount (
24
28
string input ,
25
29
Func < Range , Range , bool > rangeCheck
26
30
) {
27
- // '36-41,35-40' becomes [Range(36, 41), Range(35, 40)]
31
+ // E.g. '36-41,35-40' becomes [Range(36, 41), Range(35, 40)]
28
32
var parseRanges = ( string line ) =>
29
33
from range in line . Split ( ',' )
30
34
let fromTo = range . Split ( '-' ) . Select ( int . Parse )
0 commit comments