Skip to content

Commit 28def53

Browse files
committed
Day06 - Part 2
1 parent 0ee28ff commit 28def53

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/main/kotlin/Day06.kt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
import kotlin.io.path.readLines
22

33

4-
54
class Day06 : Day {
6-
override fun partOne(filename: String, verbose: Boolean): Long {
7-
val lines = filename.asPath().readLines()
8-
val numbers = lines.dropLast(1)
9-
.mapToArray { line ->
5+
override fun partOne(filename: String, verbose: Boolean): Long =
6+
calculate(filename, verbose) { lines ->
7+
lines.mapToArray { line ->
108
Regex("""\d+""")
119
.findAll(line)
1210
.map { it.value.toLong() }
1311
.toList()
1412
.toTypedArray()
1513
}.rotate()
14+
}
15+
16+
override fun partTwo(filename: String, verbose: Boolean): Long =
17+
calculate(filename, verbose) { lines ->
18+
val maxLength = lines.maxOf { it.length }
19+
lines.map { it.padEnd(maxLength) }
20+
.parseMatrix()
21+
.rotate()
22+
.fold(mutableListOf(mutableListOf<Long>())) { problems, chars ->
23+
val line = String(chars.toCharArray())
24+
if (line.isBlank()) {
25+
problems += mutableListOf<Long>()
26+
} else {
27+
problems.last() += line.trim().toLong()
28+
}
29+
problems
30+
}.mapToArray { it.toTypedArray() }
31+
}
32+
33+
private fun calculate(filename: String, verbose: Boolean, parseNumbers: (List<String>) -> Array<Array<Long>>): Long {
34+
val lines = filename.asPath().readLines()
35+
val numbers = parseNumbers(lines.dropLast(1))
1636

1737
val operators = lines.last()
1838
.let { line ->
@@ -38,10 +58,7 @@ class Day06 : Day {
3858
else -> throw IllegalArgumentException("Unhandled operator")
3959
}
4060
}
41-
}
4261

43-
override fun partTwo(filename: String, verbose: Boolean): Any {
44-
TODO("Not yet implemented")
4562
}
4663

4764
companion object : Day.Main("Day06.txt") {

src/main/kotlin/Matrix.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ internal inline fun <reified T> Path.parseMatrix(charTransformer: (Char) -> T):
3131
internal inline fun <reified T> String.parseMatrix(charTransformer: (Char) -> T): Matrix<T> =
3232
lines().mapToArray { it.mapToArray(charTransformer) }
3333

34+
internal inline fun <reified T> List<String>.parseMatrix(charTransformer: (Char) -> T): Matrix<T> =
35+
mapToArray { it.mapToArray(charTransformer) }
36+
3437
fun Path.parseMatrix() =
3538
parseMatrix { it }
3639

3740
fun String.parseMatrix() =
3841
parseMatrix { it }
3942

43+
fun List<String>.parseMatrix() =
44+
parseMatrix { it }
45+
4046
fun <T> Matrix<T>.getOrthogonalNeighbors(coordinate: Coordinate) =
4147
listOf(
4248
Coordinate(coordinate.x, coordinate.y - 1),

src/test/kotlin/Day06Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Day06Test : DayTest<Day06>("Day06_test.txt") {
22
override val partOneExpected = 4277556L
3-
override val partTwoExpected = null
3+
override val partTwoExpected = 3263827L
44
}

0 commit comments

Comments
 (0)