@@ -8,7 +8,7 @@ namespace AdventOfCode.Y2024.Day22;
8
8
class Solution : Solver {
9
9
10
10
public object PartOne ( string input ) {
11
- return GetNums ( input ) . Select ( x => ( long ) SecretNumbers ( x ) . Last ( ) ) . Sum ( ) ;
11
+ return GetNums ( input ) . Select ( x => ( long ) SecretNumbers ( x ) . Last ( ) ) . Sum ( ) ;
12
12
}
13
13
14
14
public object PartTwo ( string input ) {
@@ -17,7 +17,7 @@ public object PartTwo(string input) {
17
17
var buyingOptions = new Dictionary < string , int > ( ) ;
18
18
foreach ( var num in GetNums ( input ) ) {
19
19
var optionsBySeller = BuyingOptions ( num ) ;
20
- foreach ( var seq in optionsBySeller . Keys ) {
20
+ foreach ( var seq in optionsBySeller . Keys ) {
21
21
buyingOptions [ seq ] = buyingOptions . GetValueOrDefault ( seq ) + optionsBySeller [ seq ] ;
22
22
}
23
23
}
@@ -32,24 +32,24 @@ Dictionary<string, int> BuyingOptions(int seed) {
32
32
// a sliding window of 5 elements over the sold bananas defines the sequence the monkey
33
33
// will recognize. add the first occurrence of each sequence to the buyOptions dictionary
34
34
// with the corresponding banana count
35
- for ( var i = 5 ; i < bananasSold . Length ; i ++ ) {
36
- var slice = bananasSold [ ( i - 5 ) .. i ] ;
37
- var seq = string . Join ( "," , Diff ( slice ) ) ;
35
+ for ( var i = 0 ; i <= bananasSold . Length - 5 ; i ++ ) {
36
+ var slice = bananasSold [ i .. ( i + 5 ) ] ;
37
+ var seq = string . Join ( "," , Diff ( slice ) ) ;
38
38
if ( ! buyOptions . ContainsKey ( seq ) ) {
39
39
buyOptions [ seq ] = slice . Last ( ) ;
40
40
}
41
41
}
42
42
return buyOptions ;
43
43
}
44
- int [ ] Bananas ( int seed ) => SecretNumbers ( seed ) . Select ( n => n % 10 ) . ToArray ( ) ;
44
+ int [ ] Bananas ( int seed ) => SecretNumbers ( seed ) . Select ( n => n % 10 ) . ToArray ( ) ;
45
45
46
46
int [ ] Diff ( IEnumerable < int > x ) => x . Zip ( x . Skip ( 1 ) ) . Select ( p => p . Second - p . First ) . ToArray ( ) ;
47
47
48
48
IEnumerable < int > SecretNumbers ( int seed ) {
49
49
var mixAndPrune = ( int a , long b ) => ( int ) ( ( a ^ b ) % 16777216 ) ;
50
-
50
+
51
51
yield return seed ;
52
- for ( var i = 0 ; i < 2000 ; i ++ ) {
52
+ for ( var i = 0 ; i < 2000 ; i ++ ) {
53
53
seed = mixAndPrune ( seed , seed * 64L ) ;
54
54
seed = mixAndPrune ( seed , seed / 32L ) ;
55
55
seed = mixAndPrune ( seed , seed * 2048L ) ;
0 commit comments