Skip to content

Commit a6fa88b

Browse files
committed
2025/03 iterative
1 parent 11dc06a commit a6fa88b

1 file changed

Lines changed: 9 additions & 18 deletions

File tree

2025/Day03/Solution.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace AdventOfCode.Y2025.Day03;
22

3-
using System;
43
using System.Linq;
54

65
[ProblemName("Lobby")]
@@ -12,24 +11,16 @@ class Solution : Solver {
1211
public long MaxJoltSum(string input, int batteryCount) =>
1312
input.Split("\n").Select(bank => MaxJolt(bank, batteryCount)).Sum();
1413

15-
// Determines the maximum possible jolt value by selecting `batteryCount`
16-
// digits in descending order from `bank` while preserving order.
1714
long MaxJolt(string bank, int batteryCount) {
18-
if (batteryCount == 0) {
19-
return 0;
15+
long res = 0L;
16+
for (; batteryCount > 0; batteryCount--) {
17+
// jump forward to the highest available digit in the bank, but keep
18+
// batteryCount digits in the suffix, so that we can select something
19+
// for those remaining batteries as well.
20+
char jolt = bank[..^(batteryCount - 1)].Max();
21+
bank = bank[(bank.IndexOf(jolt) + 1)..];
22+
res = 10 * res + (jolt - '0');
2023
}
21-
if (bank.Length < batteryCount) {
22-
return -1;
23-
}
24-
for (int jolt = 9; jolt > 0; jolt--) {
25-
var index = bank.IndexOf((char)(jolt + '0'));
26-
if (index >= 0) {
27-
var res = MaxJolt(bank[(index + 1)..], batteryCount - 1);
28-
if (res >= 0) {
29-
return jolt * (long)Math.Pow(10, batteryCount-1) + res;
30-
}
31-
}
32-
}
33-
throw new Exception();
24+
return res;
3425
}
3526
}

0 commit comments

Comments
 (0)