-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day19.kt
45 lines (35 loc) · 1.48 KB
/
Day19.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
package aoc2017.day19
import util.Coord
import util.readInputLineByLine
class Day19(val path: String) {
private val deltas = listOf(Coord(0, 1), Coord(0, -1), Coord(1, 0), Coord(-1, 0))
val input = readInputLineByLine(path)
private val grid = input.map { it.toCharArray() }
private val computedPath = computePath(Coord(input[0].indexOf("|"), 0), Coord(0, 1))
fun computeLettersVisited(): String = computedPath.second
fun computeTotalSteps(): Int = computedPath.first
private tailrec fun computePath(
location: Coord,
direction: Coord,
visited: List<Char> = emptyList(),
steps: Int = 0
): Pair<Int, String> =
if (grid.at(location) == ' ')
Pair(steps, visited.joinToString(""))
else {
when (grid.at(location)) {
in 'A'..'Z' -> computePath(location + direction, direction, visited + grid.at(location), steps.inc())
'+' -> {
val turn = turn(location, direction)
computePath(location + turn, turn, visited, steps.inc())
}
else -> computePath(location + direction, direction, visited, steps.inc())
}
}
private fun turn(location: Coord, direction: Coord) = deltas
.filter { it != direction }
.filter { it != !direction }
.first { grid.at(location + it) != ' ' }
private fun List<CharArray>.at(coord: Coord): Char =
this[coord.y][coord.x]
}