@@ -28,7 +28,9 @@ public string Part1(string input)
28
28
29
29
foreach ( var lineRange in lines )
30
30
{
31
- if ( AnalyseReport ( inputSpan [ lineRange ] ) == ReportResult . Safe )
31
+ var line = inputSpan [ lineRange ] ;
32
+
33
+ if ( AnalyseReport ( line ) == ( ReportResult . Safe , null ) )
32
34
{
33
35
safeCount ++ ;
34
36
}
@@ -37,53 +39,66 @@ public string Part1(string input)
37
39
return safeCount . ToString ( ) ;
38
40
}
39
41
40
- private ReportResult AnalyseReport ( ReadOnlySpan < char > report , bool withDampener = false )
42
+ private ( ReportResult result , int ? failedIndex ) AnalyseReport ( ReadOnlySpan < char > report , int ? ignoreIndex = null )
41
43
{
42
44
var values = report . Split ( " " ) ;
43
45
var currentDirection = Direction . Unknown ;
46
+ int currentIndex = 0 ;
44
47
int ? lastValue = null ;
45
- bool dampenerTriggered = ! withDampener ; // Consider the dampener triggered if there is no dampener
46
48
47
49
foreach ( var valueRange in values )
48
50
{
49
51
int currentValue = Number . FastParseInt ( report [ valueRange ] ) ;
50
52
51
- if ( lastValue is null )
53
+ if ( lastValue is null && ignoreIndex != currentIndex )
52
54
{
53
55
lastValue = currentValue ;
56
+ currentIndex ++ ;
54
57
continue ; // Skip the first value
55
58
}
56
59
57
- int change = Math . Abs ( ( int ) ( currentValue - lastValue ) ) ;
58
- var pendingResult = ReportResult . Safe ;
59
-
60
- if ( change < 1 || change > 3 )
60
+ if ( currentIndex != ignoreIndex )
61
61
{
62
- pendingResult = ReportResult . Unsafe ;
62
+ int change = Math . Abs ( ( int ) ( currentValue - lastValue ) ! ) ;
63
+
64
+ if ( change < 1 || change > 3 )
65
+ {
66
+ return ( ReportResult . Unsafe , currentIndex ) ;
67
+ }
68
+
69
+ var nextDirection = currentValue > lastValue ? Direction . Ascending : Direction . Descending ;
70
+
71
+ if ( currentDirection == Direction . Unknown )
72
+ {
73
+ currentDirection = nextDirection ;
74
+ }
75
+ else if ( currentDirection != nextDirection )
76
+ {
77
+ return ( ReportResult . Unsafe , currentIndex ) ;
78
+ }
79
+
80
+ lastValue = currentValue ;
63
81
}
82
+
83
+ currentIndex ++ ;
84
+ }
64
85
65
- var nextDirection = currentValue > lastValue ? Direction . Ascending : Direction . Descending ;
66
-
67
- if ( currentDirection == Direction . Unknown )
68
- {
69
- currentDirection = nextDirection ;
70
- }
71
- else if ( currentDirection != nextDirection )
72
- {
73
- pendingResult = ReportResult . Unsafe ;
74
- }
86
+ return ( ReportResult . Safe , null ) ;
87
+ }
75
88
76
- if ( pendingResult == ReportResult . Unsafe && dampenerTriggered )
89
+ private ReportResult AnalyseReportWithDampening ( ReadOnlySpan < char > report )
90
+ {
91
+ foreach ( var ignoreIndex in new int ? [ ] { null , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 } )
92
+ {
93
+ if ( AnalyseReport ( report , ignoreIndex ) == ( ReportResult . Safe , null ) )
77
94
{
78
- return pendingResult ;
95
+ return ReportResult . Safe ;
79
96
}
80
-
81
- lastValue = currentValue ;
82
97
}
83
98
84
- return ReportResult . Safe ;
99
+ return ReportResult . Unsafe ;
85
100
}
86
-
101
+
87
102
public string Part2 ( string input )
88
103
{
89
104
var inputSpan = input . AsSpan ( ) ;
@@ -93,7 +108,9 @@ public string Part2(string input)
93
108
94
109
foreach ( var lineRange in lines )
95
110
{
96
- if ( AnalyseReport ( inputSpan [ lineRange ] , true ) == ReportResult . Safe )
111
+ var line = inputSpan [ lineRange ] ;
112
+
113
+ if ( AnalyseReportWithDampening ( line ) == ReportResult . Safe )
97
114
{
98
115
safeCount ++ ;
99
116
}
0 commit comments