Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 742c2dc

Browse files
optimize: precompute zIndices
1 parent 987cab2 commit 742c2dc

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ identify what the configuration of the full adder should look like for each z wi
187187
I ended up implementing [a solution by someone](https://www.reddit.com/r/adventofcode/comments/1hneuf0/comment/m41ms44/)
188188
who used a brute force approach I thought was pretty clever: set up a bunch of test devices with randomized x and y
189189
values, find a swap that results in the most improvement for all of them, and repeat until you find the set of 4 swaps
190-
that works for every test device. After adding caching to the evaluation of wire values, my solution ran in only 35s
190+
that works for every test device. After adding caching to the evaluation of wire values, my solution ran in only 30s
191191
(the author reported it taking 2.5 mins on their machine). More details are in the code.
192192

193193
I suppose slogging through it was worth the insight into how mathematical addition works at the level of logical gates

‎src/main/java/com/codefork/aoc2024/day24/Device.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
import 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 staticList<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) {

‎src/main/java/com/codefork/aoc2024/day24/Part02.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class Part02 extends Problem {
88

99
public String solve(Stream<String> data) {
10-
System.out.println("This takes ~35s to run");
10+
System.out.println("This takes ~30s to run");
1111
var device = Device.parse(data);
1212
return Device.formatAnswer(device.findSwapsToMakeAdder());
1313
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /