Skip to content

Commit d80e8a8

Browse files
committed
2024 q7
1 parent a178f3a commit d80e8a8

File tree

7 files changed

+959
-4
lines changed

7 files changed

+959
-4
lines changed

twentytwentyfour/src/main/java/com/icecreamhead/adventofcode/q6/GuardGallivant.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.icecreamhead.adventofcode.q6;
22

3-
import java.io.IOException;
43
import java.util.ArrayList;
54
import java.util.HashSet;
65
import java.util.Set;
@@ -95,7 +94,7 @@ long pt2(char[][] map) {
9594
x = nextx;
9695
y = nexty;
9796
been.add(new P(g, x, y));
98-
if (newPathIntersectsExistingPath(been, map, rotate90(g), x, y)) {
97+
if (newPathIntersectsExistingPath(been, map, rotate90(g), x, y, false)) {
9998
switch (g) {
10099
case UP -> blocks.add(new P('O', x, y - 1));
101100
case RIGHT -> blocks.add(new P('O', x + 1, y));
@@ -120,11 +119,18 @@ long pt2(char[][] map) {
120119
return blocks.size();
121120
}
122121

123-
private static boolean newPathIntersectsExistingPath(Set<P> been, char[][] map, char g, int x, int y) {
122+
private static boolean newPathIntersectsExistingPath(Set<P> been, char[][] map, char g, int x, int y, boolean alreadyRecursed) {
124123
while (true) {
125124
// print(map, g, x, y);
125+
126126
try {
127-
if (map[y][x] == '#') return false; // need to recurse here
127+
if (map[y][x] == '#' && !alreadyRecursed) return switch (g) {
128+
case UP -> newPathIntersectsExistingPath(been, map, rotate90(g), x, y + 1, true);
129+
case DOWN -> newPathIntersectsExistingPath(been, map, rotate90(g), x, y - 1, true);
130+
case LEFT -> newPathIntersectsExistingPath(been, map, rotate90(g), x + 1, y, true);
131+
case RIGHT -> newPathIntersectsExistingPath(been, map, rotate90(g), x - 1, y, true);
132+
default -> throw new IllegalStateException("Illegal g: " + g);
133+
};
128134
} catch (ArrayIndexOutOfBoundsException e) {
129135
// System.err.println(e);
130136
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.icecreamhead.adventofcode.q7;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Set;
6+
import java.util.function.BiFunction;
7+
import java.util.stream.Stream;
8+
9+
public class BridgeRepair {
10+
11+
long pt1(List<String> input) {
12+
var ops = Set.of(add, mul);
13+
return input.stream()
14+
.mapToLong(l -> {
15+
long expected = Long.parseLong(l.split(":")[0].strip());
16+
long[] vs = Arrays.stream(l.split(":")[1].strip().split(" ")).mapToLong(Long::parseLong).toArray();
17+
if (ops.stream().anyMatch(o -> recursiveFind(o, vs[0], vs[1], vs, 2, expected, ops))) {
18+
return expected;
19+
}
20+
return 0;
21+
})
22+
.sum();
23+
}
24+
25+
long pt2(List<String> input) {
26+
var ops = Set.of(add, mul, cat);
27+
return input.stream()
28+
.mapToLong(l -> {
29+
long expected = Long.parseLong(l.split(":")[0].strip());
30+
long[] vs = Arrays.stream(l.split(":")[1].strip().split(" ")).mapToLong(Long::parseLong).toArray();
31+
if (ops.stream().anyMatch(o -> recursiveFind(o, vs[0], vs[1], vs, 2, expected, ops))) {
32+
return expected;
33+
}
34+
return 0;
35+
})
36+
.sum();
37+
}
38+
39+
private boolean recursiveFind(BiFunction<Long, Long, Long> op, long x, long y, long[] tail, int nextIdx, long expected, Set<BiFunction<Long, Long, Long>> ops) {
40+
long z = op.apply(x, y);
41+
if (z > expected) return false;
42+
if (nextIdx == tail.length) {
43+
return z == expected;
44+
}
45+
return ops.stream().anyMatch(o -> recursiveFind(o, z, tail[nextIdx], tail, nextIdx + 1, expected, ops));
46+
}
47+
48+
private static final BiFunction<Long, Long, Long> add = Long::sum;
49+
private static final BiFunction<Long, Long, Long> mul = (Long x, Long y) -> x * y;
50+
private static final BiFunction<Long, Long, Long> cat = (Long x, Long y) -> Long.parseLong("" + x + y);
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.#....
2+
....#.
3+
#^....
4+
......
5+
......
6+
...#..

0 commit comments

Comments
 (0)