Skip to content

Commit 2dd8e06

Browse files
committed
finish day13
1 parent 7b53daf commit 2dd8e06

File tree

6 files changed

+161
-8
lines changed

6 files changed

+161
-8
lines changed

src/main/kotlin/tr/emreone/adventofcode/days/Day08.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package tr.emreone.adventofcode.days
22

3-
import tr.emreone.kotlin_utils.Resources
43
import tr.emreone.kotlin_utils.automation.Day
5-
import tr.emreone.kotlin_utils.lcm
6-
import java.lang.IllegalStateException
4+
import tr.emreone.kotlin_utils.math.lcm
75

86
class Day08 : Day(8, 2023, "Haunted Wasteland") {
97

@@ -29,11 +27,11 @@ class Day08 : Day(8, 2023, "Haunted Wasteland") {
2927

3028
override fun part1(): Int {
3129
val (command, maps) = parseInput()
32-
var currentPosition ="AAA"
30+
var currentPosition = "AAA"
3331

3432
var counter = 0
3533
val totalCommands = command.size
36-
while(currentPosition != "ZZZ") {
34+
while (currentPosition != "ZZZ") {
3735
val index = (counter.mod(totalCommands))
3836
// print("%6d: $currentPosition to ($index -> ${ if (command[index] == 0) 'L' else 'R'}) ".format(counter))
3937
currentPosition = maps[currentPosition]?.get(command[index]) ?: throw IllegalStateException()
@@ -54,7 +52,7 @@ class Day08 : Day(8, 2023, "Haunted Wasteland") {
5452
var currentPosition = it
5553

5654
var counter = 0L
57-
while(!currentPosition.endsWith('Z')) {
55+
while (!currentPosition.endsWith('Z')) {
5856
val index = (counter.mod(totalCommands))
5957
currentPosition = maps[currentPosition]?.get(command[index]) ?: throw IllegalStateException()
6058
counter++

src/main/kotlin/tr/emreone/adventofcode/days/Day11.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package tr.emreone.adventofcode.days
22

33
import tr.emreone.kotlin_utils.automation.Day
4-
import tr.emreone.kotlin_utils.manhattanDistanceTo
54
import tr.emreone.kotlin_utils.math.Coords
5+
import tr.emreone.kotlin_utils.math.manhattanDistanceTo
66

77
class Day11 : Day(11, 2023, "Cosmic Expansion") {
88

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,130 @@
11
package tr.emreone.adventofcode.days
22

33
import tr.emreone.kotlin_utils.automation.Day
4+
import java.lang.IllegalStateException
45

56
class Day13 : Day(13, 2023, "Point of Incidence") {
67

8+
class Grid(input: String) {
9+
val area: List<List<Char>>
10+
var horizontalLineIndex: Int? = null
11+
var verticalLineIndex: Int? = null
12+
13+
init {
14+
this.area = input.split("\\r?\\n".toRegex())
15+
.map {
16+
it.toCharArray().map { it }
17+
}
18+
}
19+
20+
private fun getColumn(x: Int): List<Char> {
21+
if (x !in this.area.get(0).indices) {
22+
throw IndexOutOfBoundsException()
23+
}
24+
return (this.area).map {
25+
it[x]
26+
}
27+
}
28+
29+
private fun getRow(y: Int): List<Char> {
30+
return this.area.get(y)
31+
}
32+
33+
private fun isVerticalReflectedAt(yi: Int): Boolean {
34+
if (yi < 0 || yi == this.area.lastIndex) return false
35+
36+
val maxHeight = minOf(yi, this.area.lastIndex - 1 - yi)
37+
return (0..maxHeight).all { i ->
38+
val row1 = getRow(yi - i)
39+
val row2 = getRow(yi + 1 + i)
40+
// println("Comparing y=${yi - i} ($row1) with y=${yi + 1 + i} ($row2)")
41+
row1.equals(row2)
42+
}
43+
}
44+
45+
private fun isHorizontalReflectedAt(xi: Int): Boolean {
46+
if (xi < 0 || xi == this.area.get(0).lastIndex) return false
47+
48+
val maxWidth = minOf(xi, this.area.get(0).lastIndex - 1 - xi)
49+
return (0..maxWidth).all { i ->
50+
val col1 = getColumn(xi - i)
51+
val col2 = getColumn(xi + 1 + i)
52+
// println("Comparing x=${xi - i} ($col1) with x=${xi + 1 + i} ($col2)")
53+
col1.equals(col2)
54+
}
55+
}
56+
57+
fun getScore(originalGrid: Grid? = null): Int {
58+
val possibleVerticalLines = (0 until this.area.lastIndex).filter {
59+
isVerticalReflectedAt(it)
60+
}
61+
if (possibleVerticalLines.isNotEmpty()) {
62+
this.verticalLineIndex = possibleVerticalLines.firstOrNull {
63+
it != originalGrid?.verticalLineIndex
64+
}
65+
if (this.verticalLineIndex != null) {
66+
return (this.verticalLineIndex!! + 1) * 100
67+
}
68+
}
69+
70+
val possibleHorizontalLines = (0 until this.area.get(0).lastIndex).filter {
71+
isHorizontalReflectedAt(it)
72+
}
73+
if (possibleHorizontalLines.isNotEmpty()) {
74+
this.horizontalLineIndex = possibleHorizontalLines.firstOrNull {
75+
it != originalGrid?.horizontalLineIndex
76+
}
77+
if (this.horizontalLineIndex != null) {
78+
return this.horizontalLineIndex!! + 1
79+
}
80+
}
81+
82+
return 0
83+
}
84+
85+
fun getCopyWithToggledCell(xi: Int, yi: Int): Grid {
86+
val aGrid = this.area.map { it.toMutableList() }
87+
aGrid[yi][xi] = if (aGrid[yi][xi] == '.') '#' else '.'
88+
89+
return Grid(aGrid.joinToString("\n") { it.joinToString("") })
90+
}
91+
92+
fun getAlternativeScore(): Int {
93+
for(yi in this.area.indices) {
94+
for(xi in this.area.get(yi).indices) {
95+
val alternativeGrid = getCopyWithToggledCell(xi, yi)
96+
val aScore = alternativeGrid.getScore(this)
97+
98+
if (aScore > 0) {
99+
return aScore
100+
}
101+
}
102+
}
103+
104+
throw IllegalStateException("No alternative score found.")
105+
}
106+
107+
fun print() {
108+
for(y in this.area.indices) {
109+
println(this.area[y].joinToString(""))
110+
}
111+
}
112+
}
113+
114+
override fun part1(): Int {
115+
return inputAsString.split("(\\r?\\n){2}".toRegex())
116+
.sumOf {
117+
Grid(it).getScore()
118+
}
119+
}
120+
121+
override fun part2(): Int {
122+
return inputAsString.split("(\\r?\\n){2}".toRegex())
123+
.sumOf {
124+
val grid = Grid(it)
125+
val score = grid.getScore()
126+
grid.getAlternativeScore()
127+
}
128+
}
129+
7130
}

src/test/kotlin/tr/emreone/adventofcode/days/Day12Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal class Day12Test {
88

99
@Test
1010
fun `execute_tests`() {
11-
solve<Day12>(true) {
11+
solve<Day12>(false) {
1212
Resources.resourceAsList("day12_example.txt")
1313
.joinToString("\n") part1 21 part2 525_152
1414
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tr.emreone.adventofcode.days
2+
3+
import org.junit.jupiter.api.Test
4+
import tr.emreone.kotlin_utils.Resources
5+
import tr.emreone.kotlin_utils.automation.solve
6+
7+
internal class Day13Test {
8+
9+
@Test
10+
fun `execute_tests`() {
11+
solve<Day13>(false) {
12+
Resources.resourceAsList("day13_example.txt")
13+
.joinToString("\n") part1 405 part2 400
14+
}
15+
}
16+
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#.##..##.
2+
..#.##.#.
3+
##......#
4+
##......#
5+
..#.##.#.
6+
..##..##.
7+
#.#.##.#.
8+
9+
#...##..#
10+
#....#..#
11+
..##..###
12+
#####.##.
13+
#####.##.
14+
..##..###
15+
#....#..#

0 commit comments

Comments
 (0)