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 a3fd8e7

Browse files
rewrite in functional style
1 parent d9c1328 commit a3fd8e7

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.codefork.aoc2024.day17;
22

33
import com.codefork.aoc2024.Problem;
4-
import com.codefork.aoc2024.util.Assert;
54

65
import java.util.ArrayList;
76
import java.util.List;
7+
import java.util.stream.IntStream;
88
import java.util.stream.Stream;
99

1010
/**
@@ -30,39 +30,32 @@
3030
*/
3131
public class Part02 extends Problem {
3232

33+
public long findLowestAForQuine(Computer computer, int i, List<Long> candidates) {
34+
if(i >= 0) {
35+
var expectedOutput = computer.program().subList(i, computer.program().size());
36+
var newCandidates = candidates.stream()
37+
.flatMap(candidate ->
38+
IntStream.range(0, 8).boxed().flatMap(threeBits -> {
39+
var testA = (candidate << 3) + threeBits;
40+
var testComputer = computer.withA(testA).run();
41+
return testComputer.output().equals(expectedOutput) ? Stream.of(testA) : Stream.empty();
42+
}))
43+
.toList();
44+
return findLowestAForQuine(computer, i-1, newCandidates);
45+
} else {
46+
return candidates.stream().mapToLong(n -> n).min().orElseThrow();
47+
}
48+
}
49+
3350
public String solve(Stream<String> data) {
3451
var initialComputer = Computer.parse(data);
35-
var programSize = initialComputer.program().size();
36-
37-
var i = programSize - 1;
38-
List<Integer> expectedOutput = new ArrayList<>();
39-
List<Long> candidates = new ArrayList<>();
40-
candidates.add(0L);
4152

42-
while (i >= 0) {
43-
expectedOutput.addFirst(initialComputer.program().get(i));
53+
vari = initialComputer.program().size() - 1;
54+
List<Long> candidates = List.of(0L);
4455

45-
List<Long> newCandidates = new ArrayList<>();
46-
47-
//System.out.println("looking for next expected output=" + expectedOutput);
48-
49-
for(var candidate : candidates) {
50-
for(var threeBits = 0; threeBits < 8; threeBits++) {
51-
var testA = (candidate << 3) + threeBits;
52-
var testComputer = initialComputer.withA(testA);
53-
var finalState = testComputer.run();
54-
if(finalState.output().equals(expectedOutput)) {
55-
newCandidates.add(testA);
56-
}
57-
}
58-
}
59-
candidates = newCandidates;
60-
//System.out.println("candidates=" + candidates);
61-
i--;
62-
}
56+
var lowestA = findLowestAForQuine(initialComputer, i, candidates);
6357

64-
var lowest = candidates.stream().mapToLong(n -> n).min().orElseThrow();
65-
return String.valueOf(lowest);
58+
return String.valueOf(lowestA);
6659
}
6760

6861
@Override

0 commit comments

Comments
(0)

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