-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask01.cs
47 lines (38 loc) · 1017 Bytes
/
Task01.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
using FluentAssertions;
using NUnit.Framework;
namespace AoC_2024;
[TestFixture]
public class Task01
{
[Test]
[TestCase(
@"3 4
4 3
2 5
1 3
3 9
3 3",
11)]
[TestCase(@"Task01.txt", 1722302)]
public void Task(string input, int expected)
{
input = File.Exists(input) ? File.ReadAllText(input) : input;
var result = 0;
var lines = input.SplitLines();
var left = new List<int>();
var right = new List<int>();
foreach (var line in lines)
{
var split = line.SplitEmpty(" ").Select(int.Parse).ToArray();
left.Add(split[0]);
right.Add(split[1]);
}
var leftStack = new Stack<int>(left.OrderBy(x => x));
var rightStack = new Stack<int>(right.OrderBy(x => x));
while (leftStack.Count > 0 && rightStack.Count > 0)
{
result += Math.Abs(leftStack.Pop() - rightStack.Pop());
}
result.Should().Be(expected);
}
}