-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.cs
76 lines (66 loc) · 2.18 KB
/
Day13.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace AdventOfCode2022;
internal class Day13
{
private readonly List<string> _input;
public Day13(IEnumerable<string> input)
{
_input = input
.Where(s => !string.IsNullOrWhiteSpace(s))
.Select(s => s.Replace("10", "A"))
.ToList();
}
public int PartOne()
{
return _input
.Chunk(2)
.Indexed(start: 1)
.Where(x => IsCorrectlyOrdered(x.Value[0], x.Value[1]))
.Sum(x => x.Index);
}
public int PartTwo()
{
var dividers = new[] { "[[2]]", "[[6]]" };
var comparer = Comparer<string>.Create((left, right) => IsCorrectlyOrdered(left, right) ? -1 : 1);
return _input
.Concat(dividers)
.Order(comparer)
.Indexed(start: 1)
.Where(x => dividers.Contains(x.Value))
.Product(x => x.Index);
}
private bool IsCorrectlyOrdered(string left, string right)
{
var (lhead, ltail) = (left[0], left[1..]);
var (rhead, rtail) = (right[0], right[1..]);
return (lhead, rhead) switch
{
(char l, char r) when l == r => IsCorrectlyOrdered(ltail, rtail),
(']', _) => true,
(_, ']') => false,
('[', char r) => IsCorrectlyOrdered(ltail, $"{r}]{rtail}"),
(char l, '[') => IsCorrectlyOrdered($"{l}]{ltail}", rtail),
(char l, char r) => l < r,
};
}
}
public class Day13Test
{
private const string InputFile = "Day13.Input.txt";
private const string ExampleFile = "Day13.Example.txt";
[Theory]
[FileData(ExampleFile, FileContents.StringPerLine, 13)]
[FileData(InputFile, FileContents.StringPerLine, 6070)]
public void TestPartOne(IEnumerable<string> input, int expected)
{
var solution = new Day13(input);
Assert.Equal(expected, solution.PartOne());
}
[Theory]
[FileData(ExampleFile, FileContents.StringPerLine, 140)]
[FileData(InputFile, FileContents.StringPerLine, 20758)]
public void TestPartTwo(IEnumerable<string> input, int expected)
{
var solution = new Day13(input);
Assert.Equal(expected, solution.PartTwo());
}
}