Commit 8d536cfa authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[day5] solution

parent a9375c1b
import aoc2020.lib._
def day5(input: List[String]): String =
val ids = for i <- input
yield seatId(seatCoords(i))
s"max id: ${ids.max}\nown id: ${findOwn(ids).map(_.toString).getOrElse("No free seat!")}"
def seatCoords(code: String): (Int, Int) =
(
recursiveSeat('B', 'F')(code.substr(0,7).toList, 0, 127),
recursiveSeat('R', 'L')(code.substr(7).toList, 0, 7)
)
@annotation.tailrec
def recursiveSeat(upChar: Char, downChar: Char)(code: List[Char], low: Int, high: Int): Int =
code match {
case c :: rest =>
val (nLow, nHigh) = if c == upChar then up(low, high) else down(low, high)
recursiveSeat(upChar, downChar)(rest, nLow, nHigh)
case Nil => low
}
inline def up(low: Int, high: Int) = (high - (high - low) / 2, high)
inline def down(low: Int, high: Int) = (low, high - (high - low) / 2 - 1)
inline def seatId(loc: (Int, Int)) = loc._1 * 8 + loc._2
def findOwn(ids: List[Int]): Option[Int] =
ids.sorted.sliding(2).find{l => l(1) - l(0) == 2}.map(_(0)+1)
......@@ -5,5 +5,6 @@
case 2 => input()(day2)
case 3 => input(boolChar('#'))(day3)
case 4 => input()(day4)
case 5 => input()(day5)
}
println(out)
......@@ -12,3 +12,8 @@ extension (s: String)
case Array(a, b) => Some((a.asInstanceOf[String], b.asInstanceOf[String]))
case _ => None
}
def substr(from: Int, to: Int): String =
s.substring(from, to).asInstanceOf[String]
def substr(from: Int): String =
s.substring(from).asInstanceOf[String]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment