Skip to content

Instantly share code, notes, and snippets.

@horace-velmont
Last active March 5, 2021 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horace-velmont/36cb74c52f01e74e66278b9aab0b5691 to your computer and use it in GitHub Desktop.
Save horace-velmont/36cb74c52f01e74e66278b9aab0b5691 to your computer and use it in GitHub Desktop.
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