4
4
char [ , ] map = Resources . GetInputFileLines ( ) . ParseAsArray ( ) ;
5
5
Coor start = map . AllCoordinates ( ) . First ( c => map . Get ( c ) == '^' ) ;
6
6
7
- var directions = new Dictionary < char , Coor >
8
- {
9
- { '^' , Coor . Up } ,
10
- { '>' , Coor . Right } ,
11
- { 'V' , Coor . Down } ,
12
- { '<' , Coor . Left } ,
13
- } ;
14
-
15
- Simulate ( map ) ;
7
+ Simulate ( map , start ) ;
16
8
var traversedPath = map . AllCoordinates ( )
17
9
. Where ( c => map . Get ( c ) != '.' && map . Get ( c ) != '#' )
18
10
. ToList ( ) ;
24
16
{
25
17
var newMap = Resources . GetInputFileLines ( ) . ParseAsArray ( ) ;
26
18
newMap . Set ( path , '#' ) ;
27
- Simulate ( newMap ) ;
19
+ Simulate ( newMap , start ) ;
28
20
}
29
21
catch ( LoopException )
30
22
{
35
27
Console . WriteLine ( $ "Part 1: { traversedPath . Count } ") ;
36
28
Console . WriteLine ( $ "Part 2: { possibleObstaclePositions } ") ;
37
29
38
- static void Simulate ( char [ , ] map )
30
+ static void Simulate ( char [ , ] map , Coor position )
39
31
{
40
- var position = map . AllCoordinates ( ) . First ( c => map . Get ( c ) == '^' ) ;
41
32
var direction = Coor . Up ;
42
- map . Set ( position , ( char ) 0 ) ;
43
33
44
34
while ( true )
45
35
{
46
36
var current = map . Get ( position ) ;
47
- map . Set ( position , ( char ) ( current switch
37
+ map . Set ( position , current switch
48
38
{
49
- '.' => 1 ,
50
- _ => current + 1 ,
51
- } ) ) ;
39
+ '.' => '0' ,
40
+ _ => ( char ) ( current + 1 ) ,
41
+ } ) ;
52
42
53
43
var next = position + direction ;
54
44
if ( ! next . InBoundsOf ( map ) )
@@ -64,7 +54,7 @@ static void Simulate(char[,] map)
64
54
65
55
// We can only come to a field twice (from side and from top/down)
66
56
// If we come a third time, it's a loop
67
- if ( map . Get ( next ) != '.' && map . Get ( next ) > 3 )
57
+ if ( map . Get ( next ) != '.' && map . Get ( next ) == '3' )
68
58
{
69
59
throw new LoopException ( ) ;
70
60
}
0 commit comments