Skip to content

Commit e92f34a

Browse files
committed
2025, Day 6
1 parent 2ed3f2a commit e92f34a

File tree

8 files changed

+216
-2
lines changed

8 files changed

+216
-2
lines changed

.idea/runConfigurations/2025__Day_6.xml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/lv/esupe/aoc/utils/ListUtil.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,17 @@ fun <T> List<T>.allEqual(): Boolean {
119119
if (isEmpty()) return false
120120
val first = first()
121121
return drop(1).all { it == first }
122-
}
122+
}
123+
124+
fun <T> List<List<T>>.transposed(): List<List<T>> {
125+
require(isNotEmpty()) { "Matrix must not be empty." }
126+
val rowCount = size
127+
val colCount = first().size
128+
require(all { it.size == colCount }) { "All rows must have the same length." }
129+
130+
return List(colCount) { col ->
131+
List(rowCount) { row ->
132+
elementAt(row).elementAt(col)
133+
}
134+
}
135+
}

src/main/kotlin/lv/esupe/aoc/utils/String.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,29 @@ inline fun String.charByChar(other: String, crossinline block: (Char?, Char?) ->
1313

1414
fun String.count(char: Char): Int = count { it == char }
1515

16-
fun String.findAllInts(): List<Int> = NUMBER_REGEX.findAll(this).map { it.value.toInt() }.toList()
16+
fun String.findAllInts(): List<Int> = NUMBER_REGEX.findAll(this).map { it.value.toInt() }.toList()
17+
18+
fun List<String>.transposed(): List<String> {
19+
require(isNotEmpty()) { "Input must not be empty." }
20+
val width = first().length
21+
require(all { it.length == width }) { "All strings must have the same length." }
22+
23+
return List(width) { col ->
24+
buildString(size) {
25+
for (row in this@transposed.indices) {
26+
this.append(this@transposed[row][col])
27+
}
28+
}
29+
}
30+
}
31+
32+
fun List<String>.rotated90(ccw: Boolean): List<String> {
33+
return if (ccw) {
34+
transposed().reversed()
35+
} else {
36+
transposed().map { it.reversed() }
37+
}
38+
}
39+
40+
fun List<String>.rotated180(): List<String> =
41+
reversed().map { it.reversed() }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lv.esupe.aoc.year2025
2+
3+
import lv.esupe.aoc.Puzzle
4+
import lv.esupe.aoc.solve
5+
import lv.esupe.aoc.utils.chunkedBy
6+
import lv.esupe.aoc.utils.transposed
7+
8+
fun main() = solve { Day6() }
9+
10+
class Day6 : Puzzle<Long, Long>(2025, 6) {
11+
override val input = rawInput
12+
13+
override fun solvePartOne(): Long {
14+
val lines = input.map { line -> line.trim().split(Regex("\\s+")) }
15+
val operands = (0 until lines.lastIndex)
16+
.map { row -> lines[row].map { it.toLong() } }
17+
val ops = lines.last()
18+
19+
return ops.indices
20+
.sumOf { idx ->
21+
val op: Long.(Long) -> Long = if (ops[idx] == "*") Long::times else Long::plus
22+
operands
23+
.map { it[idx] }
24+
.reduce(op)
25+
}
26+
}
27+
28+
override fun solvePartTwo(): Long {
29+
return input.transposed()
30+
.chunkedBy { it.isBlank() }
31+
.sumOf { lines ->
32+
val op: Long.(Long) -> Long = if (lines.first().last() == '*') Long::times else Long::plus
33+
lines
34+
.map { line -> line.extractNumber() }
35+
.reduce(op)
36+
}
37+
}
38+
39+
private fun String.extractNumber(): Long {
40+
return fold(0) { acc, i ->
41+
if (i.isDigit()) acc * 10 + i.digitToInt() else acc
42+
}
43+
}
44+
}

src/test/kotlin/lv/esupe/aoc/utils/ListTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,23 @@ class ListTest {
3131
assertNull(list.atOrNull(-4))
3232
}
3333
}
34+
35+
@Nested
36+
inner class Transpose {
37+
@Test
38+
fun `transpose correctly transposes a matrix`() {
39+
val input = listOf(
40+
listOf('0', '1', '2'),
41+
listOf('3', '4', '5'),
42+
listOf('6', '7', '8'),
43+
listOf('9', 'a', 'b')
44+
)
45+
val expected = listOf(
46+
listOf('0', '3', '6', '9'),
47+
listOf('1', '4', '7', 'a'),
48+
listOf('2', '5', '8', 'b')
49+
)
50+
assertEquals(expected, input.transposed())
51+
}
52+
}
3453
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package lv.esupe.aoc.utils
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Nested
5+
import org.junit.jupiter.api.Test
6+
7+
class StringTest {
8+
@Nested
9+
inner class Transposed {
10+
@Test
11+
fun `transposed correctly transforms list of Strings`() {
12+
val input = listOf(
13+
"012",
14+
"345",
15+
"678",
16+
"9ab"
17+
)
18+
val expected = listOf(
19+
"0369",
20+
"147a",
21+
"258b"
22+
)
23+
assertEquals(expected, input.transposed())
24+
}
25+
}
26+
27+
@Nested
28+
inner class Rotated90 {
29+
@Test
30+
fun `Rotated90 correctly rotates list of Strings clockwise`() {
31+
val input = listOf(
32+
"012",
33+
"345",
34+
"678",
35+
"9ab"
36+
)
37+
val expected = listOf(
38+
"9630",
39+
"a741",
40+
"b852"
41+
)
42+
assertEquals(expected, input.rotated90(ccw = false))
43+
}
44+
45+
@Test
46+
fun `Rotated90 correctly rotates list of Strings counter-clockwise`() {
47+
val input = listOf(
48+
"012",
49+
"345",
50+
"678",
51+
"9ab"
52+
)
53+
val expected = listOf(
54+
"258b",
55+
"147a",
56+
"0369"
57+
)
58+
assertEquals(expected, input.rotated90(ccw = true))
59+
}
60+
}
61+
62+
@Nested
63+
inner class Rotated180 {
64+
@Test
65+
fun `Rotated180 correctly rotates list of Strings`() {
66+
val input = listOf(
67+
"012",
68+
"345",
69+
"678",
70+
"9ab"
71+
)
72+
val expected = listOf(
73+
"ba9",
74+
"876",
75+
"543",
76+
"210"
77+
)
78+
assertEquals(expected, input.rotated180())
79+
}
80+
}
81+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lv.esupe.aoc.year2025
2+
3+
import lv.esupe.aoc.DayTest
4+
import org.junit.jupiter.api.Test
5+
6+
class Day6Test : DayTest<Long, Long>() {
7+
override val puzzle = { Day6() }
8+
9+
@Test
10+
fun day6_1() {
11+
runTest("_1", 4277556, 3263827)
12+
}
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
123 328 51 64
2+
45 64 387 23
3+
6 98 215 314
4+
* + * +

0 commit comments

Comments
 (0)