Skip to content

Commit 5ef7b21

Browse files
Update docs on Sat Dec 28 11:02:25 UTC 2024
1 parent 90c34d7 commit 5ef7b21

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

2024/22/index.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ <h2 id="problem-name">Monkey Market</h2>
302302
public object PartTwo(string input) {
303303
// create a dictionary of all buying options then select the one with the most banana:
304304

305-
var buyingOptions = new Dictionary&lt;string, int&gt;();
305+
var buyingOptions = new Dictionary&lt;(int,int,int,int), int&gt;();
306+
306307
foreach (var num in GetNums(input)) {
307308
var optionsBySeller = BuyingOptions(num);
308309
foreach (var seq in optionsBySeller.Keys) {
@@ -312,22 +313,21 @@ <h2 id="problem-name">Monkey Market</h2>
312313
return buyingOptions.Values.Max();
313314
}
314315

315-
Dictionary&lt;string, int&gt; BuyingOptions(int seed) {
316+
Dictionary&lt;(int,int,int,int), int&gt; BuyingOptions(int seed) {
316317
var bananasSold = Bananas(seed).ToArray();
317-
318-
var buyOptions = new Dictionary&lt;string, int&gt;();
318+
var buyingOptions = new Dictionary&lt;(int,int,int,int), int&gt;();
319319

320320
// 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
322322
// with the corresponding banana count
323-
for (var i = 0; i &lt;= bananasSold.Length - 5; i++) {
324-
var slice = bananasSold[i..(i + 5)];
325-
var seq = string.Join(&quot;,&quot;, Diff(slice));
326-
if (!buyOptions.ContainsKey(seq)) {
327-
buyOptions[seq] = slice.Last();
323+
var diff = Diff(bananasSold);
324+
for (var i = 0; i &lt; 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];
328328
}
329329
}
330-
return buyOptions;
330+
return buyingOptions;
331331
}
332332
int[] Bananas(int seed) =&gt; SecretNumbers(seed).Select(n =&gt; n % 10).ToArray();
333333

2024/6/index.html

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ <h2 id="problem-name">Guard Gallivant</h2>
294294
using System.Collections.Immutable;
295295
using System.Linq;
296296
using System.Numerics;
297-
298-
using Map = System.Collections.Generic.Dictionary&lt;System.Numerics.Complex, char&gt;;
297+
using Map = System.Collections.Immutable.ImmutableDictionary&lt;System.Numerics.Complex, char&gt;;
299298

300299
[ProblemName(&quot;Guard Gallivant&quot;)]
301300
class Solution : Solver {
@@ -310,17 +309,11 @@ <h2 id="problem-name">Guard Gallivant</h2>
310309

311310
public object PartTwo(string input) {
312311
var (map, start) = Parse(input);
313-
var positions = Walk(map, start).positions;
314-
var loops = 0;
315-
// simply try a blocker in each locations visited by the guard and count the loops
316-
foreach (var block in positions.Where(pos =&gt; map[pos] == &#039;.&#039;)) {
317-
map[block] = &#039;#&#039;;
318-
if (Walk(map, start).isLoop) {
319-
loops++;
320-
}
321-
map[block] = &#039;.&#039;;
322-
}
323-
return loops;
312+
// try a blocker in each locations visited by the guard counting the loops
313+
return Walk(map, start).positions
314+
.AsParallel()
315+
.Count(pos =&gt; Walk(map.SetItem(pos, &#039;#&#039;), start).isLoop);
316+
324317
}
325318

326319
// returns the positions visited when starting from &#039;pos&#039;, isLoop is set if the
@@ -350,7 +343,7 @@ <h2 id="problem-name">Guard Gallivant</h2>
350343
from y in Enumerable.Range(0, lines.Length)
351344
from x in Enumerable.Range(0, lines[0].Length)
352345
select new KeyValuePair&lt;Complex, char&gt;(-Up * y + x, lines[y][x])
353-
).ToDictionary();
346+
).ToImmutableDictionary();
354347

355348
var start = map.First(x =&gt; x.Value == &#039;^&#039;).Key;
356349

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy