Skip to content

Instantly share code, notes, and snippets.

@s5bug
Created March 11, 2019 18:44
Show Gist options
  • Save s5bug/0758d77e1d2e54a07c59154eb5afee9d to your computer and use it in GitHub Desktop.
Save s5bug/0758d77e1d2e54a07c59154eb5afee9d to your computer and use it in GitHub Desktop.
object Main extends IOApp {
val coordReg = """(\d+) (\d+)""".r
override def run(args: List[String]): IO[ExitCode] = {
implicit val ec: ExecutionContextExecutor = ExecutionContext.global
program[IO].compile.drain.as(ExitCode.Success)
}
def program[F[_]: Monad : ContextShift](implicit ec: ExecutionContext, sync: Sync[F]): Stream[F, Unit] = {
val control = RectangleControl[F](10, 10, 10)
val lineIn = io.stdin[F](1024, ec).filter(_ != '\r').split(_ == '\n'.toByte)
val boards = lineIn.scan(control.empty)((board, in) => board.flatMap { b =>
val line = in.map(_.toChar).toList.mkString
if(line.matches(coordReg.regex)) {
val coordReg(x, y) = line
control.unearth(Vec2[F](x.toInt, y.toInt))(b)
} else {
Stream.eval(sync.delay(println("Invalid input!"))).map(_ => b)
}
}).flatten
boards.flatMap(control.show).evalMap(s => sync.delay(println(s)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment