Skip to content

Commit 6d296f1

Browse files
committed
Solution 2025-01 (Secret Entrance)
1 parent 34be8c6 commit 6d296f1

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package de.ronny_h.aoc.year2025.day01
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.year2025.day01.RotationDirection.LEFT
5+
import de.ronny_h.aoc.year2025.day01.RotationDirection.RIGHT
6+
import kotlin.math.abs
7+
8+
fun main() = SecretEntrance().run(1177, 6768)
9+
10+
class SecretEntrance : AdventOfCode<Int>(2025, 1) {
11+
12+
override fun part1(input: List<String>): Int {
13+
var count = 0
14+
var position = 50
15+
input.parse().forEach {
16+
val rotated = position.rotate(it)
17+
val newPosition = rotated.mod(100)
18+
if (newPosition == 0) {
19+
count++
20+
}
21+
position = newPosition
22+
}
23+
return count
24+
}
25+
26+
override fun part2(input: List<String>): Int {
27+
var count = 0
28+
var position = 50
29+
input.parse().forEach {
30+
val rotated = position.rotate(it)
31+
val newPosition = rotated.mod(100)
32+
val fullRounds = abs(rotated).div(100)
33+
if (newPosition == 0 && fullRounds == 0) {
34+
count++
35+
}
36+
count += fullRounds
37+
if (position != 0 && rotated < 0) {
38+
count++
39+
}
40+
position = newPosition
41+
}
42+
return count
43+
}
44+
45+
private fun Int.rotate(rotation: Rotation): Int = when (rotation.direction) {
46+
LEFT -> this - rotation.amount
47+
RIGHT -> this + rotation.amount
48+
}
49+
}
50+
51+
fun List<String>.parse(): List<Rotation> = map {
52+
val direction = when (it.first()) {
53+
'L' -> LEFT
54+
'R' -> RIGHT
55+
else -> error("unknown direction: $it")
56+
}
57+
Rotation(direction, it.substring(1).toInt())
58+
}
59+
60+
data class Rotation(val direction: RotationDirection, val amount: Int)
61+
62+
enum class RotationDirection { LEFT, RIGHT }
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package de.ronny_h.aoc.year2025.day01
2+
3+
import de.ronny_h.aoc.extensions.asList
4+
import de.ronny_h.aoc.year2025.day01.RotationDirection.LEFT
5+
import de.ronny_h.aoc.year2025.day01.RotationDirection.RIGHT
6+
import io.kotest.core.spec.style.StringSpec
7+
import io.kotest.matchers.shouldBe
8+
9+
class SecretEntranceTest : StringSpec({
10+
11+
"input can be parsed" {
12+
val input = """
13+
L68
14+
L30
15+
R48
16+
""".asList()
17+
18+
input.parse() shouldBe listOf(
19+
Rotation(LEFT, 68),
20+
Rotation(LEFT, 30),
21+
Rotation(RIGHT, 48),
22+
)
23+
}
24+
25+
val input = """
26+
L68
27+
L30
28+
R48
29+
L5
30+
R60
31+
L55
32+
L1
33+
L99
34+
R14
35+
L82
36+
""".asList()
37+
38+
"part 1: the password to open the door" {
39+
SecretEntrance().part1(input) shouldBe 3
40+
}
41+
42+
"part 2: the password to open the door with method 0x434C49434B" {
43+
SecretEntrance().part2(input) shouldBe 6
44+
}
45+
46+
"part 2 with 2x overflow" {
47+
SecretEntrance().part2(listOf("L200")) shouldBe 2
48+
}
49+
50+
"part 2 with 2x underflow" {
51+
SecretEntrance().part2(listOf("R200")) shouldBe 2
52+
}
53+
54+
"part 2 with an exact 0 and 2x overflow" {
55+
SecretEntrance().part2(listOf("L50", "L200")) shouldBe 3
56+
}
57+
58+
"part 2 with an exact 0 and 2x underflow" {
59+
SecretEntrance().part2(listOf("L50", "R200")) shouldBe 3
60+
}
61+
62+
"part 2 with an exact 0 and 2x overflow not landing on 0" {
63+
SecretEntrance().part2(listOf("L50", "L199")) shouldBe 2
64+
}
65+
66+
"part 2 with an exact 0 and 2x underflow not landing on 0" {
67+
SecretEntrance().part2(listOf("L50", "R199")) shouldBe 2
68+
}
69+
})

0 commit comments

Comments
 (0)