Skip to content

Commit d1bd1cf

Browse files
committed
2024/22 off by one
1 parent cb211cd commit d1bd1cf

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

2024/Day22/Solution.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace AdventOfCode.Y2024.Day22;
88
class Solution : Solver {
99

1010
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();
1212
}
1313

1414
public object PartTwo(string input) {
@@ -17,7 +17,7 @@ public object PartTwo(string input) {
1717
var buyingOptions = new Dictionary<string, int>();
1818
foreach (var num in GetNums(input)) {
1919
var optionsBySeller = BuyingOptions(num);
20-
foreach(var seq in optionsBySeller.Keys){
20+
foreach (var seq in optionsBySeller.Keys) {
2121
buyingOptions[seq] = buyingOptions.GetValueOrDefault(seq) + optionsBySeller[seq];
2222
}
2323
}
@@ -32,24 +32,24 @@ Dictionary<string, int> BuyingOptions(int seed) {
3232
// a sliding window of 5 elements over the sold bananas defines the sequence the monkey
3333
// will recognize. add the first occurrence of each sequence to the buyOptions dictionary
3434
// 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));
3838
if (!buyOptions.ContainsKey(seq)) {
3939
buyOptions[seq] = slice.Last();
4040
}
4141
}
4242
return buyOptions;
4343
}
44-
int[] Bananas(int seed) => SecretNumbers(seed).Select(n => n % 10).ToArray();
44+
int[] Bananas(int seed) => SecretNumbers(seed).Select(n => n % 10).ToArray();
4545

4646
int[] Diff(IEnumerable<int> x) => x.Zip(x.Skip(1)).Select(p => p.Second - p.First).ToArray();
4747

4848
IEnumerable<int> SecretNumbers(int seed) {
4949
var mixAndPrune = (int a, long b) => (int)((a ^ b) % 16777216);
50-
50+
5151
yield return seed;
52-
for(var i = 0;i< 2000;i++) {
52+
for (var i = 0; i < 2000; i++) {
5353
seed = mixAndPrune(seed, seed * 64L);
5454
seed = mixAndPrune(seed, seed / 32L);
5555
seed = mixAndPrune(seed, seed * 2048L);

0 commit comments

Comments
 (0)