-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.cs
53 lines (43 loc) · 1.67 KB
/
Day5.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
namespace Aoc2024Net.Days
{
internal sealed class Day5 : Day
{
public override object? SolvePart1()
{
var (rules, updates) = GetRulesAndUpdates();
return updates.Where(u => IsCorrect(u, rules)).Sum(u => u[u.Length / 2]);
}
public override object? SolvePart2()
{
var (rules, updates) = GetRulesAndUpdates();
var incorrectUpdates = updates.Where(u => !IsCorrect(u, rules)).ToArray();
var result = 0;
foreach (var update in incorrectUpdates)
{
while (!IsCorrect(update, rules))
{
for (var i = 0; i < update.Length; i++)
{
var j = Enumerable
.Range(0, i)
.FirstOrDefault(k => rules.Contains($"{update[i]}|{update[k]}"), -1);
if (j >= 0)
(update[j], update[i]) = (update[i], update[j]);
}
}
result += update[update.Length / 2];
}
return result;
}
private (string[] Rules, int[][] Updates) GetRulesAndUpdates()
{
var groups = InputData.GetInputLinesGroups();
var rules = groups[0];
var updates = groups[1].Select(l => l.Split(',').Select(int.Parse).ToArray()).ToArray();
return (rules, updates);
}
private static bool IsCorrect(int[] update, string[] rules) => Enumerable
.Range(0, update.Length)
.All(i => !update[..i].Any(n => rules.Contains($"{update[i]}|{n}")));
}
}