Skip to content

Commit b7790fb

Browse files
committed
Solution 2018-14: Small improvement
Make especially part 2 better readable and a bit more performant.
1 parent 6128c99 commit b7790fb

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/numbers/Digits.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ import kotlin.math.abs
77
*/
88
fun Long.digitCount(): Int {
99
if (this == 0L) return 1
10+
require(this >= 0)
11+
12+
var count = 0
13+
var currentNumber = this
14+
while (currentNumber > 0) {
15+
currentNumber /= 10
16+
count++
17+
}
18+
return count
19+
}
20+
21+
/**
22+
* Counts the number of digits of the given Int number.
23+
*/
24+
fun Int.digitCount(): Int {
25+
if (this == 0) return 1
26+
require(this >= 0)
1027

1128
var count = 0
1229
var currentNumber = this
@@ -49,3 +66,28 @@ fun Boolean.toDigit(): String = when (this) {
4966
true -> "1"
5067
false -> "0"
5168
}
69+
70+
/**
71+
* @return this positive [Int]'s digits from the lowest to the highest.
72+
*/
73+
fun Int.digits(): Sequence<Int> = sequence {
74+
require(this@digits >= 0)
75+
var remainder = this@digits
76+
if (remainder == 0) yield(0)
77+
while (remainder != 0) {
78+
yield(remainder % 10)
79+
remainder /= 10
80+
}
81+
}
82+
83+
/**
84+
* @return this positive [Int]'s digits from the highest to the lowest.
85+
*/
86+
fun Int.digitsReversed(): Sequence<Int> = sequence {
87+
require(this@digitsReversed >= 0)
88+
var factor = 10.pow(this@digitsReversed.digitCount() - 1)
89+
while (factor > 0) {
90+
yield((this@digitsReversed % (10 * factor) - this@digitsReversed % factor) / factor)
91+
factor /= 10
92+
}
93+
}

src/main/kotlin/de/ronny_h/aoc/year2018/day14/ChocolateCharts.kt

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

33
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.numbers.digitsReversed
45

56
fun main() = ChocolateCharts().run("5992684592", "20181148")
67

@@ -35,8 +36,8 @@ private class ScoreBoard {
3536
while (true) {
3637
val scoreOne = scores[indexOne]
3738
val scoreTwo = scores[indexTwo]
38-
"${scoreOne + scoreTwo}".forEach {
39-
scores.add(it.digitToInt())
39+
(scoreOne + scoreTwo).digitsReversed().forEach {
40+
scores.add(it)
4041
if (stopCondition(scores)) {
4142
return scores.joinToString("")
4243
}

src/test/kotlin/de/ronny_h/aoc/extensions/numbers/DigitsTest.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ import io.kotest.data.row
77
import io.kotest.matchers.shouldBe
88

99
class DigitsTest : StringSpec({
10-
"Digits are counted" {
10+
"Long Digits are counted" {
1111
forAll(
12-
row(0L, 1), row(123L, 3), row(1234567890, 10)
12+
row(0L, 1), row(123L, 3), row(1234567890L, 10)
13+
) { number, digits ->
14+
number.digitCount() shouldBe digits
15+
}
16+
}
17+
18+
"Int Digits are counted" {
19+
forAll(
20+
row(0, 1), row(123, 3), row(1234567890, 10)
1321
) { number, digits ->
1422
number.digitCount() shouldBe digits
1523
}
@@ -61,4 +69,26 @@ class DigitsTest : StringSpec({
6169
true.toDigit() shouldBe "1"
6270
false.toDigit() shouldBe "0"
6371
}
72+
73+
"the digits of an Int" {
74+
forAll(
75+
row(0, listOf(0)),
76+
row(1, listOf(1)),
77+
row(123, listOf(3, 2, 1)),
78+
row(1234567, listOf(7, 6, 5, 4, 3, 2, 1)),
79+
) { int, digits ->
80+
int.digits().toList() shouldBe digits
81+
}
82+
}
83+
84+
"the reversed digits of an Int" {
85+
forAll(
86+
row(0, listOf(0)),
87+
row(1, listOf(1)),
88+
row(123, listOf(1, 2, 3)),
89+
row(1234567, listOf(1, 2, 3, 4, 5, 6, 7)),
90+
) { int, digits ->
91+
int.digitsReversed().toList() shouldBe digits
92+
}
93+
}
6494
})

0 commit comments

Comments
 (0)