Skip to content

Commit 61cc9d7

Browse files
committed
Day 25
1 parent 9b2bf62 commit 61cc9d7

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/main/kotlin/y24/Day25.kt

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package y24
2+
3+
import common.puzzle.solvePuzzle
4+
import common.puzzle.Input
5+
import common.puzzle.Puzzle
6+
import common.datastructures.*
7+
import common.ext.*
8+
import common.util.*
9+
import java.util.*
10+
import kotlin.math.*
11+
import kotlin.system.exitProcess
12+
13+
14+
fun main() = solvePuzzle(year = 2024, day = 25) { Day25(it) }
15+
16+
class Day25(val input: Input) : Puzzle {
17+
18+
private fun parseInput(lines: List<String>): Pair<List<List<Int>>, List<List<Int>>> {
19+
val locks = mutableListOf<List<Int>>()
20+
val keys = mutableListOf<List<Int>>()
21+
22+
var i = 0
23+
while (i < lines.size) {
24+
if (lines[i].all { it == '#' }) {
25+
locks += sequence(lines.subList(i, i + 7))
26+
} else {
27+
keys += sequence(lines.subList(i, i + 7).reversed())
28+
}
29+
i += 8
30+
}
31+
32+
return locks to keys
33+
}
34+
35+
private fun sequence(lines: List<String>): List<Int> {
36+
return IntArray(lines[0].length) { i ->
37+
lines.indexOfFirst { it[i] == '.' } - 1
38+
}.toList()
39+
}
40+
41+
private fun fits(lock: List<Int>, key: List<Int>): Boolean {
42+
return lock.indices.all { i -> lock[i] + key[i] < 6 }
43+
}
44+
45+
override fun solveLevel1(): Any {
46+
val (locks, keys) = parseInput(input.lines)
47+
return locks.sumOf { lock ->
48+
keys.count { key ->
49+
fits(lock, key)
50+
}
51+
}
52+
}
53+
54+
override fun solveLevel2(): Any {
55+
TODO()
56+
}
57+
}

src/test/kotlin/y24/Day25Test.kt

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package y24
2+
3+
import common.puzzle.Input
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Test
6+
7+
internal class Day25Test {
8+
private val sample = Input("""
9+
#####
10+
.####
11+
.####
12+
.####
13+
.#.#.
14+
.#...
15+
.....
16+
17+
#####
18+
##.##
19+
.#.##
20+
...##
21+
...#.
22+
...#.
23+
.....
24+
25+
.....
26+
#....
27+
#....
28+
#...#
29+
#.#.#
30+
#.###
31+
#####
32+
33+
.....
34+
.....
35+
#.#..
36+
###..
37+
###.#
38+
###.#
39+
#####
40+
41+
.....
42+
.....
43+
.....
44+
#....
45+
#.#..
46+
#.#.#
47+
#####
48+
""".trimIndent())
49+
50+
private val day = Day25(sample)
51+
52+
@Test
53+
fun solveLevel1() {
54+
assertEquals(3, day.solveLevel1())
55+
}
56+
57+
@Test
58+
fun solveLevel2() {
59+
// assertEquals(, day.solveLevel2())
60+
}
61+
}

0 commit comments

Comments
 (0)