File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11namespace AdventOfCode . Y2025 . Day03 ;
22
3- using System ;
43using 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}
You can’t perform that action at this time.
0 commit comments