File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
main/kotlin/de/ronny_h/aoc/year2017/day01
test/kotlin/de/ronny_h/aoc/year2017/day01 Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ package de.ronny_h.aoc.year2017.day01
2+
3+ import de.ronny_h.aoc.AdventOfCode
4+
5+ fun main () = InverseCaptcha ().run (1343 , 1274 )
6+
7+ class InverseCaptcha : AdventOfCode <Int >(2017 , 1 ) {
8+ override fun part1 (input : List <String >): Int = sumOfSameNumbersInDistance(input.first(), 1 )
9+ override fun part2 (input : List <String >): Int = sumOfSameNumbersInDistance(input.first(), input.first().length / 2 )
10+
11+ private fun sumOfSameNumbersInDistance (sequence : String , distance : Int ): Int {
12+ var sum = 0
13+ sequence.forEachIndexed { i, n ->
14+ if (n == sequence[(i + distance) % sequence.length]) {
15+ sum + = " $n " .toInt()
16+ }
17+ }
18+ return sum
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package de.ronny_h.aoc.year2017.day01
2+
3+ import io.kotest.core.spec.style.StringSpec
4+ import io.kotest.data.forAll
5+ import io.kotest.data.row
6+ import io.kotest.matchers.shouldBe
7+
8+ class InverseCaptchaTest : StringSpec ({
9+
10+ " part 1: all the examples given" {
11+ forAll(
12+ row("1122", 3),
13+ row("1111", 4),
14+ row("1234", 0),
15+ row("91212129", 9),
16+ ) { sequence, sum ->
17+ InverseCaptcha ().part1(listOf(sequence)) shouldBe sum
18+ }
19+ }
20+
21+ " part 2: all the examples given" {
22+ forAll(
23+ row("1212", 6),
24+ row("1221", 0),
25+ row("123425", 4),
26+ row("123123", 12),
27+ row("12131415", 4),
28+ ) { sequence, sum ->
29+ InverseCaptcha ().part2(listOf(sequence)) shouldBe sum
30+ }
31+ }
32+ })
You can’t perform that action at this time.
0 commit comments