-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
70 lines (60 loc) · 1.87 KB
/
Program.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
using AdventOfCode.Common;
var lines = Resources.GetInputFileLines();
var rules = lines
.TakeWhile(l => l.Contains('|'))
.Select(line => line.SplitToNumbers("|"))
.Select(numbers => (First: numbers[0], Second: numbers[1]));
var updates = lines
.SkipWhile(l => l.Contains('|'))
.Select(line => line.SplitToNumbers());
var dependentOn = new Dictionary<int, HashSet<int>>();
foreach (var (former, latter) in rules)
{
if (!dependentOn.TryGetValue(latter, out HashSet<int>? deps))
{
deps = [];
dependentOn[latter] = deps;
}
deps.Add(former);
}
Console.WriteLine($"Part 1: {GetMiddlePage(false, dependentOn, updates)}");
Console.WriteLine($"Part 2: {GetMiddlePage(true, dependentOn, updates)}");
static int GetMiddlePage(bool takeIncorrectlyReordered, Dictionary<int, HashSet<int>> rules, IEnumerable<int[]> updates)
{
var result = 0;
foreach (var update in updates)
{
var isCorrect = true;
for (int i = 0; i < update.Length; i++)
{
Again:
var shouldBeEarlier = rules[update[i]];
for (int j = i + 1; j < update.Length; ++j)
{
if (shouldBeEarlier.Contains(update[j]))
{
isCorrect = false;
if (takeIncorrectlyReordered)
{
(update[i], update[j]) = (update[j], update[i]);
i = 0;
goto Again;
}
else
{
break;
}
}
}
if (!takeIncorrectlyReordered && !isCorrect)
{
break;
}
}
if (takeIncorrectlyReordered != isCorrect)
{
result += update[update.Length / 2];
}
}
return result;
}