Skip to content

Commit 58287d4

Browse files
committed
Solution 2015-10 (Elves Look, Elves Say)
1 parent 80daf7b commit 58287d4

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package de.ronny_h.aoc.year2015.day10
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
5+
fun main() = ElvesLookElvesSay().run(329356, 4666278)
6+
7+
class ElvesLookElvesSay : AdventOfCode<Int>(2015, 10) {
8+
9+
override fun part1(input: List<String>) = input.first().lookAndSay(40)
10+
override fun part2(input: List<String>) = input.first().lookAndSay(50)
11+
12+
private fun String.lookAndSay(times: Int): Int {
13+
var sequence = this
14+
repeat(times) {
15+
sequence = lookAndSay(sequence)
16+
}
17+
return sequence.length
18+
}
19+
20+
fun lookAndSay(sequence: String): String {
21+
var char = sequence.first()
22+
var count = 0
23+
return sequence.map {
24+
if (it == char) {
25+
count++
26+
""
27+
} else {
28+
val s = "$count$char"
29+
char = it
30+
count = 1
31+
s
32+
}
33+
}.joinToString("") + "$count$char"
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package de.ronny_h.aoc.year2015.day10
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.matchers.shouldBe
5+
6+
class ElvesLookElvesSayTest : StringSpec({
7+
8+
val day10 = ElvesLookElvesSay()
9+
10+
"1 becomes 11" {
11+
day10.lookAndSay("1") shouldBe "11"
12+
}
13+
14+
"11 becomes 21" {
15+
day10.lookAndSay("11") shouldBe "21"
16+
}
17+
18+
"21 becomes 1211" {
19+
day10.lookAndSay("21") shouldBe "1211"
20+
}
21+
22+
"1211 becomes 111221" {
23+
day10.lookAndSay("1211") shouldBe "111221"
24+
}
25+
26+
"111221 becomes 312211" {
27+
day10.lookAndSay("111221") shouldBe "312211"
28+
}
29+
30+
"part 1: The length of 40 times lookAndSay for 1" {
31+
day10.part1(listOf("1")) shouldBe 82350
32+
}
33+
34+
"part 2: The length of 50 times lookAndSay for 1" {
35+
day10.part2(listOf("1")) shouldBe 1166642
36+
}
37+
})

0 commit comments

Comments
 (0)