-
-
Save waynejo/816cad09736ec3aa051d35fd8c833e84 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 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