@@ -134,6 +134,7 @@ object Day06 : Solution() {
134134 }
135135
136136 override fun part2 (input : SolutionInput ): SolutionResult {
137+ // --- precompute cache ---
137138 val (grid, pos) = parse(input)
138139
139140 val guardOrigin = pos to Direction .UP
@@ -147,17 +148,17 @@ object Day06 : Solution() {
147148
148149 while (grid.isObstacle(newPos)) {
149150 cacheDir = cacheDir.rotate()
150- newPos = cachePos // rotate in place
151+ newPos = cachePos + cacheDir
151152 }
152153
153154 cachePos = newPos
154155 }
155156
156-
157- // NOTE: we don't care about candidate direction so we can cut it out of the zip
157+ // --- filter out duplicated candidates and zip with guard positions ---
158+ // NOTE: don't care about candidate direction so cut it out of the zip
158159 val guardWithCandidate = cache.zipWithNext().map { (a, b) -> a to b.pos }
159160
160- // NOTE: filter guard with candidate zip to only unique candidates with their guard position pair
161+ // NOTE: filter zip to only unique candidates with their guard position pair
161162 val seenBoxes = mutableSetOf<Pair <Int , Int >>()
162163 val filteredPaths = mutableListOf<Pair <DirectionalPos , Pair <Int , Int >>>()
163164 for ((guard, candidate) in guardWithCandidate) {
@@ -166,10 +167,10 @@ object Day06 : Solution() {
166167 }
167168 }
168169
170+ // --- count loops ---
169171 val seen = DirectionalGridBitSet (grid.width, grid.height)
170172 var count = 0
171173
172- // zipped iteration for (candidate and guard) pairs
173174 filteredPaths.forEachIndexed { i, (guard, candidate) ->
174175 var (pos, dir) = guard
175176 seen.clear()
0 commit comments