Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active January 8, 2021 12:05
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 waynejo/bc38a5894c5224871e71dce55306e163 to your computer and use it in GitHub Desktop.
Save waynejo/bc38a5894c5224871e71dce55306e163 to your computer and use it in GitHub Desktop.
import java.io.FileInputStream
import scala.io.StdIn
@main def solve5() =
def solve(seatNumber: String): Int = {
case class Range(min: Int, max: Int)
def partitioning(ch: Char, range: Range): Range = {
if ch == 'F' || ch == 'L' then
Range(range.min, range.min + (range.max - range.min) / 2)
else
Range(range.max - (range.max - range.min) / 2, range.max)
}
val row = seatNumber.take(7).foldLeft(Range(0, 127)) { (acc, ch) => partitioning(ch, acc) }.min
val col = seatNumber.drop(7).foldLeft(Range(0, 7)) { (acc, ch) => partitioning(ch, acc) }.min
row * 8 + col
}
val in = new FileInputStream("example5-1.in")
System.setIn(in)
val inputs = Iterator.continually(StdIn.readLine()).takeWhile(_ != null).toVector
println(inputs.map { solve }.max)
import java.io.FileInputStream
import scala.io.StdIn
@main def solve5() =
def solve(seatNumber: String): Int = {
case class Range(min: Int, max: Int)
def partitioning(ch: Char, range: Range): Range = {
if ch == 'F' || ch == 'L' then
Range(range.min, range.min + (range.max - range.min) / 2)
else
Range(range.max - (range.max - range.min) / 2, range.max)
}
val row = seatNumber.take(7).foldLeft(Range(0, 127)) { (acc, ch) => partitioning(ch, acc) }.min
val col = seatNumber.drop(7).foldLeft(Range(0, 7)) { (acc, ch) => partitioning(ch, acc) }.min
row * 8 + col
}
val in = new FileInputStream("example5-1.in")
System.setIn(in)
val inputs = Iterator.continually(StdIn.readLine()).takeWhile(_ != null).toVector
val ids = inputs.map { solve }.sorted
val emptyIdx = ids.indices.tail.find(idx => ids(idx - 1) + 1 != ids(idx)).getOrElse(0)
println(ids(emptyIdx - 1) + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment