-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.cs
60 lines (51 loc) · 1.67 KB
/
Day04.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace AdventOfCode2022;
internal class Day04
{
private readonly List<(List<int>, List<int>)> values;
public Day04(IEnumerable<string> input)
{
values = input
.Select(s => s.Split(',').Select(ParseRange).ToArray())
.Select(x => (x[0], x[1]))
.ToList();
}
public int PartOne()
{
return values.Count(x =>
{
var intersect = x.Item1.Intersect(x.Item2);
return intersect.SequenceEqual(x.Item1) || intersect.SequenceEqual(x.Item2);
});
}
public int PartTwo()
{
return values.Count(x => x.Item1.Any(y => x.Item2.Contains(y)) || x.Item2.Any(y => x.Item1.Contains(y)));
}
private static List<int> ParseRange(string range)
{
var start = int.Parse(range.Split('-')[0]);
var end = int.Parse(range.Split('-')[1]);
return Enumerable.Range(start, end - start + 1).ToList();
}
}
public class Day04Test
{
private const string InputFile = "Day04.Input.txt";
private const string ExampleFile = "Day04.Example.txt";
[Theory]
[FileData(ExampleFile, FileContents.StringPerLine, 2)]
[FileData(InputFile, FileContents.StringPerLine, 542)]
public void TestPartOne(IEnumerable<string> input, int expected)
{
var solution = new Day04(input);
Assert.Equal(expected, solution.PartOne());
}
[Theory]
[FileData(ExampleFile, FileContents.StringPerLine, 4)]
[FileData(InputFile, FileContents.StringPerLine, 900)]
public void TestPartTwo(IEnumerable<string> input, int expected)
{
var solution = new Day04(input);
Assert.Equal(expected, solution.PartTwo());
}
}