Skip to content

Commit 775853f

Browse files
committed
Solved day13 part 2
1 parent 440a2ea commit 775853f

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
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.3'
7+
version '0.12.4'
88

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class Day13(private var filename: String) {
77
.toLong()
88

99
fun solvePart2(): Long =
10-
400L
10+
makePatterns(getResourceAsText(filename))
11+
.sumOf { it.smudgeMirrorValue() }
12+
.toLong()
1113

1214
private fun makePatterns(input: List<String>): List<Pattern> {
1315
return input

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

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

33
data class Pattern(val rows: List<String>) {
4+
private val ICON_ASH = '.'
5+
private val ICON_ROCK = '#'
46
private val numColumns = rows[0].length
57

68
override fun toString(): String {
@@ -22,10 +24,28 @@ data class Pattern(val rows: List<String>) {
2224
.filter(::isMirrorColumn)
2325
.map { it + 1 }
2426

25-
return (rowScores + columnScores)
27+
return (rowScores + columnScores + 0)
2628
.first { it != pleaseDoNotBe }
2729
}
2830

31+
fun smudgeMirrorValue(): Int {
32+
val originalValue = mirrorValue()
33+
34+
for (rowIndex in rows.indices) {
35+
for (columnIndex in rows[0].indices) {
36+
val pattern = Pattern(rows.flipMirror(rowIndex, columnIndex))
37+
val newValue = pattern.mirrorValue(originalValue)
38+
39+
when {
40+
newValue != 0 && newValue != originalValue -> {
41+
return newValue
42+
}
43+
}
44+
}
45+
}
46+
return originalValue
47+
}
48+
2949
@SuppressWarnings("kotlin:S6510")
3050
private fun isMirrorRow(index: Int): Boolean {
3151
var top = index
@@ -75,4 +95,16 @@ data class Pattern(val rows: List<String>) {
7595
}
7696
}
7797
}
98+
99+
private fun List<String>.flipMirror(rowIndex: Int, columnIndex: Int): List<String> {
100+
val mutableList = this.toMutableList()
101+
val rowToReplace = mutableList[rowIndex].toMutableList()
102+
rowToReplace[columnIndex] = when {
103+
rowToReplace[columnIndex] == ICON_ASH -> ICON_ROCK
104+
else -> ICON_ASH
105+
}
106+
mutableList[rowIndex] = rowToReplace.joinToString("")
107+
108+
return mutableList.toList()
109+
}
78110
}

day13/src/test/kotlin/de/havox_design/aoc2023/day13/Day13Test.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class Day13Test {
2727
@JvmStatic
2828
private fun getDataForTestSolvePart1(): Stream<Arguments> =
2929
Stream.of(
30-
Arguments.of("part1sample.txt", 405L)
30+
Arguments.of("sample.txt", 405L)
3131
)
3232

3333
@JvmStatic
3434
private fun getDataForTestSolvePart2(): Stream<Arguments> =
3535
Stream.of(
36-
Arguments.of("part2sample1.txt", 400L)
36+
Arguments.of("sample.txt", 400L)
3737
)
3838
}
3939
}

day13/src/test/resources/part2sample1.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)