generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.kt
158 lines (130 loc) · 4.26 KB
/
Day12.kt
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package year2021.`12`
import readInput
private sealed class Place {
data object Start : Place() {
override fun toString(): String = "start"
}
data class LargePlace(val key: String) : Place() {
override fun toString(): String = key
}
data class SmallPlace(val key: String) : Place() {
override fun toString(): String = key
}
data object End : Place() {
override fun toString(): String = "end"
}
fun isStart(): Boolean = this is Start
}
private data class Path(
val from: Place,
val to: Place,
) {
override fun toString(): String {
return "$from-$to"
}
}
private fun Place.neighbours(paths: Set<Path>): Set<Place> {
return paths
.filter { it.from == this }
.map { it.to }
.toSet()
}
fun main() {
fun from(string: String): Place {
return when {
string == "start" -> Place.Start
string == "end" -> Place.End
string.all { it.isUpperCase() } -> Place.LargePlace(string)
string.all { it.isLowerCase() } -> Place.SmallPlace(string)
else -> error("Illegal string: $string")
}
}
fun parse(input: List<String>): Set<Path> {
return input.map {
val (from, to) = it.split("-")
.map(::from)
listOf(Path(from, to), Path(to, from))
}
.flatten()
.toSet()
}
fun recursion(
cache: MutableSet<List<Place>>,
current: Place,
paths: Set<Path>,
currentPath: List<Place>,
placeThatCanBeVisitedTwice: Place?,
visitedPlaces: Map<Place, Int>,
) {
if (current is Place.End) {
cache.add(currentPath + current)
return
}
val amountOfVisited = visitedPlaces.getOrDefault(current, 0).inc()
val newVisitedPlace = visitedPlaces + (current to amountOfVisited)
current.neighbours(paths)
// Do not return to start
.filter { neighbour -> !neighbour.isStart() }
// Do not return to small cave if you were here previously.
.filter { neighbour ->
val threshold = if (neighbour == placeThatCanBeVisitedTwice) {
1
} else {
0
}
val smallPlaceVisitedTwice = (visitedPlaces.getOrDefault(neighbour, 0) > threshold)
val shouldStop = (neighbour is Place.SmallPlace) && smallPlaceVisitedTwice
!shouldStop
}
.forEach {
recursion(
cache = cache,
current = it,
paths = paths,
currentPath = currentPath + current,
placeThatCanBeVisitedTwice = placeThatCanBeVisitedTwice,
visitedPlaces = newVisitedPlace
)
}
}
fun part1(input: List<String>): Int {
val pathSet = parse(input)
val cache = mutableSetOf<List<Place>>()
recursion(
cache = cache,
current = Place.Start,
paths = pathSet,
currentPath = emptyList(),
placeThatCanBeVisitedTwice = null,
visitedPlaces = emptyMap()
)
return cache.size
}
fun part2(input: List<String>): Int {
val pathSet = parse(input)
val cache = mutableSetOf<List<Place>>()
pathSet.map { listOf(it.to, it.from) }
.flatten()
.filterIsInstance<Place.SmallPlace>()
.toSet()
.forEach {
recursion(
cache = cache,
current = Place.Start,
paths = pathSet,
currentPath = emptyList(),
placeThatCanBeVisitedTwice = it,
visitedPlaces = emptyMap()
)
}
return cache.size
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day12_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 10)
val input = readInput("Day12")
println(part1(input).also { check(it == 5254) })
println(part2(input).also { check(it == 149385) })
}