generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.cs
64 lines (53 loc) · 1.11 KB
/
Day01.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
namespace aoc.Year2024;
internal partial class Day01
{
private readonly Example example = new(
"""
3 4
4 3
2 5
1 3
3 9
3 3
""");
public (int[] first, int[] second) Parse(string input)
{
var parser =
Numerics.IntegerInt32.ThenIgnore(SpanX.Space)
.Then(Numerics.IntegerInt32)
.Lines()
.Select(lines => (
lines.Select(x => x.Item1).ToArray(),
lines.Select(x => x.Item2).ToArray()
));
return parser.Parse(input);
}
internal partial class Part1
{
public Part1()
{
Expect(example, 11);
}
public int Solve((int[] first, int[] second) input)
{
var totalDifference =
input.first.Order()
.Zip(input.second.Order(), Distance).Sum();
return totalDifference;
int Distance(int x, int y) => Math.Abs(x - y);
}
}
internal partial class Part2
{
public Part2()
{
Expect(example, 31);
}
public int Solve((int[] first, int[] second) input)
{
var secondGroups = input.second.ToLookup(x => x);
var similarityScore = input.first.Select(x => x * secondGroups[x].Count()).Sum();
return similarityScore;
}
}
}