AdventOfCode-2017/day13_2.kt

33 lines
968 B
Kotlin

import java.io.File
import kotlin.math.cos
import kotlin.system.measureTimeMillis
fun main(args: Array<String>){
val file = File("../puzzle_13.txt")
// val file = File("test.txt")
val lines = file.readLines()
val scannerDepths = mutableMapOf<Int, Int>()
lines.forEach {
val pieces = it.split(": ")
scannerDepths.put(pieces[0].toInt(), pieces[1].toInt())
}
var offset = 0
val cost = scannerDepths.map {
if(it.key + offset % ((it.value - 1) * 2) == 0) it.value * it.key else 0
}.fold(0) { a, b -> a + b}
val time = measureTimeMillis {
while(offset < Int.MAX_VALUE){
val cost = scannerDepths.map {
if((it.key + offset) % ((it.value - 1) * 2) == 0) it.value * it.key + 1 else 0
}.fold(0) { a, b -> a + b}
if(cost == 0) {
println(offset)
break
}
offset++
}
}
println(time)
}