|
| 1 | +package com.codefork.aoc2024.day24; |
| 2 | + |
| 3 | +import com.codefork.aoc2024.Problem; |
| 4 | +import com.codefork.aoc2024.util.Assert; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import static com.codefork.aoc2024.util.FoldLeft.foldLeft; |
| 13 | + |
| 14 | +public class Part02 extends Problem { |
| 15 | + |
| 16 | + public String solve(Stream<String> data) { |
| 17 | + var device = Device.parse(data); |
| 18 | + |
| 19 | + // this is just a bunch of exploratory code at the moment |
| 20 | + |
| 21 | + var xyz = List.of("x", "y", "z"); |
| 22 | + // group xyz wires by two-digit numeric suffix |
| 23 | + var byPos = device.namesToWires().keySet().stream() |
| 24 | + .filter(name -> xyz.contains(name.substring(0, 1))) |
| 25 | + .collect(Collectors.groupingBy((name) -> name.substring(1, 3))); |
| 26 | + |
| 27 | + var positions = byPos.keySet().stream().sorted().toList(); |
| 28 | + |
| 29 | + var badZ = new ArrayList<String>(); |
| 30 | + |
| 31 | + // look through each bit position, calculate what it should be, and flag it if it's not what we expect |
| 32 | + var carry = 0; |
| 33 | + for(var pos : positions) { |
| 34 | + var z = device.getWire("z" + pos); |
| 35 | + if(device.hasWire("x" + pos)) { |
| 36 | + var x = device.getWire("x" + pos); |
| 37 | + var y = device.getWire("y" + pos); |
| 38 | + var posSum = x ^ y; |
| 39 | + var expected = posSum ^ carry; // if x and y differ, you get the carry |
| 40 | + var flag = (z != expected) ? "FLAGGED" : ""; |
| 41 | + if (z != expected) { |
| 42 | + badZ.add("z" + pos); |
| 43 | + } |
| 44 | + System.out.printf("%s carry=%s x=%s y=%s z=%s expected=%s %s%n", pos, carry, x, y, z, expected, flag); |
| 45 | + carry = (expected == 0 & carry == 1) || ((x & y) == 1) ? 1 : 0; |
| 46 | + } else { |
| 47 | + System.out.printf("%s z=%s%n", pos, z); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // badZ and its dependencies = swap candidates |
| 52 | + var candidates = new HashSet<String>(); |
| 53 | + for(var zName : badZ) { |
| 54 | + var deps = device.getGateDependencies(zName); |
| 55 | + System.out.println(zName + " dependencies = " + deps); |
| 56 | + candidates.addAll(deps); |
| 57 | + } |
| 58 | + // this reduces the candidate pool, but only by about 50. doesn't seem like the optimal way to do this. |
| 59 | + System.out.println("total candidates=" + candidates.size()); |
| 60 | + |
| 61 | + // figure out how to test every combination of 4 pair swaps in that set of candidates? |
| 62 | + return "UNFINISHED"; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String solve() { |
| 67 | + return solve(getInput()); |
| 68 | + } |
| 69 | + |
| 70 | + public static void main(String[] args) { |
| 71 | + new Part02().run(); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments