- import scala.collection.mutable.Map
- import scala.collection.mutable.Set
- case class Field(name:String, a: Int, b:Int, c:Int, d:Int){
- def good(x: Int) = (x >= a && x <= b) || (x >= c && x <= d)
- }
- object Field {
- def apply(line: String) : Field = {
- val Array(name, values) = line.split(": ")
- val Array(a,b,c,d) = values.split(" or ").map(_.split("-")) .flatten.map(_.toInt)
- Field(name, a, b, c, d)
- }
- }
- val C = io.Source.fromFile("input").getLines.toList
- val fields = C.takeWhile(_.size>0).map(Field.apply)
- val mine = C.dropWhile(_.size>0).drop(2).head.split(",").map(_.toInt).toArray
- val nearBy= C.dropWhile(_.size>0).drop(5).map(_.split(",").map(_.toInt)).map(_.toList)
- val goods = Set[Int]()
- for(x <- 1 to 1000) {
- if(fields.exists(f => f.good(x))) goods.add(x)
- }
- val validNear = nearBy.filter{ near =>
- near.filterNot(goods).isEmpty
- }
- var order : List[(Int, List[Field])] = Nil
- for(col <- 0 until nearBy.head.size){
- val vertValues = validNear.map(_.drop(col).head)
- val chosenFields = fields.filter(f => vertValues.forall(f.good))
- println(col+ " => " + chosenFields.map(_.name).size)
- order = (col, chosenFields) :: order
- }
- val check = order.sortBy(_._2.size)
- val woozy = check.map(o => (o._1, o._2.map(_.name).toSet))
- val result = Map[Int, String]()
- val found = Set[String]()
- woozy.foreach {
- case (idx, wuss) => {
- val curr = wuss.diff(found)
- result(idx) = curr.head
- found.add(curr.head)
- }
- }
- val recipe = result.filter {
- case (k, v) => v.contains("departure")
- }
- println("Recipe " + recipe)
- val woah = recipe.keys.map(k => mine(k)).map(_.toLong).product
- println(woah)