Skip to content

Commit 8a4a9cb

Browse files
committed
Solution 2017-02 (Corruption Checksum)
1 parent 04fa39a commit 8a4a9cb

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package de.ronny_h.aoc.year2017.day02
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.combinations
5+
6+
fun main() = CorruptionChecksum().run(44216, 320)
7+
8+
class CorruptionChecksum : AdventOfCode<Int>(2017, 2) {
9+
override fun part1(input: List<String>): Int = input.sumOf {
10+
val ints = it.parseInts()
11+
ints.max() - ints.min()
12+
}
13+
14+
override fun part2(input: List<String>): Int = input.sumOf {
15+
it.parseInts()
16+
.combinations()
17+
.forEach { (a, b) ->
18+
if (a % b == 0) {
19+
return@sumOf a / b
20+
}
21+
}
22+
.let { return 0 }
23+
}
24+
25+
private fun String.parseInts(): List<Int> = split(" ", "\t").map { it.toInt() }
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package de.ronny_h.aoc.year2017.day02
2+
3+
import de.ronny_h.aoc.extensions.asList
4+
import io.kotest.core.spec.style.StringSpec
5+
import io.kotest.matchers.shouldBe
6+
7+
class CorruptionChecksumTest : StringSpec({
8+
9+
"part 1: the checksum of the example spreadsheet" {
10+
val input = """
11+
5 1 9 5
12+
7 5 3
13+
2 4 6 8
14+
""".asList()
15+
CorruptionChecksum().part1(input) shouldBe 18
16+
}
17+
18+
"part 2: the sum of evenly divisible values' divide results" {
19+
val input = """
20+
5 9 2 8
21+
9 4 7 3
22+
3 8 6 5
23+
""".asList()
24+
CorruptionChecksum().part2(input) shouldBe 9
25+
}
26+
})

0 commit comments

Comments
 (0)