Skip to content

Commit 29b66b9

Browse files
committed
Clean up day 15
1 parent 1c81e6d commit 29b66b9

File tree

3 files changed

+38
-48
lines changed

3 files changed

+38
-48
lines changed

src/main/kotlin/day15/Day15.kt

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import kotlin.collections.component1
1111
import kotlin.collections.component2
1212

1313
fun solveA(text: String, debug: Debug = Debug.Disabled): Int {
14-
val (mapText, instructionsText) = text.split("\n\n")
15-
val map = mapText.lines()
16-
val instructions = instructionsText.replace("\n", "")
14+
val (map, instructions) = text.split("\n\n").let { (first, second) ->
15+
first.lines() to second.replace("\n", "")
16+
}
17+
1718
val walls = mutableSetOf<Point>()
1819
val boxes = mutableSetOf<Point>()
1920
var robot = map.indexOf('@')
@@ -26,31 +27,18 @@ fun solveA(text: String, debug: Debug = Debug.Disabled): Int {
2627
}
2728
}
2829

29-
3030
instructions.forEach { c ->
31-
val direction = when (c) {
32-
'<' -> Direction.LEFT
33-
'>' -> Direction.RIGHT
34-
'^' -> Direction.UP
35-
'v' -> Direction.DOWN
36-
else -> TODO("Unknown direction $c")
31+
val direction = Direction.parse(c)
32+
val boxesToMove = mutableSetOf<Point>()
33+
var nextPoint = robot + direction.point
34+
while (nextPoint in boxes) {
35+
boxesToMove.add(nextPoint)
36+
nextPoint += direction.point
3737
}
38-
39-
val nextPoint = robot + direction.point
40-
if (nextPoint !in walls && nextPoint !in boxes) {
41-
robot = nextPoint
42-
} else {
43-
val boxesToMove = mutableSetOf<Point>()
44-
var nextBox = nextPoint
45-
while (nextBox in boxes) {
46-
boxesToMove.add(nextBox)
47-
nextBox += direction.point
48-
}
49-
if (nextBox !in walls) {
50-
boxes.removeAll(boxesToMove)
51-
boxes.addAll(boxesToMove.map { it + direction.point })
52-
robot = nextPoint
53-
}
38+
if (nextPoint !in walls) {
39+
boxes.removeAll(boxesToMove)
40+
boxes.addAll(boxesToMove.map { it + direction.point })
41+
robot += direction.point
5442
}
5543
}
5644

@@ -59,11 +47,12 @@ fun solveA(text: String, debug: Debug = Debug.Disabled): Int {
5947

6048

6149
fun solveB(text: String, debug: Debug = Debug.Disabled): Int {
62-
val (mapText, instructionsText) = text.split("\n\n")
63-
val map = mapText.lines()
50+
val (map, instructions) = text.split("\n\n").let { (first, second) ->
51+
first.lines() to second.replace("\n", "")
52+
}
53+
6454
val width = map[0].length * 2
6555
val height = map.size
66-
val instructions = instructionsText.replace("\n", "")
6756
val walls = mutableSetOf<Point>()
6857
val boxes = mutableSetOf<Pair<Point, Point>>()
6958

@@ -91,18 +80,14 @@ fun solveB(text: String, debug: Debug = Debug.Disabled): Int {
9180
}
9281

9382
instructions.forEachIndexed { i, c ->
94-
val direction = when (c) {
95-
'<' -> Direction.LEFT
96-
'>' -> Direction.RIGHT
97-
'^' -> Direction.UP
98-
'v' -> Direction.DOWN
99-
else -> throw IllegalArgumentException("Unknown direction $c")
100-
}
83+
val direction = Direction.parse(c)
10184

10285
val boxesToMove = mutableSetOf<Pair<Point, Point>>()
103-
val pointsToVisit = mutableSetOf(robot + direction.point)
104-
while (pointsToVisit.isNotEmpty() && pointsToVisit.first() !in walls) {
105-
val nextBoxPoint = pointsToVisit.removeFirst()
86+
val boxPointsToVisit = mutableSetOf(robot + direction.point)
87+
while (boxPointsToVisit.isNotEmpty() && boxPointsToVisit.first() !in walls) {
88+
val nextBoxPoint = boxPointsToVisit.removeFirst()
89+
//For left and right only one of these is possible.
90+
//But for the sake of simplicity, we just search for both
10691
val possibleBoxes = setOf(
10792
Pair(nextBoxPoint + Direction.LEFT.point, nextBoxPoint),
10893
Pair(nextBoxPoint, nextBoxPoint + Direction.RIGHT.point)
@@ -112,16 +97,16 @@ fun solveB(text: String, debug: Debug = Debug.Disabled): Int {
11297
boxesToMove.add(box)
11398
when (direction) {
11499
Direction.UP, Direction.DOWN -> {
115-
pointsToVisit.add(box.first + direction.point)
116-
pointsToVisit.add(box.second + direction.point)
100+
boxPointsToVisit.add(box.first + direction.point)
101+
boxPointsToVisit.add(box.second + direction.point)
117102
}
118103

119-
Direction.LEFT -> pointsToVisit.add(box.first + direction.point)
120-
else -> pointsToVisit.add(box.second + direction.point)
104+
Direction.LEFT -> boxPointsToVisit.add(box.first + direction.point)
105+
else -> boxPointsToVisit.add(box.second + direction.point)
121106
}
122107
}
123108
}
124-
if (pointsToVisit.none { it in walls }) {
109+
if (boxPointsToVisit.none { it in walls }) {
125110
boxes.removeAll(boxesToMove)
126111
boxes.addAll(boxesToMove.map { (first, second) -> first + direction.point to second + direction.point })
127112
robot += direction.point

src/main/kotlin/helper/point/Direction.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ enum class Direction(val char: Char, val orientation: Orientation) {
4747
val EAST = RIGHT
4848
val SOUTH = DOWN
4949
val WEST = LEFT
50+
51+
fun parse(c: Char) = when (c) {
52+
'<' -> Direction.LEFT
53+
'>' -> Direction.RIGHT
54+
'^' -> Direction.UP
55+
'v' -> Direction.DOWN
56+
else -> throw IllegalArgumentException("Unknown direction $c")
57+
}
58+
5059
}
5160
}
5261

src/test/kotlin/day15/Day15KtTest.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ internal class Day15KtTest {
4242

4343
@Test
4444
fun sample2() {
45-
// val text = readDayFile(day, "sample2.in").readText().trimEnd()
46-
4745
val text = """
4846
########
4947
#..O.O.#
@@ -62,8 +60,6 @@ internal class Day15KtTest {
6260

6361
@Test
6462
fun sample3() {
65-
// val text = readDayFile(day, "sample2.in").readText().trimEnd()
66-
6763
val text = """
6864
#######
6965
#...#.#

0 commit comments

Comments
 (0)