diff --git a/2024/22/index.html b/2024/22/index.html index b22c2700..fdcb293f 100644 --- a/2024/22/index.html +++ b/2024/22/index.html @@ -296,7 +296,7 @@

Monkey Market

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) { @@ -305,7 +305,7 @@

Monkey Market

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]; } } @@ -320,24 +320,24 @@

Monkey Market

// 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);