Skip to content

Commit 1e6bd5f

Browse files
committed
day 24
1 parent 3e5f9df commit 1e6bd5f

File tree

3 files changed

+40
-319
lines changed

3 files changed

+40
-319
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"preLaunchTask": "build",
1313
// If you have changed target frameworks, make sure to update the program path.
1414
"program": "${workspaceFolder}/bin/Debug/net6.0/adventofcode.dll",
15-
"args": ["2021/23"],
15+
"args": ["2021"],
1616
"cwd": "${workspaceFolder}",
1717
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
1818
"console": "internalConsole",

2021/Day24/Solution.cs

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
1-
using System.Numerics;
1+
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
24

35
namespace AdventOfCode.Y2021.Day24;
46

57
[ProblemName("Arithmetic Logic Unit")]
68
class Solution : Solver {
79

8-
public object PartOne(string input) {
9-
return "96979989692495";
10-
}
11-
12-
public object PartTwo(string input) {
13-
return "51316214181141";
14-
}
15-
16-
BigInteger Run2(string input, BigInteger z, string[] lines) {
17-
int from = 0;
18-
var ich = 0;
19-
BigInteger step(int iblock, BigInteger z, BigInteger S, BigInteger T, BigInteger U) {
20-
if (z < 0) {
21-
return -1;
22-
}
23-
if (iblock < from || ich >= input.Length) {
24-
return z;
10+
public object PartOne(string input) => Solve(input).max;
11+
public object PartTwo(string input) => Solve(input).min;
12+
13+
(string min, string max) Solve(string input) {
14+
15+
var max = Enumerable.Repeat(int.MinValue, 14).ToArray();
16+
var min = Enumerable.Repeat(int.MaxValue, 14).ToArray();
17+
var d = new int[14];
18+
var digits = Enumerable.Range(1, 9).ToArray();
19+
var stack = new Stack<int>();
20+
var blocks = input.Split("inp w\n").Skip(1).ToArray();
21+
22+
for (var i = 0; i < 14; i++) {
23+
var block = blocks[i];
24+
var lines = block.Split('\n');
25+
26+
if (block.Contains("div z 1")) {
27+
d[i] = int.Parse(lines[^4].Split(' ').Last());
28+
29+
stack.Push(i);
30+
} else {
31+
d[i] = int.Parse(lines[4].Split(' ').Last());
32+
33+
var pair = stack.Pop();
34+
foreach (var digit in digits) {
35+
var digitPair = digit - d[pair] - d[i];
36+
if (digits.Contains(digitPair)) {
37+
if (digitPair > max[pair]) {
38+
(max[pair], max[i]) = (digitPair, digit);
39+
}
40+
if (digitPair < min[pair]) {
41+
(min[pair], min[i]) = (digitPair, digit);
42+
}
43+
}
44+
}
2545
}
26-
var w = input[ich++] - '0';
27-
28-
var x = (z % 26 + T) != w ? 1 : 0;
29-
z = z / S;
30-
return z * (25 * x + 1) + (w + U) * x;
3146
}
3247

33-
var zOrig = z;
34-
z = step(0, z, 1, 12, 1); // 9 ------------\ 5
35-
z = step(1, z, 1, 13, 9); // 6 -------\ | 1
36-
// | |
37-
z = step(2, z, 1, 12, 11); // 9 | | 3
38-
z = step(3, z, 26, -13, 6); // 7 | | 1
39-
// | |
40-
z = step(4, z, 1, 11, 6); // 9 ----\ | | 6
41-
// | | |
42-
z = step(5, z, 1, 15, 13); // 9 | | | 2
43-
z = step(6, z, 26, -14, 13); // 8 | | | 1
44-
// | | |
45-
z = step(7, z, 1, 12, 5); // 9 | | | 4
46-
z = step(8, z, 26, -8, 7); // 6 | | | 1
47-
// | | |
48-
z = step(9, z, 1, 14, 2); // 9 | | | 8
49-
z = step(10, z, 26, -9, 10); // 2 | | | 1
50-
// | | |
51-
z = step(11, z, 26, -11, 14); // 4 <----/ | | 1
52-
z = step(12, z, 26, -6, 7); // 9 <--------/ | 4
53-
z = step(13, z, 26, -5, 1); // 5 <-------------/ 1
54-
55-
return z;
48+
return (string.Join("", min), string.Join("", max));
5649
}
5750

5851
}

2021/Day24/inp w.hsig

Lines changed: 0 additions & 272 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy