File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
main/kotlin/de/ronny_h/aoc/year2015/day10
test/kotlin/de/ronny_h/aoc/year2015/day10 Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ })
You can’t perform that action at this time.
0 commit comments