-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.plagov.advent.aoc2015 | ||
|
||
class Day10 { | ||
|
||
fun partOne(input: String): Int { | ||
var result = input | ||
|
||
repeat(40) { | ||
result = getNextSequence(result) | ||
} | ||
|
||
return result.length | ||
} | ||
|
||
fun partTwo(input: String): Int { | ||
var result = input | ||
|
||
repeat(50) { | ||
result = getNextSequence(result) | ||
} | ||
|
||
return result.length | ||
} | ||
|
||
private fun getNextSequence(input: String): String { | ||
val regex = """(\d)\1*""".toRegex() | ||
|
||
return regex.findAll(input) | ||
.map { "${it.value.length}${it.value.first()}" } | ||
.joinToString(separator = "") { it } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.plagov.advent.aoc2015 | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class Day10Test { | ||
|
||
private val day10 = Day10() | ||
|
||
private val input = "1321131112" | ||
|
||
@Test | ||
fun solveFirst() { | ||
assertEquals(492982, day10.partOne(input)) | ||
} | ||
|
||
@Test | ||
fun solveSecond() { | ||
assertEquals(6989950, day10.partTwo(input)) | ||
} | ||
} |