Skip to content

Commit ffb0a79

Browse files
committed
Solved day16 part 1
1 parent 811ec37 commit ffb0a79

File tree

4 files changed

+129
-2
lines changed

4 files changed

+129
-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.15.1'
7+
version '0.15.2'
88

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

day16/src/main/kotlin/de/havox_design/aoc2023/day16/Day16.kt

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

33
class Day16(private var filename: String) {
44
fun solvePart1(): Long =
5-
46L
5+
getEnergy(parseTiles(getResourceAsText(filename)), Direction.EAST, Pair(0, 0))
6+
.toLong()
67

78
fun solvePart2(): Long =
89
0L
910

11+
private fun getEnergy(contraption: List<List<Tile>>, enteredDirection: Direction, startPoint: Pair<Int, Int>): Int {
12+
traceBeam(contraption, enteredDirection, contraption[startPoint.first][startPoint.second])
13+
14+
return contraption
15+
.flatten()
16+
.count { it.isEnergized() }
17+
}
18+
19+
private fun traceBeam(contraption: List<List<Tile>>, enteredDirection: Direction, startTile: Tile) {
20+
val nextDirections = startTile
21+
.getNextDirections(enteredDirection)
22+
val nextLocations = nextDirections
23+
.map { nextLocation(startTile.row, startTile.column, it) }
24+
25+
for (location in nextLocations.withIndex()) {
26+
when {
27+
(
28+
location.value.first !in contraption.indices
29+
|| location.value.second !in contraption[startTile.row].indices
30+
|| nextDirections[location.index] == Direction.NONE
31+
) -> continue
32+
}
33+
traceBeam(
34+
contraption,
35+
nextDirections[location.index],
36+
contraption[location.value.first][location.value.second]
37+
)
38+
}
39+
}
40+
41+
private fun nextLocation(row: Int, column: Int, direction: Direction): Pair<Int, Int> {
42+
return when (direction) {
43+
Direction.NORTH -> Pair(row - 1, column)
44+
Direction.SOUTH -> Pair(row + 1, column)
45+
Direction.EAST -> Pair(row, column + 1)
46+
Direction.WEST -> Pair(row, column - 1)
47+
else -> Pair(row, column)
48+
}
49+
}
50+
51+
private fun parseTiles(input: List<String>) =
52+
input.mapIndexed { row, line -> line.mapIndexed { column, c -> Tile(row, column, c) } }
53+
1054
private fun getResourceAsText(path: String): List<String> =
1155
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
1256
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.havox_design.aoc2023.day16
2+
3+
enum class Direction {
4+
NORTH,
5+
SOUTH,
6+
WEST,
7+
EAST,
8+
NONE
9+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package de.havox_design.aoc2023.day16
2+
3+
data class Tile(val row: Int, val column: Int, val type: Char) {
4+
private val MARKER_UP = "^"
5+
private val MARKER_DOWN = "V"
6+
private val MARKER_LEFT = "<"
7+
private val MARKER_RIGHT = ">"
8+
private val MARKER_EMPTY = '.'
9+
private val MARKER_NORTH_EAST_MIRROR = '\\'
10+
private val MARKER_SOUTH_EAST_MIRROR = '/'
11+
private val MARKER_ROW_SPLITTER = '|'
12+
private val MARKER_COL_SPLITTER = '-'
13+
14+
private val enteredDirections = mutableSetOf<Direction>()
15+
16+
fun isEnergized(): Boolean = enteredDirections.isNotEmpty()
17+
18+
fun getNextDirections(enteredDirection: Direction): List<Direction> {
19+
if (enteredDirection == Direction.NONE || !enteredDirections.add(enteredDirection)) {
20+
return listOf(Direction.NONE)
21+
}
22+
return when (type) {
23+
MARKER_EMPTY -> listOf(enteredDirection)
24+
MARKER_SOUTH_EAST_MIRROR -> when (enteredDirection) {
25+
Direction.NORTH -> listOf(Direction.EAST)
26+
Direction.EAST -> listOf(Direction.NORTH)
27+
Direction.SOUTH -> listOf(Direction.WEST)
28+
Direction.WEST -> listOf(Direction.SOUTH)
29+
else -> listOf(Direction.NONE)
30+
}
31+
32+
MARKER_NORTH_EAST_MIRROR -> when (enteredDirection) {
33+
Direction.NORTH -> listOf(Direction.WEST)
34+
Direction.EAST -> listOf(Direction.SOUTH)
35+
Direction.SOUTH -> listOf(Direction.EAST)
36+
Direction.WEST -> listOf(Direction.NORTH)
37+
else -> listOf(Direction.NONE)
38+
}
39+
40+
MARKER_COL_SPLITTER -> {
41+
if (enteredDirection in listOf(Direction.WEST, Direction.EAST)) listOf(enteredDirection)
42+
else listOf(Direction.WEST, Direction.EAST)
43+
}
44+
45+
MARKER_ROW_SPLITTER -> {
46+
if (enteredDirection in listOf(Direction.NORTH, Direction.SOUTH)) listOf(enteredDirection)
47+
else listOf(Direction.NORTH, Direction.SOUTH)
48+
}
49+
50+
else -> listOf(Direction.NONE)
51+
}
52+
}
53+
54+
@SuppressWarnings("kotlin:S6510")
55+
override fun toString(): String {
56+
when {
57+
enteredDirections.size > 1 -> {
58+
return enteredDirections.size.toString()
59+
}
60+
61+
else -> return when (this.type) {
62+
MARKER_EMPTY -> when (enteredDirections.firstOrNull()) {
63+
Direction.NORTH -> MARKER_UP
64+
Direction.EAST -> MARKER_RIGHT
65+
Direction.SOUTH -> MARKER_DOWN
66+
Direction.WEST -> MARKER_LEFT
67+
else -> this.type.toString()
68+
}
69+
70+
else -> this.type.toString()
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)