Commit 0dc106ce authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[day4, refactor] factor out logic to split input list by separator lines

parent 0e5f0d26
package aoc2020
import aoc2020.lib._
import aoc2020.lib.Passports._
def day4(input: List[String]): String =
......@@ -6,13 +7,6 @@ def day4(input: List[String]): String =
s"complete: ${complete.length}\nvalid: ${complete.count(_.isValid)}"
def findPassports(input: List[String]): List[Passport] =
def go(lines: List[String], accu: List[Passport]): List[Passport] =
lines.span(_ != "") match {
case (passport, "" :: rest) => go(rest, Passport.fromStrings(passport) :: accu)
case (passport, Nil) => Passport.fromStrings(passport) :: accu
case _ => List() // malformed input
}
go(input, List())
input.split("").foldLeft(List[Passport]())(
(passes, lines) => Passport.fromStrings(lines) :: passes
)
package aoc2020.lib
/* for splitting input with separator lines */
extension [A](input: List[A])(using Eql[A,A])
def split(separator: A): LazyList[List[A]] =
input.span(_ != separator) match {
case (h, _ :: t) => h #:: t.split(separator)
case (Nil, Nil) => LazyList()
case (h, Nil) => LazyList(h)
}
/* Using -Yexplicit-nulls isn't really ready for use with the java standard
* library. e.g. String doesn't have `@NotNull` annotations
*/
......
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