@@ -278,7 +278,6 @@ <h2 id="problem-name">Lobby</h2>
278278</ div >
279279 < div id ="code-container "> < pre class ="hljs language-csharp "> < code > namespace AdventOfCode.Y2025.Day03;
280280
281- using System;
282281using System.Linq;
283282
284283[ProblemName("Lobby")]
@@ -290,25 +289,17 @@ <h2 id="problem-name">Lobby</h2>
290289 public long MaxJoltSum(string input, int batteryCount) =>
291290 input.Split("\n").Select(bank => MaxJolt(bank, batteryCount)).Sum();
292291
293- // Determines the maximum possible jolt value by selecting `batteryCount`
294- // digits in descending order from `bank` while preserving order.
295292 long MaxJolt(string bank, int batteryCount) {
296- if (batteryCount == 0) {
297- return 0;
298- }
299- if (bank.Length < batteryCount) {
300- return -1;
301- }
302- for (int jolt = 9; jolt > 0; jolt--) {
303- var index = bank.IndexOf((char)(jolt + '0'));
304- if (index >= 0) {
305- var res = MaxJolt(bank[(index + 1)..], batteryCount - 1);
306- if (res >= 0) {
307- return jolt * (long)Math.Pow(10, batteryCount-1) + res;
308- }
309- }
310- }
311- throw new Exception();
293+ long res = 0L;
294+ for (; batteryCount > 0; batteryCount--) {
295+ // jump forward to the highest available digit in the bank, but keep
296+ // batteryCount digits in the suffix, so that we can select something
297+ // for those remaining batteries as well.
298+ char jolt = bank[..^(batteryCount - 1)].Max();
299+ bank = bank[(bank.IndexOf(jolt) + 1)..];
300+ res = 10 * res + (jolt - '0');
301+ }
302+ return res;
312303 }
313304}</ code > </ pre > </ div >
314305 < div id ="code-location "> < p > Please ☆ my < a href ="https://github.com/encse/adventofcode/ "> repo</ a > if you like it!</ p > </ div >
0 commit comments