@@ -15,12 +15,12 @@ public object PartOne(string input)
15
15
// We find where the 'S' is
16
16
var startingPoint = FindStartingPoint ( lines ) ;
17
17
pipesMap . Add ( startingPoint . Key , startingPoint . Value ) ;
18
-
18
+
19
19
while ( true )
20
20
{
21
21
var currentPoint = new ValueTuple < int , int > ( ) ;
22
22
var newDirection = Direction . Down ;
23
-
23
+
24
24
if ( pipesMap . Last ( ) . Value == Direction . Right )
25
25
{
26
26
currentPoint = ( pipesMap . Last ( ) . Key . Item1 , pipesMap . Last ( ) . Key . Item2 + 1 ) ;
@@ -41,22 +41,104 @@ public object PartOne(string input)
41
41
currentPoint = ( pipesMap . Last ( ) . Key . Item1 + 1 , pipesMap . Last ( ) . Key . Item2 ) ;
42
42
newDirection = GetNewDirection ( Direction . Down , lines [ currentPoint . Item1 ] [ currentPoint . Item2 ] ) ;
43
43
}
44
-
44
+
45
45
if ( newDirection == Direction . End )
46
46
{
47
47
// We went back to the previous point
48
48
break ;
49
49
}
50
-
50
+
51
51
pipesMap . Add ( currentPoint , newDirection ) ;
52
52
}
53
-
53
+
54
54
return ( pipesMap . Count ) / 2 ;
55
55
}
56
56
57
57
public object PartTwo ( string input )
58
58
{
59
- return 0 ;
59
+ var lines = ParseLines ( input ) ;
60
+ var pipesMap = new Dictionary < ( int , int ) , Direction > ( ) ;
61
+ var visitedTiles = new HashSet < ( int , int ) > ( ) ;
62
+ // We find where the 'S' is
63
+ var startingPoint = FindStartingPoint ( lines ) ;
64
+
65
+ pipesMap . Add ( startingPoint . Key , startingPoint . Value ) ;
66
+ visitedTiles . Add ( startingPoint . Key ) ;
67
+
68
+ while ( true )
69
+ {
70
+ var currentPoint = new ValueTuple < int , int > ( ) ;
71
+ var newDirection = Direction . Down ;
72
+
73
+ if ( pipesMap . Last ( ) . Value == Direction . Right )
74
+ {
75
+ currentPoint = ( pipesMap . Last ( ) . Key . Item1 , pipesMap . Last ( ) . Key . Item2 + 1 ) ;
76
+ newDirection = GetNewDirection ( Direction . Right , lines [ currentPoint . Item1 ] [ currentPoint . Item2 ] ) ;
77
+ }
78
+ else if ( pipesMap . Last ( ) . Value == Direction . Left )
79
+ {
80
+ currentPoint = ( pipesMap . Last ( ) . Key . Item1 , pipesMap . Last ( ) . Key . Item2 - 1 ) ;
81
+ newDirection = GetNewDirection ( Direction . Left , lines [ currentPoint . Item1 ] [ currentPoint . Item2 ] ) ;
82
+ }
83
+ else if ( pipesMap . Last ( ) . Value == Direction . Up )
84
+ {
85
+ currentPoint = ( pipesMap . Last ( ) . Key . Item1 - 1 , pipesMap . Last ( ) . Key . Item2 ) ;
86
+ newDirection = GetNewDirection ( Direction . Up , lines [ currentPoint . Item1 ] [ currentPoint . Item2 ] ) ;
87
+ }
88
+ else if ( pipesMap . Last ( ) . Value == Direction . Down )
89
+ {
90
+ currentPoint = ( pipesMap . Last ( ) . Key . Item1 + 1 , pipesMap . Last ( ) . Key . Item2 ) ;
91
+ newDirection = GetNewDirection ( Direction . Down , lines [ currentPoint . Item1 ] [ currentPoint . Item2 ] ) ;
92
+ }
93
+
94
+ if ( newDirection == Direction . End )
95
+ {
96
+ // We went back to the previous point
97
+ break ;
98
+ }
99
+
100
+ pipesMap . Add ( currentPoint , newDirection ) ;
101
+ visitedTiles . Add ( currentPoint ) ;
102
+ }
103
+
104
+ // We modify Starting point for algo
105
+ var index = startingPoint . Key . Item2 ;
106
+ var str = lines [ startingPoint . Key . Item1 ] ;
107
+ var newStr = str . Remove ( index , 1 ) . Insert ( index , "L" ) ;
108
+ lines [ startingPoint . Key . Item1 ] = newStr ;
109
+
110
+ var insideCount = 0 ;
111
+ for ( int y = 1 ; y < lines . Length - 1 ; y ++ )
112
+ {
113
+ for ( int x = 1 ; x < lines [ 0 ] . Length - 1 ; x ++ )
114
+ {
115
+ if ( visitedTiles . Contains ( ( y , x ) ) )
116
+ {
117
+ continue ;
118
+ }
119
+
120
+ var crossings = 0 ;
121
+
122
+ for ( int i = x + 1 ; i < lines [ 0 ] . Length ; i ++ )
123
+ {
124
+ var tile = lines [ y ] [ i ] ;
125
+ // If we find a wall
126
+ var symbols = new [ ] { '|' , 'J' , 'L' } ;
127
+
128
+ if ( visitedTiles . Contains ( ( y , i ) ) && symbols . Contains ( tile ) )
129
+ {
130
+ crossings ++ ;
131
+ }
132
+ }
133
+
134
+ if ( crossings % 2 == 1 )
135
+ {
136
+ insideCount ++ ;
137
+ }
138
+ }
139
+ }
140
+
141
+ return insideCount ;
60
142
}
61
143
62
144
private static string [ ] ParseLines ( string input ) =>
@@ -127,4 +209,4 @@ internal enum Direction
127
209
Left ,
128
210
Right ,
129
211
End
130
- }
212
+ }
0 commit comments