Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created December 25, 2020 11:44
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/816cad09736ec3aa051d35fd8c833e84 to your computer and use it in GitHub Desktop.
Save waynejo/816cad09736ec3aa051d35fd8c833e84 to your computer and use it in GitHub Desktop.
import java.io.FileInputStream
import scala.io.StdIn
@main def solve4() =
val validKeys: Set[String] = Set("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid")
def isValid(passport: Vector[(String, String)]): Boolean = {
val keys = passport.map { _._1 }.toSet
(validKeys diff keys).isEmpty
}
val in = new FileInputStream("example4-1.in")
System.setIn(in)
val inputs = Iterator.continually(StdIn.readLine()).takeWhile(_ != null).toVector
val passportElements = inputs.foldLeft(Vector[Vector[(String, String)]](Vector())) { (acc, line) =>
if line.trim.isEmpty then
acc :+ Vector()
else
acc.init :+ (acc.last ++ line.split(" ").map { x =>
val Array(key, value) = x.split(":")
(key, value)
}.toVector)
}
println(passportElements.count(isValid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment