Skip to content

Commit 5cce81c

Browse files
committed
Solved day13 part 1
1 parent 7310546 commit 5cce81c

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.12.1'
7+
version '0.12.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day13/src/main/kotlin/de/havox_design/aoc2023/day13/Day13.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ package de.havox_design.aoc2023.day13
22

33
class Day13(private var filename: String) {
44
fun solvePart1(): Long =
5-
405L
5+
makePatterns(getResourceAsText(filename))
6+
.sumOf { it.mirrorValue() }
7+
.toLong()
68

79
fun solvePart2(): Long =
810
0L
911

12+
private fun makePatterns(input: List<String>): List<Pattern> {
13+
return input
14+
.withIndex()
15+
.filter { (_, line) -> line.isEmpty() }
16+
.map { it.index }
17+
.let { listOf(-1) + it + (input.lastIndex + 1) }
18+
.windowed(2)
19+
.map { (before, after) -> input.subList(before + 1, after) }
20+
.map(::Pattern)
21+
}
22+
1023
private fun getResourceAsText(path: String): List<String> =
1124
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
1225
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package de.havox_design.aoc2023.day13
2+
3+
data class Pattern(val rows: List<String>) {
4+
private val numColumns = rows[0].length
5+
6+
override fun toString(): String {
7+
return rows
8+
.joinToString("\n")
9+
}
10+
11+
private fun column(index: Int): String {
12+
return rows
13+
.map { it[index] }
14+
.joinToString("")
15+
}
16+
17+
fun mirrorValue(pleaseDoNotBe: Int = 0): Int {
18+
val rowScores = (0..<rows.lastIndex)
19+
.filter(::isMirrorRow)
20+
.map { (it + 1) * 100 }
21+
val columnScores = (0..<numColumns)
22+
.filter(::isMirrorColumn)
23+
.map { it + 1 }
24+
25+
return (rowScores + columnScores)
26+
.first { it != pleaseDoNotBe }
27+
}
28+
29+
@SuppressWarnings("kotlin:S6510")
30+
private fun isMirrorRow(index: Int): Boolean {
31+
var top = index
32+
var bottom = index + 1
33+
34+
when {
35+
bottom > rows.lastIndex -> return false
36+
else -> {
37+
while (top >= 0 && bottom <= rows.lastIndex) {
38+
when {
39+
rows[top] != rows[bottom] -> {
40+
return false
41+
}
42+
43+
else -> {
44+
top--
45+
bottom++
46+
}
47+
}
48+
}
49+
return true
50+
}
51+
}
52+
}
53+
54+
@SuppressWarnings("kotlin:S6510")
55+
private fun isMirrorColumn(index: Int): Boolean {
56+
var left = index
57+
var right = index + 1
58+
59+
when (right) {
60+
numColumns -> return false
61+
else -> {
62+
while (left >= 0 && right < numColumns) {
63+
when {
64+
column(left) != column(right) -> {
65+
return false
66+
}
67+
68+
else -> {
69+
left--
70+
right++
71+
}
72+
}
73+
}
74+
return true
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)