Skip to content

Commit 947a5d9

Browse files
authored
Update Solution.cs
1 parent b9f8676 commit 947a5d9

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

2021/Day24/Solution.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,33 @@ class Solution : Solver {
1414

1515
var digits = Enumerable.Range(1, 9).ToArray();
1616

17-
var max = Enumerable.Repeat(int.MinValue, 14).ToArray();
18-
var min = Enumerable.Repeat(int.MaxValue, 14).ToArray();
19-
var stack = new Stack<int>();
20-
var stmBlocks = input.Split("inp w\n")[1..]; // the input has of 14 'blocks', reading one digit into w
17+
var stmBlocks = input.Split("inp w\n")[1..]; // The input has 14 code blocks, each dealing with one digit.
2118

22-
// Extracts the numeric argument of a statement of the block at the given line
19+
// Extracts the numeric argument of a statement:
2320
var getArgFromLine = (int iblock, Index iline) =>
2421
int.Parse(stmBlocks[iblock].Split('\n')[iline].Split(' ')[^1]);
2522

2623
// The blocks define 7 pairs of `a`, `b` digits and a `shift` between them.
2724
// The input is valid if for each pair the condition `a + shift = b` holds.
2825

26+
// A stack will contain the index of an `a` digit when we find it's corresponding `b`.
27+
var stack = new Stack<int>();
28+
29+
// We will fill up the result when `b` is found.
30+
var max = Enumerable.Repeat(int.MinValue, 14).ToArray();
31+
var min = Enumerable.Repeat(int.MaxValue, 14).ToArray();
32+
2933
for (var j = 0; j < 14; j++) {
3034
if (stmBlocks[j].Contains("div z 1")) {
3135
// j points to an `a` digit.
3236
stack.Push(j);
3337
} else {
3438
// j points to a `b` digit.
3539

36-
var i = stack.Pop(); // The stack points to the index of the corresponding `a`.
40+
// `a` is at i.
41+
var i = stack.Pop();
3742

38-
// Shift is split into two, both blocks contain a part:
43+
// A part of shift is hidden in each of the two blocks:
3944
var shift = getArgFromLine(i, ^4) + getArgFromLine(j, 4);
4045

4146
// Find the best a and b so that the equation holds

0 commit comments

Comments
 (0)