Skip to content

Commit 672d35c

Browse files
committed
2022/04 comments
1 parent 41b8862 commit 672d35c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

2022/Day04/Solution.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,30 @@ namespace AdventOfCode.Y2022.Day04;
55

66
[ProblemName("Camp Cleanup")]
77
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+
814
public object PartOne(string input) => DuplicatedWorkCount(input, Contains);
915
public object PartTwo(string input) => DuplicatedWorkCount(input, Overlaps);
1016

11-
record struct Range(int from, int to);
12-
1317
// True if r1 contains r2 [ { } ]
1418
bool Contains(Range r1, Range r2) => r1.from <= r2.from && r2.to <= r1.to;
1519

1620
// True if r1 overlaps r2 { [ } ], the other direction is not checked.
1721
bool Overlaps(Range r1, Range r2) => r1.to >= r2.from && r1.from <= r2.to;
1822

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.
2327
private int DuplicatedWorkCount(
2428
string input,
2529
Func<Range, Range, bool> rangeCheck
2630
) {
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)]
2832
var parseRanges = (string line) =>
2933
from range in line.Split(',')
3034
let fromTo = range.Split('-').Select(int.Parse)

0 commit comments

Comments
 (0)