-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay7.java
45 lines (36 loc) · 1.51 KB
/
Day7.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import com.horstmann.adventofcode.*;
record Equation(long result, List<Long> operands) {
static Equation parse(String line) {
List<Long> parts = Util.parseLongs(line, ":? ");
return new Equation(parts.get(0), Lists.withoutFirst(parts));
}
boolean hasSolution(List<LongBinaryOperator> operators) { return hasSolution(operands.get(0), 1, operators); }
boolean hasSolution(long prefixValue, int k, List<LongBinaryOperator> operators) {
if (prefixValue > result) return false;
else if (k == operands.size()) return result == prefixValue;
else return operators.stream().anyMatch(op -> hasSolution(op.applyAsLong(prefixValue, operands.get(k)), k + 1, operators));
}
}
List<Equation> equations;
void parse(Path p) throws IOException {
equations = Files.lines(p).map(Equation::parse).toList();
}
LongBinaryOperator PLUS = Long::sum;
LongBinaryOperator TIMES = (x, y) -> x * y;
LongBinaryOperator CONCAT = (x, y) -> Long.parseLong("" + x + y);
Object part1() {
return equations.stream().filter(e -> e.hasSolution(List.of(PLUS, TIMES))).mapToLong(Equation::result).sum();
}
Object part2() {
return equations.stream().filter(e -> e.hasSolution(List.of(PLUS, TIMES, CONCAT))).mapToLong(Equation::result).sum();
}
void main() throws IOException {
Util.time(() -> {
parse(Util.inputPath("a"));
IO.println(part1());
IO.println(part2());
parse(Util.inputPath("z"));
IO.println(part1());
IO.println(part2());
});
}