-
-
Save waynejo/bc38a5894c5224871e71dce55306e163 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
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) |
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
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