Skip to content

Commit 9aa3686

Browse files
committed
Use Int.pow() implementation where potentials of Int are calculated
This avoids type conversion to Double and makes the code a bit more readable.
1 parent a9dcde1 commit 9aa3686

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/Combinations.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.ronny_h.aoc.extensions
22

3-
import kotlin.math.pow
3+
import de.ronny_h.aoc.extensions.numbers.pow
44

55

66
/**
@@ -46,7 +46,7 @@ fun <E> permutationsOf(list: List<E>): Sequence<List<E>> = sequence {
4646
fun <E> allSublistsOf(list: List<E>): Sequence<List<E>> = sequence {
4747
// count a binary number with list.size digits from 0...0 to 1...1
4848
var number = 0
49-
while (number < 2.0.pow(list.size.toDouble())) {
49+
while (number < 2.pow(list.size)) {
5050
val sublist = ArrayList<E>(list.size)
5151
val binary = number.toString(2).reversed()
5252
// take the list elements at indices of the 1 bits

src/main/kotlin/de/ronny_h/aoc/year2015/day17/NoSuchThingAsTooMuch.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package de.ronny_h.aoc.year2015.day17
33
import de.ronny_h.aoc.AdventOfCode
44
import de.ronny_h.aoc.extensions.allSublistsOf
55

6-
fun main() = NoSuchThingAsTooMuch().run(654, 0)
6+
fun main() = NoSuchThingAsTooMuch().run(654, 57)
77

88
class NoSuchThingAsTooMuch : AdventOfCode<Int>(2015, 17) {
99
override fun part1(input: List<String>): Int =

src/main/kotlin/de/ronny_h/aoc/year2024/day07/BridgeRepair.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.ronny_h.aoc.year2024.day07
22

33
import de.ronny_h.aoc.AdventOfCode
4-
import kotlin.math.pow
4+
import de.ronny_h.aoc.extensions.numbers.pow
55

66
fun main() = BridgeRepair().run(1399219271639, 275791737999003)
77

@@ -55,7 +55,7 @@ data class Equation(val result: Long, val numbers: List<Long>, var solvable: Boo
5555
* - `operatorIndexSequenceFor(2,3)` yields `[0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2]` (counting from 0 `00` to 9 `22`)
5656
*/
5757
private fun operatorIndexSequenceFor(digits: Int, operatorCount: Int = 2) = sequence {
58-
for (count in 0..<(operatorCount.toDouble().pow(digits).toInt())) {
58+
for (count in 0..<operatorCount.pow(digits)) {
5959
val binary = count.toString(operatorCount).fillLeadingZeros(digits)
6060
for (i in 0..<digits) {
6161
yield(binary[i].digitToInt())

src/main/kotlin/de/ronny_h/aoc/year2024/day11/PlutonianPebbles.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package de.ronny_h.aoc.year2024.day11
22

33
import de.ronny_h.aoc.AdventOfCode
4-
import de.ronny_h.aoc.extensions.numbers.digitCount
54
import de.ronny_h.aoc.extensions.memoize
5+
import de.ronny_h.aoc.extensions.numbers.digitCount
6+
import de.ronny_h.aoc.extensions.numbers.pow
67

78
fun main() = PlutonianPebbles().run(193899, 229682160383225)
89

@@ -13,16 +14,8 @@ class PlutonianPebbles : AdventOfCode<Long>(2024, 11) {
1314
.map(String::toLong)
1415
.toMutableList()
1516

16-
private fun tenToPowerOf(digitCount: Int): Int {
17-
var tens = 10
18-
repeat((digitCount / 2) - 1) {
19-
tens *= 10
20-
}
21-
return tens
22-
}
23-
2417
private val cacheDigitCounts = mutableMapOf<Long, Int>()
25-
private val cachePowers = IntArray(20) { tenToPowerOf(it) }
18+
private val cachePowers = IntArray(20) { 10.pow(it / 2) }
2619

2720
private fun blink(stone: Long): List<Long> {
2821
if (stone == 0L) {
@@ -45,7 +38,12 @@ class PlutonianPebbles : AdventOfCode<Long>(2024, 11) {
4538
if (state.height == 0) {
4639
state.stones.size.toLong()
4740
} else if (state.stones.size > 1) {
48-
blinkRec(State(state.height, state.stones.subList(0, state.stones.size/2))) + blinkRec(State(state.height, state.stones.subList(state.stones.size/2, state.stones.size)))
41+
blinkRec(
42+
State(
43+
state.height,
44+
state.stones.subList(0, state.stones.size / 2)
45+
)
46+
) + blinkRec(State(state.height, state.stones.subList(state.stones.size / 2, state.stones.size)))
4947
} else {
5048
blinkRec(State(state.height - 1, blink(state.stones.single())))
5149
}

0 commit comments

Comments
 (0)