Skip to content

Commit 3bd46e0

Browse files
committed
Solution 2015-03 (Perfectly Spherical Houses in a Vacuum)
1 parent 4da49b0 commit 3bd46e0

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package de.ronny_h.aoc.year2015.day03
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.Coordinates
5+
import de.ronny_h.aoc.extensions.Direction
6+
7+
fun main() = PerfectlySphericalHouses().run(2592, 2360)
8+
9+
class PerfectlySphericalHouses : AdventOfCode<Int>(2015, 3) {
10+
override fun part1(input: List<String>): Int {
11+
require(input.size == 1)
12+
var position = Coordinates(0, 0)
13+
val visited = mutableSetOf<Coordinates>(position)
14+
input.parseDirections()
15+
.forEach {
16+
position += it
17+
visited.add(position)
18+
}
19+
return visited.size
20+
}
21+
22+
override fun part2(input: List<String>): Int {
23+
require(input.size == 1)
24+
var santaPosition = Coordinates(0, 0)
25+
var robotSantaPosition = santaPosition
26+
val visited = mutableSetOf<Coordinates>(santaPosition)
27+
input.parseDirections()
28+
.forEachIndexed { i, direction ->
29+
if (i % 2 == 0) {
30+
robotSantaPosition += direction
31+
visited.add(robotSantaPosition)
32+
} else {
33+
santaPosition += direction
34+
visited.add(santaPosition)
35+
}
36+
}
37+
return visited.size
38+
}
39+
40+
private fun List<String>.parseDirections() = first()
41+
.map {
42+
when (it) {
43+
'^' -> Direction.NORTH
44+
'v' -> Direction.SOUTH
45+
'>' -> Direction.EAST
46+
'<' -> Direction.WEST
47+
else -> error("invalid direction: $it")
48+
}
49+
}
50+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.ronny_h.aoc.year2015.day03
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.matchers.shouldBe
5+
6+
class PerfectlySphericalHousesTest : StringSpec({
7+
8+
"part 1: Santa delivers at least one present to a number of houses" {
9+
PerfectlySphericalHouses().part1(listOf(">")) shouldBe 2
10+
PerfectlySphericalHouses().part1(listOf("^>v<")) shouldBe 4
11+
PerfectlySphericalHouses().part1(listOf("^v^v^v^v^v")) shouldBe 2
12+
}
13+
14+
"part 2: Santa and Robo-Santa deliver at least one present to a number of houses" {
15+
PerfectlySphericalHouses().part2(listOf("^v")) shouldBe 3
16+
PerfectlySphericalHouses().part2(listOf("^>v<")) shouldBe 3
17+
PerfectlySphericalHouses().part2(listOf("^v^v^v^v^v")) shouldBe 11
18+
}
19+
})

0 commit comments

Comments
 (0)