-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.cs
55 lines (46 loc) · 1.4 KB
/
Day1.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
using System;
using System.Collections.Generic;
using System.Linq;
using ZUtilLib;
namespace AOC2024_CS_CPP
{
public class Day1 : AOCSolutionBase
{
public override void Run1(string[] inputLines)
{
// Take each side as lists, sort them, then take sum of differences per index
List<int> left = new(), right = new();
for (int lN = 0; lN < inputLines.Length; lN++)
{
IEnumerable<int> nPair = inputLines[lN].Trim().Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
left.Add(nPair.First());
right.Add(nPair.Last());
}
left.Sort();
right.Sort();
long sumOfDiffs = 0;
for (int lN = 0; lN < inputLines.Length; lN++)
{
sumOfDiffs += Math.Abs(left[lN] - right[lN]);
}
Console.WriteLine($"Result: {sumOfDiffs}");
}
public override void Run2(string[] inputLines)
{
// Take each side as lists, sort them, then take sum of differences per index
List<int> left = new(), right = new();
for (int lN = 0; lN < inputLines.Length; lN++)
{
IEnumerable<int> nPair = inputLines[lN].Trim().Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
left.Add(nPair.First());
right.Add(nPair.Last());
}
long simScore = 0;
foreach (int n in left)
{
simScore += n * right.Count(n.Equals);
}
Console.WriteLine($"Result: {simScore}");
}
}
}