Skip to content

Commit 965fc44

Browse files
committed
Solution 2018-13, part 2 (Mine Cart Madness)
1 parent 3fd01cb commit 965fc44

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/main/kotlin/de/ronny_h/aoc/year2018/day13/MineCartMadness.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import de.ronny_h.aoc.extensions.grids.SimpleCharGrid
99
import de.ronny_h.aoc.extensions.grids.Turn
1010
import de.ronny_h.aoc.extensions.grids.Turn.*
1111

12-
fun main() = MineCartMadness().run("33,69", "")
12+
fun main() = MineCartMadness().run("33,69", "135,9")
1313

1414
class MineCartMadness : AdventOfCode<String>(2018, 13) {
1515
override fun part1(input: List<String>): String {
@@ -18,12 +18,13 @@ class MineCartMadness : AdventOfCode<String>(2018, 13) {
1818
}
1919

2020
override fun part2(input: List<String>): String {
21-
return ""
21+
val remaining = Track(input).moveCartsUntilOnlyOneRemains()
22+
return "${remaining.col},${remaining.row}"
2223
}
2324
}
2425

2526
class Track(input: List<String>) : SimpleCharGrid(input, ' ') {
26-
private val carts: List<Cart> = buildList {
27+
private val carts: MutableList<Cart> = buildList {
2728
forEachCoordinates { position, element ->
2829
when (element) {
2930
'<' -> {
@@ -47,24 +48,40 @@ class Track(input: List<String>) : SimpleCharGrid(input, ' ') {
4748
}
4849
}
4950
}.last()
50-
}
51+
}.toMutableList()
5152

5253
fun moveCartsUntilFirstCrash(): Coordinates {
5354
var collision: Coordinates? = null
5455
while (collision == null) {
55-
collision = tick()
56+
collision = tickUntilFirstCollision()
5657
}
5758
return collision
5859
}
5960

60-
private fun tick(): Coordinates? {
61+
fun moveCartsUntilOnlyOneRemains(): Coordinates {
62+
while (carts.size != 1) {
63+
tickRemovingCollidingCarts()
64+
}
65+
return carts.single().position
66+
}
67+
68+
private fun tickUntilFirstCollision(): Coordinates? {
6169
for (cart in carts.sortedBy { it.position }) {
6270
cart.move()
6371
carts.firstDuplicate(Cart::position)?.also { return it }
6472
}
6573
return null
6674
}
6775

76+
private fun tickRemovingCollidingCarts() {
77+
for (cart in carts.sortedBy { it.position }) {
78+
cart.move()
79+
carts.firstDuplicate(Cart::position)?.also { crashed ->
80+
carts.removeAll { it.position == crashed }
81+
}
82+
}
83+
}
84+
6885
private fun Cart.move() {
6986
position += direction
7087
direction = when (getAt(position)) {

src/test/kotlin/de/ronny_h/aoc/year2018/day13/MineCartMadnessTest.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import io.kotest.matchers.shouldBe
66

77
class MineCartMadnessTest : StringSpec({
88

9-
val input = """
9+
"part 1: the location of the first crash" {
10+
val input = """
1011
/->-\
1112
| | /----\
1213
| /-+--+-\ |
@@ -15,12 +16,19 @@ class MineCartMadnessTest : StringSpec({
1516
\------/
1617
""".asList()
1718

18-
"part 1: the location of the first crash" {
1919
MineCartMadness().part1(input) shouldBe "7,3"
2020
}
2121

22-
"part 2" {
23-
val input = listOf("")
24-
MineCartMadness().part2(input) shouldBe ""
22+
"part 2: the location of the last cart" {
23+
val input = """
24+
/>-<\
25+
| |
26+
| /<+-\
27+
| | | v
28+
\>+</ |
29+
| ^
30+
\<->/
31+
""".asList()
32+
MineCartMadness().part2(input) shouldBe "6,4"
2533
}
2634
})

0 commit comments

Comments
 (0)