From d1bd1cf47f280cbefc87d9e5303693c9b261c7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Da=CC=81vid=20Ne=CC=81meth=20Cs?= Date: Wed, 25 Dec 2024 07:14:05 +0100 Subject: [PATCH] 2024/22 off by one --- 2024/Day22/Solution.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/2024/Day22/Solution.cs b/2024/Day22/Solution.cs index 889eda8f..84e6960d 100644 --- a/2024/Day22/Solution.cs +++ b/2024/Day22/Solution.cs @@ -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) { @@ -17,7 +17,7 @@ public object PartTwo(string input) { var buyingOptions = new Dictionary(); 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]; } } @@ -32,24 +32,24 @@ Dictionary 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 x) => x.Zip(x.Skip(1)).Select(p => p.Second - p.First).ToArray(); IEnumerable 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);