-
-
Save horace-velmont/36cb74c52f01e74e66278b9aab0b5691 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package day13 | |
import java.io.FileInputStream | |
import scala.io.StdIn | |
@main def solve13_1() = | |
case class Bus(id: Int, wating: Int) | |
def find(id: Int, time: Int): Bus = Bus(id, id - (time % id)) | |
val in = new FileInputStream("src/example13-1.in") | |
System.setIn(in) | |
var time = StdIn.readLine().toInt | |
val sol = StdIn.readLine().split(",") | |
.filterNot(_.equals("x")) | |
.map(x => find(x.toInt, time)) | |
.minBy(_.wating) | |
println(sol.id * sol.wating) | |
// failed to solve | |
@main def solve13_2() = | |
val in = new FileInputStream("src/example13-1.in") | |
System.setIn(in) | |
var time = StdIn.readLine().toInt | |
case class Bus(item: Int, idx: Int) | |
val vec = StdIn.readLine().split(",") | |
.zipWithIndex | |
.map((item, idx) => if item == "x" then Bus(0, idx) else Bus(item.toInt, idx)) | |
.filterNot(_.item.equals(0)) | |
val tree = vec(0).item | |
val sol = Iterator.from(1).find(i => vec.forall(bus => { | |
val src = tree * i | |
val target = src + bus.idx | |
if target >= bus.item | |
then target % bus.item == 0 | |
else false | |
})) | |
println(sol.get * tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment