-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD01.cs
93 lines (76 loc) · 2.14 KB
/
D01.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
namespace AOC2024;
/// <summary>
/// Day 1: Historian Hysteria
/// </summary>
public class D01
{
private readonly AOCHttpClient _client = new AOCHttpClient(1);
public void Part1()
{
string input = _client.RetrieveFile();
// input = @"3 4
// 4 3
// 2 5
// 1 3
// 3 9
// 3 3";
string[] array = input.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
List<Model> models = new List<Model>();
foreach (string item in array)
{
string[] split = item.Split(" ");
models.Add(new Model()
{
Left = int.Parse(split[0]),
Right = int.Parse(split[1])
});
}
models.Sort((a, b) => a.Left.CompareTo(b.Left));
long sum = 0;
foreach (Model current in models)
{
Model min = models.Where(x => !x.RightSeen).MinBy(x=> x.Right);
min.RightSeen = true;
sum += Math.Abs(current.Left - min.Right);
}
Console.WriteLine(sum);
}
public void Part2()
{
string input = _client.RetrieveFile();
// input = @"3 4
// 4 3
// 2 5
// 1 3
// 3 9
// 3 3";
string[] array = input.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
List<Model> models = new List<Model>();
foreach (string item in array)
{
string[] split = item.Split(" ");
models.Add(new Model()
{
Left = int.Parse(split[0]),
Right = int.Parse(split[1])
});
}
models.Sort((a, b) => a.Left.CompareTo(b.Left));
long sum = 0;
foreach (Model current in models)
{
int totalCount = models.Where(x => !x.RightSeen).Count(x=> x.Right == current.Left);
sum += current.Left * totalCount;
}
Console.WriteLine(sum);
}
private record Model
{
public int Left;
public int Right;
public bool RightSeen;
}
}