@@ -5,8 +5,7 @@ namespace AdventOfCode.Y2024.Day06;
5
5
using System . Collections . Immutable ;
6
6
using System . Linq ;
7
7
using System . Numerics ;
8
-
9
- using Map = System . Collections . Generic . Dictionary < System . Numerics . Complex , char > ;
8
+ using Map = System . Collections . Immutable . ImmutableDictionary < System . Numerics . Complex , char > ;
10
9
11
10
[ ProblemName ( "Guard Gallivant" ) ]
12
11
class Solution : Solver {
@@ -21,17 +20,11 @@ public object PartOne(string input) {
21
20
22
21
public object PartTwo ( string input ) {
23
22
var ( map , start ) = Parse ( input ) ;
24
- var positions = Walk ( map , start ) . positions ;
25
- var loops = 0 ;
26
- // simply try a blocker in each locations visited by the guard and count the loops
27
- foreach ( var block in positions . Where ( pos => map [ pos ] == '.' ) ) {
28
- map [ block ] = '#' ;
29
- if ( Walk ( map , start ) . isLoop ) {
30
- loops ++ ;
31
- }
32
- map [ block ] = '.' ;
33
- }
34
- return loops ;
23
+ // try a blocker in each locations visited by the guard counting the loops
24
+ return Walk ( map , start ) . positions
25
+ . AsParallel ( )
26
+ . Count ( pos => Walk ( map . SetItem ( pos , '#' ) , start ) . isLoop ) ;
27
+
35
28
}
36
29
37
30
// returns the positions visited when starting from 'pos', isLoop is set if the
@@ -61,7 +54,7 @@ public object PartTwo(string input) {
61
54
from y in Enumerable . Range ( 0 , lines . Length )
62
55
from x in Enumerable . Range ( 0 , lines [ 0 ] . Length )
63
56
select new KeyValuePair < Complex , char > ( - Up * y + x , lines [ y ] [ x ] )
64
- ) . ToDictionary ( ) ;
57
+ ) . ToImmutableDictionary ( ) ;
65
58
66
59
var start = map . First ( x => x . Value == '^' ) . Key ;
67
60
0 commit comments