Skip to content

Commit

Permalink
2024/22 off by one
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 25, 2024
1 parent cb211cd commit d1bd1cf
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions 2024/Day22/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace AdventOfCode.Y2024.Day22;
class Solution : Solver {

public object PartOne(string input) {
return GetNums(input).Select(x => (long)SecretNumbers(x).Last()).Sum();
return GetNums(input).Select(x => (long)SecretNumbers(x).Last()).Sum();
}

public object PartTwo(string input) {
Expand All @@ -17,7 +17,7 @@ public object PartTwo(string input) {
var buyingOptions = new Dictionary<string, int>();
foreach (var num in GetNums(input)) {
var optionsBySeller = BuyingOptions(num);
foreach(var seq in optionsBySeller.Keys){
foreach (var seq in optionsBySeller.Keys) {
buyingOptions[seq] = buyingOptions.GetValueOrDefault(seq) + optionsBySeller[seq];
}
}
Expand All @@ -32,24 +32,24 @@ Dictionary<string, int> BuyingOptions(int seed) {
// a sliding window of 5 elements over the sold bananas defines the sequence the monkey
// will recognize. add the first occurrence of each sequence to the buyOptions dictionary
// with the corresponding banana count
for (var i = 5;i < bananasSold.Length; i++) {
var slice = bananasSold[(i-5) .. i];
var seq = string.Join(",", Diff(slice));
for (var i = 0; i <= bananasSold.Length - 5; i++) {
var slice = bananasSold[i..(i + 5)];
var seq = string.Join(",", Diff(slice));
if (!buyOptions.ContainsKey(seq)) {
buyOptions[seq] = slice.Last();
}
}
return buyOptions;
}
int[] Bananas(int seed) => SecretNumbers(seed).Select(n => n % 10).ToArray();
int[] Bananas(int seed) => SecretNumbers(seed).Select(n => n % 10).ToArray();

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

IEnumerable<int> SecretNumbers(int seed) {
var mixAndPrune = (int a, long b) => (int)((a ^ b) % 16777216);

yield return seed;
for(var i = 0;i< 2000;i++) {
for (var i = 0; i < 2000; i++) {
seed = mixAndPrune(seed, seed * 64L);
seed = mixAndPrune(seed, seed / 32L);
seed = mixAndPrune(seed, seed * 2048L);
Expand Down

0 comments on commit d1bd1cf

Please sign in to comment.