Skip to content

Commit 4e7922e

Browse files
committed
Solved day16 part 2
1 parent ddff0e5 commit 4e7922e

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
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.3'
7+
version '0.15.4'
88

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

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,66 @@ class Day16(private var filename: String) {
55
getEnergy(parseTiles(getResourceAsText(filename)), Direction.EAST, Pair(0, 0))
66
.toLong()
77

8-
fun solvePart2(): Long =
9-
51L
8+
fun solvePart2(): Long {
9+
val input = getResourceAsText(filename)
10+
val startPoints = input
11+
.flatMapIndexed { row, s ->
12+
s
13+
.withIndex()
14+
.filter {
15+
row == 0
16+
|| row == input.size - 1
17+
|| it.index == 0
18+
|| it.index == s.length - 1
19+
}
20+
.map { iv -> Pair(row, iv.index) }
21+
}
22+
val maxRow = startPoints
23+
.maxOf { it.first }
24+
val maxColumn = startPoints
25+
.maxOf { it.second }
26+
val results = startPoints
27+
.flatMap {
28+
when {
29+
it.first == 0 && it.second == 0 -> {
30+
listOf(
31+
Pair(it, getEnergy(parseTiles(input), Direction.EAST, it)),
32+
Pair(it, getEnergy(parseTiles(input), Direction.SOUTH, it))
33+
)
34+
}
35+
36+
it.first == maxRow && it.second == maxColumn -> {
37+
listOf(
38+
Pair(it, getEnergy(parseTiles(input), Direction.WEST, it)),
39+
Pair(it, getEnergy(parseTiles(input), Direction.NORTH, it))
40+
)
41+
}
42+
43+
it.first == 0 -> {
44+
listOf(Pair(it, getEnergy(parseTiles(input), Direction.SOUTH, it)))
45+
}
46+
47+
it.second == 0 -> {
48+
listOf(Pair(it, getEnergy(parseTiles(input), Direction.EAST, it)))
49+
}
50+
51+
it.first == maxRow -> {
52+
listOf(Pair(it, getEnergy(parseTiles(input), Direction.NORTH, it)))
53+
}
54+
55+
it.second == maxColumn -> {
56+
listOf(Pair(it, getEnergy(parseTiles(input), Direction.WEST, it)))
57+
}
58+
59+
else -> {
60+
emptyList()
61+
}
62+
}
63+
}
64+
return results
65+
.maxOf { it.second }
66+
.toLong()
67+
}
1068

1169
private fun getEnergy(contraption: List<List<Tile>>, enteredDirection: Direction, startPoint: Pair<Int, Int>): Int {
1270
traceBeam(contraption, enteredDirection, contraption[startPoint.first][startPoint.second])

0 commit comments

Comments
 (0)