-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.cs
56 lines (52 loc) · 1.36 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
using System;
using System.Collections.Generic;
namespace AdventOfCode2024
{
internal class Day01 : Solution
{
readonly List<int> left = [];
readonly List<int> right = [];
public Day01(string input)
: base(input)
{
foreach (var line in input.Split('\n'))
{
string[] cols = line.Split(" ");
if (cols.Length == 2)
{
left.Add(int.Parse(cols[0]));
right.Add(int.Parse(cols[1]));
}
}
}
public override string Part1()
{
left.Sort();
right.Sort();
int total = 0;
for (int i = 0; i < left.Count; i++)
{
total += Math.Abs(left[i] - right[i]);
}
return total.ToString();
}
public override string Part2()
{
int total = 0;
int multiplier = 0;
foreach (var l in left)
{
foreach (var r in right)
{
if (l == r)
{
multiplier++;
}
}
total += (l * multiplier);
multiplier = 0;
}
return total.ToString();
}
}
}