@@ -12,29 +12,16 @@ public partial class Part2 : IPuzzleSolution
12
12
public async Task < string > SolveAsync ( StreamReader inputReader )
13
13
{
14
14
await ReadInputAsync ( inputReader ) ;
15
-
16
- _swappedOutputs . Add ( ( "z12" , "vdc" ) ) ;
17
- _swappedOutputs . Add ( ( "z21" , "nhn" ) ) ;
18
- _swappedOutputs . Add ( ( "tvb" , "khg" ) ) ;
19
- _swappedOutputs . Add ( ( "z33" , "gst" ) ) ;
20
15
21
- int lastFaultyIndex = 33 ;
22
-
23
- if ( _swappedOutputs . Count > 4 )
16
+ SortConnectionsTopologically ( ) ;
17
+ var lastFaultyIndex = 12 ;
18
+ while ( _swappedOutputs . Count < 4 )
24
19
{
25
- foreach ( var swap in _swappedOutputs )
26
- {
27
- SwapOutputs ( swap . gateA , swap . gateB ) ;
28
- }
29
-
30
- SortConnectionsTopologically ( ) ;
20
+ var fix = FindFixForFaultyConnection ( lastFaultyIndex ) ;
31
21
32
- var fixes = FindFixesForFaultyConnection ( lastFaultyIndex ) ;
33
-
34
- foreach ( var fix in fixes )
35
- {
36
- Console . WriteLine ( $ "{ fix . gate1 } -> { fix . gate2 } #{ fix . faultIndex } ") ;
37
- }
22
+ _swappedOutputs . Add ( ( fix . gate1 , fix . gate2 ) ) ;
23
+ SwapOutputs ( fix . gate1 , fix . gate2 ) ;
24
+ lastFaultyIndex = fix . faultIndex ;
38
25
}
39
26
40
27
var names = string . Join ( "," ,
@@ -46,7 +33,7 @@ public async Task<string> SolveAsync(StreamReader inputReader)
46
33
return names ;
47
34
}
48
35
49
- private ( string gate1 , string gate2 , int faultIndex ) [ ] FindFixesForFaultyConnection ( int firstInvalidZGate )
36
+ private ( string gate1 , string gate2 , int faultIndex ) FindFixForFaultyConnection ( int firstInvalidZGate )
50
37
{
51
38
var potentialFixes = new List < ( string gate1 , string gate2 , int faultIndex ) > ( ) ;
52
39
var confirmedGates = GetDependencies ( firstInvalidZGate - 1 ) ;
@@ -62,13 +49,12 @@ public async Task<string> SolveAsync(StreamReader inputReader)
62
49
63
50
if ( newFaultIndex > firstInvalidZGate )
64
51
{
65
- potentialFixes . Add ( ( suspectedGate , swapGate , newFaultIndex ) ) ;
66
- Console . WriteLine ( $ "Potential fix: { suspectedGate } -> { swapGate } #{ newFaultIndex } ") ;
52
+ return ( suspectedGate , swapGate , newFaultIndex ) ;
67
53
}
68
54
}
69
55
}
70
56
71
- return potentialFixes . ToArray ( ) ;
57
+ throw new InvalidOperationException ( "No fix found." ) ;
72
58
}
73
59
74
60
private HashSet < string > GetDependencies ( int zGate )
0 commit comments