@@ -5,7 +5,7 @@ import de.ronny_h.aoc.extensions.grids.Coordinates
55import de.ronny_h.aoc.extensions.grids.Grid
66import de.ronny_h.aoc.extensions.numbers.digit
77
8- fun main () = ChronalCharge ().run (" 34,13" , " " )
8+ fun main () = ChronalCharge ().run (" 34,13" , " 280,218,11 " )
99
1010class ChronalCharge : AdventOfCode <String >(2018 , 11 ) {
1111 // start: 11:59
@@ -17,41 +17,65 @@ class ChronalCharge : AdventOfCode<String>(2018, 11) {
1717}
1818
1919class PowerCellGrid (serialNumber : Int ) : Grid<Int>(300 , 300 , -1000 ) {
20+ private val cache = mutableMapOf<Configuration , Int >()
21+
2022 init {
2123 forEachIndex { y, x ->
2224 val rackId = x + 10
2325 var powerLevel = rackId * y + serialNumber
2426 powerLevel * = rackId
2527 val hundredsDigit = powerLevel.digit(3 )
2628 this [y, x] = hundredsDigit - 5
29+ cache[Configuration (Coordinates (y, x), 1 )] = this [y, x]
2730 }.last()
2831 }
2932
3033 fun findSquareWithLargestTotal (): String {
3134 val max = (1 .. 300 )
32- .map { findSquareWithLargestTotalForSize(it) }
35+ .map {
36+ println (" calc for square size $it " )
37+ findSquareWithLargestTotalForSize(it)
38+ }
3339 .maxBy { it.total }
40+ .configuration
3441 return " ${max.coordinates.col} ,${max.coordinates.row} ,${max.size} " // X,Y,size
3542 }
3643
3744 fun find3x3SquareWithLargestTotal (): String {
38- val maxCoordinates = findSquareWithLargestTotalForSize(3 ).coordinates
45+ val maxCoordinates = findSquareWithLargestTotalForSize(3 ).configuration. coordinates
3946 return " ${maxCoordinates.col} ,${maxCoordinates.row} " // X,Y
4047 }
4148
42- private data class Square (val coordinates : Coordinates , val size : Int , val total : Int )
49+ private data class Configuration (val coordinates : Coordinates , val size : Int )
50+ private data class Square (val configuration : Configuration , val total : Int )
4351
4452 private fun findSquareWithLargestTotalForSize (squareSize : Int ): Square =
4553 forEachCoordinates { coordinates, _ ->
46- val total = (0 .. < squareSize).sumOf { i ->
47- (0 .. < squareSize).sumOf { j ->
48- this [coordinates.row + i, coordinates.col + j]
49- }
54+ val config = Configuration (coordinates, squareSize)
55+ val total = if (squareSize == 1 ) {
56+ cache.getValue(config)
57+ } else {
58+ val smallerCached = cache[Configuration (coordinates, squareSize - 1 )]
59+
60+ val total = smallerCached?.let { cached ->
61+ val additionalColumnTotal =
62+ (0 .. < squareSize).sumOf { this [coordinates.row + it, coordinates.col + squareSize - 1 ] }
63+ val additionalRowTotal =
64+ (0 .. < squareSize - 1 ).sumOf { this [coordinates.row + squareSize - 1 , coordinates.col + it] }
65+ cached + additionalColumnTotal + additionalRowTotal
66+ } ? : sumOfSquare(coordinates, squareSize)
67+ cache[config] = total
68+ total
5069 }
51- Square (coordinates, squareSize , total)
70+ Square (config , total)
5271 }
5372 .maxBy { it.total }
5473
74+ private fun sumOfSquare (coordinates : Coordinates , squareSize : Int ): Int = (0 .. < squareSize).sumOf { i ->
75+ (0 .. < squareSize).sumOf { j ->
76+ this [coordinates.row + i, coordinates.col + j]
77+ }
78+ }
5579
5680 override fun Char.toElementType (): Int = digitToInt()
5781}
0 commit comments