@@ -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