Skip to content

Commit e569369

Browse files
committed
Solution 2016-01 (No Time for a Taxicab)
1 parent 746ac72 commit e569369

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/Coordinates.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package de.ronny_h.aoc.extensions
33
import kotlin.math.abs
44

55
data class Coordinates(val row: Int, val col: Int) {
6+
7+
companion object {
8+
val ZERO = Coordinates(0, 0)
9+
}
610
operator fun plus(other: Coordinates) = Coordinates(row + other.row, col + other.col)
711
operator fun minus(other: Coordinates) = Coordinates(row - other.row, col - other.col)
812

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.ronny_h.aoc.year2016.day01
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.Coordinates
5+
import de.ronny_h.aoc.extensions.Coordinates.Companion.ZERO
6+
import de.ronny_h.aoc.extensions.Direction.NORTH
7+
8+
fun main() = NoTimeForATaxicab().run(239, 141)
9+
10+
class NoTimeForATaxicab : AdventOfCode<Int>(2016, 1) {
11+
override fun part1(input: List<String>): Int = input.parseInstructions().followInstructions() taxiDistanceTo ZERO
12+
13+
private fun List<String>.parseInstructions(): List<String> = first().split(", ")
14+
15+
private fun List<String>.followInstructions(stopAtFirstLocationVisitedTwice: Boolean = false): Coordinates {
16+
var position = ZERO
17+
var direction = NORTH
18+
val visited = mutableSetOf(position)
19+
20+
forEach {
21+
val turn = it.first()
22+
val steps = it.substring(1).toInt()
23+
24+
direction = when (turn) {
25+
'L' -> direction.turnLeft()
26+
'R' -> direction.turnRight()
27+
else -> error("Invalid turn: $turn")
28+
}
29+
30+
repeat(steps) {
31+
position += direction
32+
if (stopAtFirstLocationVisitedTwice) {
33+
if (!visited.add(position)) {
34+
return position
35+
}
36+
}
37+
}
38+
}
39+
return position
40+
}
41+
42+
override fun part2(input: List<String>): Int =
43+
input.parseInstructions().followInstructions(true) taxiDistanceTo ZERO
44+
}

src/test/kotlin/de/ronny_h/aoc/extensions/CoordinatesTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class CoordinatesTest : StringSpec({
4242
}
4343
}
4444

45+
"Coordinates of ZERO" {
46+
Coordinates.ZERO shouldBe Coordinates(0, 0)
47+
}
48+
4549
"Add a direction" {
4650
forAll(
4751
row(Coordinates(5, 5), NORTH, Coordinates(4, 5)),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package de.ronny_h.aoc.year2016.day01
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.matchers.shouldBe
5+
6+
class NoTimeForATaxicabTest : StringSpec({
7+
8+
"part 1, small example" {
9+
NoTimeForATaxicab().part1(listOf("R2, L3")) shouldBe 5
10+
}
11+
12+
"part 1, medium example" {
13+
NoTimeForATaxicab().part1(listOf("R2, R2, R2")) shouldBe 2
14+
}
15+
16+
"part 1, large example" {
17+
NoTimeForATaxicab().part1(listOf("R5, L5, R5, R3")) shouldBe 12
18+
}
19+
20+
"part 1, negative x, positive y coordinate" {
21+
NoTimeForATaxicab().part1(listOf("L1, R1")) shouldBe 2
22+
}
23+
24+
"part 2: the first location visited twice" {
25+
NoTimeForATaxicab().part2(listOf("R8, R4, R4, R8")) shouldBe 4
26+
}
27+
})

0 commit comments

Comments
 (0)