1616
1717import static com .codefork .aoc2024 .util .FoldLeft .foldLeft ;
1818
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+ }
2024
2125 /**
2226 * Evaluates wires in a device. This has a cache, which makes evaluating
@@ -131,7 +135,7 @@ public Device swap(String name1, String name2) {
131135 var wire2 = newNamesToWires .get (name2 );
132136 newNamesToWires .put (name1 , wire2 );
133137 newNamesToWires .put (name2 , wire1 );
134- return new Device (newNamesToWires );
138+ return new Device (newNamesToWires , zIndices );
135139 }
136140
137141 /**
@@ -197,8 +201,8 @@ public Map<String, Set<String>> getDependencies() {
197201 * returns two-digit strings ("00", "01", etc.) for the wires with
198202 * the specific character prefix
199203 */
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 ()
202206 .filter (name -> name .charAt (0 ) == prefix )
203207 .map (name -> name .substring (1 , 3 ))
204208 .sorted ()
@@ -215,10 +219,11 @@ public List<String> getIndices(char prefix) {
215219 * @return
216220 */
217221 public List <String > validateAddition (boolean debug ) {
222+ var xIndices = zIndices .subList (0 , zIndices .size () - 1 );
218223 var eval = new Evaluator (this );
219224 long x = 0L ;
220225 long y = 0L ;
221- for (var pos : getIndices ( 'x' ) .reversed ()) {
226+ for (var pos : xIndices .reversed ()) {
222227 var xWire = eval .get ("x" + pos );
223228 x = (x << 1 ) + xWire ;
224229 var yWire = eval .get ("y" + pos );
@@ -231,7 +236,7 @@ public List<String> validateAddition(boolean debug) {
231236 System .out .printf (" sum=%s (%s)%n" , sum , Long .toBinaryString (sum ));
232237 }
233238
234- return getIndices ( 'z' ) .stream ()
239+ return zIndices .stream ()
235240 .flatMap (pos -> {
236241 var z = eval .get ("z" + pos );
237242 var expected = (sum >> Integer .parseInt (pos )) & 1 ;
@@ -297,13 +302,14 @@ public void writeGraphVizDotFile() {
297302 * returns a new Device with randomized values in the x and y wires
298303 */
299304 public Device randomizeXandY () {
305+ var xIndices = zIndices .subList (0 , zIndices .size () - 1 );
300306 Map <String , Wire > newMap = new HashMap <>(namesToWires ());
301307 var random = new Random ();
302- for (var pos : getIndices ( 'x' ) ) {
308+ for (var pos : xIndices ) {
303309 newMap .put ("x" + pos , new Device .LiteralValue (Math .abs (random .nextInt ()) % 2 ));
304310 newMap .put ("y" + pos , new Device .LiteralValue (Math .abs (random .nextInt ()) % 2 ));
305311 }
306- return new Device (newMap );
312+ return new Device (newMap , zIndices );
307313 }
308314
309315 public static String formatAnswer (List <Pair > swaps ) {
0 commit comments