Skip to content

Commit 04fa39a

Browse files
committed
Solution 2017-01 (Inverse Captcha)
1 parent 260bf21 commit 04fa39a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
})

0 commit comments

Comments
 (0)