Skip to content

Commit 3c54aa5

Browse files
committed
AoC 2024 Part 2 automated
1 parent b8c7444 commit 3c54aa5

File tree

1 file changed

+10
-24
lines changed
  • src/AdventOfCode.Puzzles/2024/24/Part2

1 file changed

+10
-24
lines changed

src/AdventOfCode.Puzzles/2024/24/Part2/Part2.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,16 @@ public partial class Part2 : IPuzzleSolution
1212
public async Task<string> SolveAsync(StreamReader inputReader)
1313
{
1414
await ReadInputAsync(inputReader);
15-
16-
_swappedOutputs.Add(("z12", "vdc"));
17-
_swappedOutputs.Add(("z21", "nhn"));
18-
_swappedOutputs.Add(("tvb", "khg"));
19-
_swappedOutputs.Add(("z33", "gst"));
2015

21-
int lastFaultyIndex = 33;
22-
23-
if (_swappedOutputs.Count > 4)
16+
SortConnectionsTopologically();
17+
var lastFaultyIndex = 12;
18+
while (_swappedOutputs.Count < 4)
2419
{
25-
foreach (var swap in _swappedOutputs)
26-
{
27-
SwapOutputs(swap.gateA, swap.gateB);
28-
}
29-
30-
SortConnectionsTopologically();
20+
var fix = FindFixForFaultyConnection(lastFaultyIndex);
3121

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;
3825
}
3926

4027
var names = string.Join(",",
@@ -46,7 +33,7 @@ public async Task<string> SolveAsync(StreamReader inputReader)
4633
return names;
4734
}
4835

49-
private (string gate1, string gate2, int faultIndex)[] FindFixesForFaultyConnection(int firstInvalidZGate)
36+
private (string gate1, string gate2, int faultIndex) FindFixForFaultyConnection(int firstInvalidZGate)
5037
{
5138
var potentialFixes = new List<(string gate1, string gate2, int faultIndex)>();
5239
var confirmedGates = GetDependencies(firstInvalidZGate - 1);
@@ -62,13 +49,12 @@ public async Task<string> SolveAsync(StreamReader inputReader)
6249

6350
if (newFaultIndex > firstInvalidZGate)
6451
{
65-
potentialFixes.Add((suspectedGate, swapGate, newFaultIndex));
66-
Console.WriteLine($"Potential fix: {suspectedGate} -> {swapGate} #{newFaultIndex}");
52+
return (suspectedGate, swapGate, newFaultIndex);
6753
}
6854
}
6955
}
7056

71-
return potentialFixes.ToArray();
57+
throw new InvalidOperationException("No fix found.");
7258
}
7359

7460
private HashSet<string> GetDependencies(int zGate)

0 commit comments

Comments
 (0)