Skip to content

Instantly share code, notes, and snippets.

@whaley
Last active January 10, 2018 02:28
Show Gist options
  • Save whaley/c495c237c1bc27191bae2d6709ec7200 to your computer and use it in GitHub Desktop.
Save whaley/c495c237c1bc27191bae2d6709ec7200 to your computer and use it in GitHub Desktop.
fun checksum(spreadsheet: String): Int {
val lines = spreadsheet.split("\n")
val rows : List<List<Int>> = lines.map { it -> it.split(Regex("""\s""")).map { it -> Integer.valueOf(it) } }
return rows.fold(0, {sum:Int, list: List<Int> -> sum + (list.max() ?: 0) - (list.min() ?: 0)})
}
package com.morninghacks.aoc2017
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import kotlin.test.assertEquals
object Day02Spec : Spek({
given("Day Two : Part One") {
val spreadsheet = """5 1 9 5
.7 5 3
.2 4 6 8""".trimMargin(".")
on("checksum") {
assertEquals(18,checksum(spreadsheet))
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment