Skip to content

Commit 13951be

Browse files
committed
wip
1 parent 8a82679 commit 13951be

File tree

4 files changed

+100
-72
lines changed

4 files changed

+100
-72
lines changed

2024/Day21/Solution.cs

+36-21
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,64 @@ namespace AdventOfCode.Y2024.Day21;
55
using System.Linq;
66
using System.Numerics;
77
using AngleSharp.Common;
8-
using Cache = System.Collections.Concurrent.ConcurrentDictionary<(char, System.Numerics.Complex, int, System.Numerics.Complex), (long, System.Numerics.Complex)>;
8+
using Cache = System.Collections.Concurrent.ConcurrentDictionary<(char, System.Numerics.Complex, int, System.Numerics.Complex), (long, string, System.Numerics.Complex)>;
99

1010

1111
[ProblemName("Keypad Conundrum")]
1212
class Solution : Solver {
1313

1414
public object PartOne(string input) {
15-
return input.Split("\n").Sum(line => Solve2(line, 2));
15+
return input.Split("\n").Sum(line => Solve2(line, 2).Item1);
1616
}
1717
public object PartTwo(string input) {
18-
// 24: 156944425344350 low
19-
// 25: 400827838993176 high
20-
return input.Split("\n").Sum(line => Solve2(line, 24));
18+
return input.Split("\n").Sum(line => Solve2(line, 25).Item1);
2119
}
2220

2321
static readonly Complex Left = -1;
2422
static readonly Complex Right = 1;
2523
static readonly Complex Up = Complex.ImaginaryOne;
2624
static readonly Complex Down = -Complex.ImaginaryOne;
2725

28-
long Solve2(string line, int depth) {
26+
(long, string) Solve2(string line, int depth) {
2927
var keypad1 = ParseKeypad("789\n456\n123\n 0A");
3028
var keypad2 = ParseKeypad(" ^A\n<v>");
3129

3230
Cache cache = new Cache();
3331
var res = long.MaxValue;
32+
var st = "";
3433
foreach (var plan in Encode(line, keypad1, keypad1['A'])) {
35-
var (length, _) = EncodeString(plan, keypad2, depth, cache, keypad2['A']);
36-
res = Math.Min(res, length);
34+
var (length, stT, _) = EncodeString(plan, keypad2, depth, cache, keypad2['A']);
35+
if (length < res) {
36+
res = Math.Min(res, length);
37+
st = stT;
38+
}
3739
}
38-
return res * int.Parse(line.Substring(0, line.Length - 1));
40+
return (res * int.Parse(line.Substring(0, line.Length - 1)), st);
3941
}
4042

41-
(long, Complex) EncodeString(string st, Dictionary<char, Complex> keypad2, int depth, Cache cache, Complex top) {
43+
(long, string, Complex) EncodeString(string st, Dictionary<char, Complex> keypad2, int depth, Cache cache, Complex top) {
4244
if (depth == 0) {
43-
return (st.Length, top);
45+
return (st.Length, st, top);
4446
} else {
4547
var length = 0L;
46-
var pos = depth == 1 ? top : keypad2['A'];
47-
var originalTop = top;
48+
var pos = keypad2['A']; ; //depth == 1 ? top : keypad2['A'];
49+
var originalTop = top;
50+
var resSt = "";
4851
foreach (var step in st) {
4952
long cost;
50-
(cost, top) = EncodeKey(step, pos, keypad2, depth, cache, top);
53+
string stT;
54+
(cost, stT, top) = EncodeKey(step, pos, keypad2, depth, cache, top);
5155
length += cost;
56+
resSt += stT;
5257
pos = keypad2[step];
5358
}
5459
if (depth == 1) {
55-
top = st.Length == 1 ? originalTop : keypad2[st[^1]];
60+
top = st.Length == 1 ? originalTop : keypad2[st[^1]];
5661
}
57-
return (length, top);
62+
return (length, resSt, top);
5863
}
5964
}
60-
(long, Complex) EncodeKey(char ch, Complex pos, Dictionary<char, Complex> keypad2, int depth, Cache cache, Complex top) {
65+
(long, string, Complex) EncodeKey(char ch, Complex pos, Dictionary<char, Complex> keypad2, int depth, Cache cache, Complex top) {
6166
var key = (ch, pos, depth, top);
6267
if (cache.ContainsKey(key)) {
6368
return cache[key];
@@ -75,8 +80,12 @@ long Solve2(string line, int depth) {
7580

7681
var resCost = long.MaxValue;
7782
var resTop = Complex.Infinity;
83+
var resSt = "";
84+
85+
string toEncode = "";
86+
7887

79-
var toEncode = "";
88+
toEncode = "";
8089
if (pos + dy * Up != keypad2[' ']) {
8190
if (dy < 0) {
8291
toEncode += new string('v', Math.Abs(dy));
@@ -89,14 +98,18 @@ long Solve2(string line, int depth) {
8998
toEncode += new string('>', Math.Abs(dx));
9099
}
91100
toEncode += "A";
92-
var (cost, topT) = EncodeString(toEncode, keypad2, depth - 1, cache, top);
101+
var (cost, stT, topT) = EncodeString(toEncode, keypad2, depth - 1, cache, top);
93102

94103
if (cost < resCost) {
95104
resCost = cost;
96105
resTop = topT;
106+
resSt = stT;
97107
}
98108
}
99109

110+
111+
112+
toEncode = "";
100113
if (pos + dx * Right != keypad2[' ']) {
101114
if (dx < 0) {
102115
toEncode += new string('<', Math.Abs(dx));
@@ -111,15 +124,17 @@ long Solve2(string line, int depth) {
111124
}
112125
toEncode += "A";
113126

114-
var (cost, topT) = EncodeString(toEncode, keypad2, depth - 1, cache, top);
127+
var (cost, stT, topT) = EncodeString(toEncode, keypad2, depth - 1, cache, top);
115128

116129
if (cost < resCost) {
117130
resCost = cost;
118131
resTop = topT;
132+
resSt = stT;
119133
}
120134
}
135+
resSt = "";
121136

122-
cache[key] = (resCost, resTop);
137+
cache[key] = (resCost, resSt, resTop);
123138
return cache[key];
124139
}
125140

2024/Day21/input.refout

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
176650
1+
176650
2+
217698355426872

2024/SplashScreen.cs

+48-36
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public void Show() {
88

99
var color = Console.ForegroundColor;
1010
Write(0xcc00, false, " ▄█▄ ▄▄█ ▄ ▄ ▄▄▄ ▄▄ ▄█▄ ▄▄▄ ▄█ ▄▄ ▄▄▄ ▄▄█ ▄▄▄\n █▄█ █ █ █ █ █▄█ █ █ █ █ █ █▄ ");
11-
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ /^2024$/\n \n ");
12-
Write(0xcc00, false, " ");
11+
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ $year = 2024\n ");
12+
Write(0xcc00, false, "\n ");
1313
Write(0x888888, false, " .-----. .------------------. \n ");
1414
Write(0xcccccc, false, ".--'");
1515
Write(0xe3b585, false, "~ ~ ~");
@@ -49,9 +49,9 @@ public void Show() {
4949
Write(0xcccccc, false, "| 3 ");
5050
Write(0xffff66, false, "**\n ");
5151
Write(0xcccccc, false, "|");
52-
Write(0x488813, false, "@");
52+
Write(0x1461f, false, "@");
5353
Write(0x5eabb4, false, "..");
54-
Write(0x4d8b03, false, "@");
54+
Write(0x427322, false, "@");
5555
Write(0xe3b585, false, "'. ~ ");
5656
Write(0xcc00, false, "\" ' ");
5757
Write(0xe3b585, false, "~ ");
@@ -68,10 +68,9 @@ public void Show() {
6868
Write(0xcccccc, false, "| 4 ");
6969
Write(0xffff66, false, "**\n ");
7070
Write(0xcccccc, false, "|");
71-
Write(0x7fbd39, false, "_");
71+
Write(0x4d8b03, false, "_");
7272
Write(0x5eabb4, false, ".~.");
73-
Write(0x427322, false, "_");
74-
Write(0x7fbd39, false, "#");
73+
Write(0x4d8b03, false, "_@");
7574
Write(0xe3b585, false, "'.. ~ ~ ");
7675
Write(0xffff66, true, "*");
7776
Write(0xcccccc, false, "| | ");
@@ -84,10 +83,10 @@ public void Show() {
8483
Write(0xffff66, false, "**\n ");
8584
Write(0xcccccc, false, "| ");
8685
Write(0xffffff, false, "||| ");
87-
Write(0x488813, false, "#");
86+
Write(0x488813, false, "@");
8887
Write(0x427322, false, "@");
88+
Write(0x4d8b03, false, "@");
8989
Write(0x7fbd39, false, "@");
90-
Write(0x1461f, false, "#");
9190
Write(0xe3b585, false, "'''...");
9291
Write(0xcccccc, false, "| |");
9392
Write(0xa25151, false, "... ");
@@ -98,13 +97,12 @@ public void Show() {
9897
Write(0xcccccc, false, "| 6 ");
9998
Write(0xffff66, false, "**\n ");
10099
Write(0xcccccc, false, "|");
101-
Write(0x488813, false, "@");
100+
Write(0x427322, false, "@");
102101
Write(0xffffff, false, "~~~");
103102
Write(0x427322, false, "#");
104-
Write(0x7fbd39, false, "@@#");
105-
Write(0x488813, false, "@ ");
106-
Write(0x7fbd39, false, "@@");
107-
Write(0x488813, false, "# ");
103+
Write(0x4d8b03, false, "@@ ");
104+
Write(0x7fbd39, false, "#");
105+
Write(0x427322, false, "# ");
108106
Write(0xcccccc, false, "| |");
109107
Write(0xa5a8af, false, "/\\ ");
110108
Write(0xa25151, false, "''. ");
@@ -159,10 +157,9 @@ public void Show() {
159157
Write(0xcccccc, false, "| |");
160158
Write(0xa5a8af, false, "/\\ ");
161159
Write(0xa25151, false, "..' ");
162-
Write(0xcccccc, false, "| | ");
163-
Write(0xb5ed, false, "' ");
164-
Write(0xffffff, false, ". ");
165-
Write(0xb5ed, false, "'");
160+
Write(0xcccccc, false, "| | ");
161+
Write(0xffffff, false, ". ");
162+
Write(0xb5ed, false, ". ");
166163
Write(0xcccccc, false, "| 11 ");
167164
Write(0xffff66, false, "**\n ");
168165
Write(0xcccccc, false, "| ");
@@ -173,13 +170,14 @@ public void Show() {
173170
Write(0xffff66, true, ":");
174171
Write(0x333333, false, "::");
175172
Write(0xcccccc, false, "| | ");
176-
Write(0xffffff, false, ". ");
173+
Write(0xffffff, false, ". ");
174+
Write(0xa2db, false, ". .");
177175
Write(0xcccccc, false, "| 12 ");
178176
Write(0xffff66, false, "**\n ");
179177
Write(0xcccccc, false, "|");
180178
Write(0xffffff, false, "'. - -");
181-
Write(0xcccccc, false, "| | ");
182-
Write(0x333333, false, "::");
179+
Write(0xcccccc, false, "| |");
180+
Write(0x333333, false, ". ::");
183181
Write(0x9900, true, ":");
184182
Write(0x333333, false, "::");
185183
Write(0xcccccc, false, "| | ");
@@ -206,7 +204,8 @@ public void Show() {
206204
Write(0xcccccc, false, "|");
207205
Write(0xcc00, false, ". ''. ");
208206
Write(0xcccccc, false, "| |");
209-
Write(0x666666, false, ". ");
207+
Write(0x666666, false, ".");
208+
Write(0x333333, false, ". ");
210209
Write(0x9900, true, ":::::");
211210
Write(0xcccccc, false, "| |");
212211
Write(0x666666, false, "──┬┴┴┴┬─");
@@ -231,19 +230,19 @@ public void Show() {
231230
Write(0xcc00, false, "'.");
232231
Write(0x5555bb, false, "~ ");
233232
Write(0xcc00, false, ":");
234-
Write(0xcccccc, false, "| |");
235-
Write(0x666666, false, " '. ");
233+
Write(0xcccccc, false, "| | ");
234+
Write(0x333333, false, ".");
235+
Write(0x666666, false, "'. ");
236+
Write(0x333333, false, ". ");
236237
Write(0xcccccc, false, "| |");
237238
Write(0x666666, false, "┬o┤ten├─");
238239
Write(0xcccccc, false, "| 17 ");
239240
Write(0xffff66, false, "**\n ");
240241
Write(0xcccccc, false, "| ");
241242
Write(0xcc00, false, "'..' .'");
242-
Write(0xcccccc, false, "| | ");
243-
Write(0x333333, false, ".");
244-
Write(0x666666, false, " '");
245-
Write(0x456efe, true, "o ");
246-
Write(0x333333, false, ".");
243+
Write(0xcccccc, false, "| |");
244+
Write(0x666666, false, " '");
245+
Write(0x456efe, true, "o ");
247246
Write(0xcccccc, false, "| |");
248247
Write(0x666666, false, "┘");
249248
Write(0xffff66, true, "*");
@@ -254,9 +253,7 @@ public void Show() {
254253
Write(0x5555bb, false, "~ ");
255254
Write(0xcc00, false, "..' ");
256255
Write(0xcccccc, false, "| |");
257-
Write(0x666666, false, ": ");
258-
Write(0x333333, false, ".");
259-
Write(0x666666, false, "'. ");
256+
Write(0x666666, false, ": '. ");
260257
Write(0xcccccc, false, "| |");
261258
Write(0x666666, false, "─┘├┬┬┬┴─");
262259
Write(0xcccccc, false, "| 19 ");
@@ -274,10 +271,25 @@ public void Show() {
274271
Write(0x9900, false, "<<");
275272
Write(0xcccccc, false, "| 20 ");
276273
Write(0xffff66, false, "**\n ");
277-
Write(0x666666, false, ".------'.-((---.'------. | :|\\| ~ _'' O> >>@<o<| ");
278-
Write(0xcccccc, false, "21 ");
279-
Write(0xffff66, false, "*");
280-
Write(0x666666, false, "*\n ");
274+
Write(0xcccccc, false, ".------'");
275+
Write(0x66ff, false, ".-");
276+
Write(0xcccccc, false, "((");
277+
Write(0x66ff, false, "---.");
278+
Write(0xcccccc, false, "'------. |");
279+
Write(0x666666, false, " :");
280+
Write(0xff0000, false, "|");
281+
Write(0xcccccc, false, "\\| ");
282+
Write(0x333399, false, "~ ");
283+
Write(0x9900ff, false, "_");
284+
Write(0xcccccc, false, "'' ");
285+
Write(0x9900ff, false, "O> ");
286+
Write(0x9900, false, ">>");
287+
Write(0xff0000, true, "@");
288+
Write(0x9900, false, "<");
289+
Write(0xff9900, true, "o");
290+
Write(0x9900, false, "<");
291+
Write(0xcccccc, false, "| 21 ");
292+
Write(0xffff66, false, "**\n ");
281293
Write(0x333333, false, "| | | | ");
282294
Write(0x666666, false, "22\n 23\n ");
283295
Write(0x666666, false, " 24\n 25\n ");

0 commit comments

Comments
 (0)