Skip to content

Commit b695b77

Browse files
committed
Clean up day 10 solution
1 parent 4ea8a5c commit b695b77

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

day10/src/day10.groovy

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,32 @@ class TopoMap {
1818

1919
def get(Vec2 pos) { values[pos.y][pos.x] }
2020

21-
def traceTrail(Vec2 pos, boolean isPart1, Set<Vec2> visited = new HashSet()) {
22-
if (isPart1) {
21+
def traceTrail(Vec2 pos, Set<Vec2> visited) {
22+
if (visited != null) {
2323
if (pos in visited) {
24-
return
24+
return 0
2525
}
26-
2726
visited.add(pos)
2827
}
2928

3029
int value = get(pos)
30+
int score = 0
3131
if (value == 0) {
32-
if (isPart1) {
33-
part1++
34-
} else {
35-
part2++
36-
}
32+
score++
3733
}
3834

3935
for (int dy in (-1..1)) {
4036
for (int dx in (-1..1)) {
4137
if (dy == 0 ^ dx == 0) {
4238
Vec2 neigh = new Vec2(pos.x + dx, pos.y + dy)
4339
if (neigh.y >= 0 && neigh.y < height && neigh.x >= 0 && neigh.x < width && get(neigh) == value - 1) {
44-
traceTrail(neigh, isPart1, visited)
40+
score += traceTrail(neigh, visited)
4541
}
4642
}
4743
}
4844
}
45+
46+
return score
4947
}
5048
}
5149

@@ -54,23 +52,21 @@ if (args.length == 0) {
5452
System.exit(1)
5553
}
5654

57-
List<List<Integer>> values = new File(args[0]).text
58-
.lines()
59-
.map { it.chars().map { it - (int) '0' }.toList() }
60-
.toList()
61-
55+
List<List<Integer>> values = new File(args[0]).text.lines().map { it.chars().map { it - (int) '0' }.toList() }.toList()
6256
TopoMap topoMap = new TopoMap(values)
6357

58+
int part1 = 0
59+
int part2 = 0
60+
6461
for (int y in 0..<topoMap.height) {
6562
for (int x in 0..<topoMap.width) {
6663
Vec2 pos = new Vec2(x, y)
6764
if (topoMap.get(pos) == 9) {
68-
for (boolean isPart1 in [true, false]) {
69-
topoMap.traceTrail(pos, isPart1)
70-
}
65+
part1 += topoMap.traceTrail(pos, new HashSet())
66+
part2 += topoMap.traceTrail(pos, null)
7167
}
7268
}
7369
}
7470

75-
println "Part 1: $topoMap.part1"
76-
println "Part 2: $topoMap.part2"
71+
println "Part 1: $part1"
72+
println "Part 2: $part2"

0 commit comments

Comments
 (0)