Skip to content

Commit 9f6865c

Browse files
committed
Solution 2018-11, part 1 (Chronal Charge)
1 parent bcd486f commit 9f6865c

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.ronny_h.aoc.year2018.day11
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.grids.Grid
5+
import de.ronny_h.aoc.extensions.numbers.digit
6+
7+
fun main() = ChronalCharge().run("34,13", "")
8+
9+
class ChronalCharge : AdventOfCode<String>(2018, 11) {
10+
// start: 11:59
11+
override fun part1(input: List<String>): String {
12+
val grid = PowerCellGrid(input.single().toInt())
13+
return grid.find3x3SquareWithLargestTotal()
14+
}
15+
16+
override fun part2(input: List<String>): String {
17+
return ""
18+
}
19+
}
20+
21+
class PowerCellGrid(serialNumber: Int) : Grid<Int>(300, 300, -1000) {
22+
init {
23+
forEachIndex { y, x ->
24+
val rackId = x + 10
25+
var powerLevel = rackId * y + serialNumber
26+
powerLevel *= rackId
27+
val hundredsDigit = powerLevel.digit(3)
28+
this[y, x] = hundredsDigit - 5
29+
}.last()
30+
}
31+
32+
fun find3x3SquareWithLargestTotal(): String {
33+
val maxCoordinates = forEachCoordinates { coordinates, _ ->
34+
val total = (0..2).sumOf { i ->
35+
(0..2).sumOf { j ->
36+
this[coordinates.row + i, coordinates.col + j]
37+
}
38+
}
39+
coordinates to total
40+
}
41+
.maxBy { it.second }
42+
.first
43+
return "${maxCoordinates.col},${maxCoordinates.row}" // X,Y
44+
}
45+
46+
override fun Char.toElementType(): Int = digitToInt()
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package de.ronny_h.aoc.year2018.day11
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.data.forAll
5+
import io.kotest.data.row
6+
import io.kotest.matchers.shouldBe
7+
8+
class ChronalChargeTest : StringSpec({
9+
10+
"the power level of the fuel cell at coordinates in a grid with a given serial number" {
11+
forAll(
12+
row(3, 5, 8, 4),
13+
row(122, 79, 57, -5),
14+
row(217, 196, 39, 0),
15+
row(101, 153, 71, 4),
16+
) { x, y, serialNumber, powerLevel ->
17+
PowerCellGrid(serialNumber)[y, x] shouldBe powerLevel
18+
}
19+
}
20+
21+
"part 1: For grid serial number 18, the largest total 3x3 square has a top-left corner of 33,45" {
22+
ChronalCharge().part1(listOf("18")) shouldBe "33,45"
23+
}
24+
25+
"part 1: For grid serial number 42, the largest 3x3 square's top-left is 21,61" {
26+
ChronalCharge().part1(listOf("42")) shouldBe "21,61"
27+
}
28+
29+
"part 2" {
30+
val input = listOf("")
31+
ChronalCharge().part2(input) shouldBe ""
32+
}
33+
})

0 commit comments

Comments
 (0)