Skip to content

Commit b8d0a28

Browse files
committed
part2
1 parent 183a8c9 commit b8d0a28

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/main/kotlin/aoc2021/day12.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ internal val caveMapSample = File("src/main/resources/2021/day12-sample.txt").re
77

88
fun main() {
99
part1(parseGraph(caveMapSample))
10-
// part1(parseGraph(caveMapInput))
10+
part1(parseGraph(caveMapInput))
1111

1212
part2(parseGraph(caveMapSample))
13-
// part2(parseGraph(caveMapInput))
13+
part2(parseGraph(caveMapInput))
1414
}
1515

1616
private fun part1(graph: Map<String, List<String>>) {
@@ -47,8 +47,13 @@ private fun dfsPart2(
4747
}
4848

4949
return graph[cave]!!
50-
.filter { nextCave -> nextCave !in visited + cave || nextCave.isBigCave() }
51-
.flatMap { nextCave -> dfs(graph, cave = nextCave, visited = visited + cave) }
50+
.filter { it != "start" }
51+
.filter { nextCave ->
52+
nextCave !in visited + cave ||
53+
nextCave.isBigCave() ||
54+
!(visited + cave).filter { it.isSmallCave() }.groupingBy { it }.eachCount().any { it.value >= 2 }
55+
}
56+
.flatMap { nextCave -> dfsPart2(graph, cave = nextCave, visited = visited + cave) }
5257
}
5358

5459
private fun dfs(

0 commit comments

Comments
 (0)