@@ -302,7 +302,8 @@ <h2 id="problem-name">Monkey Market</h2>
302
302
public object PartTwo(string input) {
303
303
// create a dictionary of all buying options then select the one with the most banana:
304
304
305
- var buyingOptions = new Dictionary<string, int>();
305
+ var buyingOptions = new Dictionary<(int,int,int,int), int>();
306
+
306
307
foreach (var num in GetNums(input)) {
307
308
var optionsBySeller = BuyingOptions(num);
308
309
foreach (var seq in optionsBySeller.Keys) {
@@ -312,22 +313,21 @@ <h2 id="problem-name">Monkey Market</h2>
312
313
return buyingOptions.Values.Max();
313
314
}
314
315
315
- Dictionary<string , int> BuyingOptions(int seed) {
316
+ Dictionary<(int,int,int,int) , int> BuyingOptions(int seed) {
316
317
var bananasSold = Bananas(seed).ToArray();
317
-
318
- var buyOptions = new Dictionary<string, int>();
318
+ var buyingOptions = new Dictionary<(int,int,int,int), int>();
319
319
320
320
// a sliding window of 5 elements over the sold bananas defines the sequence the monkey
321
- // will recognize. add the first occurrence of each sequence to the buyOptions dictionary
321
+ // will recognize. add the first occurrence of each sequence to the buyingOptions dictionary
322
322
// with the corresponding banana count
323
- for ( var i = 0; i <= bananasSold.Length - 5; i++) {
324
- var slice = bananasSold[i..(i + 5)];
325
- var seq = string.Join(",", Diff(slice) );
326
- if (!buyOptions .ContainsKey(seq)) {
327
- buyOptions [seq] = slice.Last() ;
323
+ var diff = Diff( bananasSold);
324
+ for (var i = 0; i < bananasSold.Length - 4; i++) {
325
+ var seq = (diff[i], diff[i+1], diff[i+2], diff[i+3] );
326
+ if (!buyingOptions .ContainsKey(seq)) {
327
+ buyingOptions [seq] = bananasSold[i+4] ;
328
328
}
329
329
}
330
- return buyOptions ;
330
+ return buyingOptions ;
331
331
}
332
332
int[] Bananas(int seed) => SecretNumbers(seed).Select(n => n % 10).ToArray();
333
333
0 commit comments