16
16
17
17
import static com .codefork .aoc2024 .util .FoldLeft .foldLeft ;
18
18
19
- public record Device (Map <String , Wire > namesToWires ) {
19
+ public record Device (Map <String , Wire > namesToWires , List <String > zIndices ) {
20
+
21
+ public Device (Map <String , Wire > namesToWires ) {
22
+ this (namesToWires , getIndices (namesToWires , 'z' ));
23
+ }
20
24
21
25
/**
22
26
* Evaluates wires in a device. This has a cache, which makes evaluating
@@ -131,7 +135,7 @@ public Device swap(String name1, String name2) {
131
135
var wire2 = newNamesToWires .get (name2 );
132
136
newNamesToWires .put (name1 , wire2 );
133
137
newNamesToWires .put (name2 , wire1 );
134
- return new Device (newNamesToWires );
138
+ return new Device (newNamesToWires , zIndices );
135
139
}
136
140
137
141
/**
@@ -197,8 +201,8 @@ public Map<String, Set<String>> getDependencies() {
197
201
* returns two-digit strings ("00", "01", etc.) for the wires with
198
202
* the specific character prefix
199
203
*/
200
- public List <String > getIndices (char prefix ) {
201
- return namesToWires () .keySet ().stream ()
204
+ public static List <String > getIndices (Map < String , Wire > namesToWires , char prefix ) {
205
+ return namesToWires .keySet ().stream ()
202
206
.filter (name -> name .charAt (0 ) == prefix )
203
207
.map (name -> name .substring (1 , 3 ))
204
208
.sorted ()
@@ -215,10 +219,11 @@ public List<String> getIndices(char prefix) {
215
219
* @return
216
220
*/
217
221
public List <String > validateAddition (boolean debug ) {
222
+ var xIndices = zIndices .subList (0 , zIndices .size () - 1 );
218
223
var eval = new Evaluator (this );
219
224
long x = 0L ;
220
225
long y = 0L ;
221
- for (var pos : getIndices ( 'x' ) .reversed ()) {
226
+ for (var pos : xIndices .reversed ()) {
222
227
var xWire = eval .get ("x" + pos );
223
228
x = (x << 1 ) + xWire ;
224
229
var yWire = eval .get ("y" + pos );
@@ -231,7 +236,7 @@ public List<String> validateAddition(boolean debug) {
231
236
System .out .printf (" sum=%s (%s)%n" , sum , Long .toBinaryString (sum ));
232
237
}
233
238
234
- return getIndices ( 'z' ) .stream ()
239
+ return zIndices .stream ()
235
240
.flatMap (pos -> {
236
241
var z = eval .get ("z" + pos );
237
242
var expected = (sum >> Integer .parseInt (pos )) & 1 ;
@@ -297,13 +302,14 @@ public void writeGraphVizDotFile() {
297
302
* returns a new Device with randomized values in the x and y wires
298
303
*/
299
304
public Device randomizeXandY () {
305
+ var xIndices = zIndices .subList (0 , zIndices .size () - 1 );
300
306
Map <String , Wire > newMap = new HashMap <>(namesToWires ());
301
307
var random = new Random ();
302
- for (var pos : getIndices ( 'x' ) ) {
308
+ for (var pos : xIndices ) {
303
309
newMap .put ("x" + pos , new Device .LiteralValue (Math .abs (random .nextInt ()) % 2 ));
304
310
newMap .put ("y" + pos , new Device .LiteralValue (Math .abs (random .nextInt ()) % 2 ));
305
311
}
306
- return new Device (newMap );
312
+ return new Device (newMap , zIndices );
307
313
}
308
314
309
315
public static String formatAnswer (List <Pair > swaps ) {
0 commit comments