diff --git a/src/main/scala/org/lemon/advent/lib/parse/csv.scala b/src/main/scala/org/lemon/advent/lib/parse/csv.scala index 830ee23..c9d411d 100644 --- a/src/main/scala/org/lemon/advent/lib/parse/csv.scala +++ b/src/main/scala/org/lemon/advent/lib/parse/csv.scala @@ -1,23 +1,28 @@ package org.lemon.advent.lib.parse -private trait Sep: +private trait GenSep: val delimeter: String def unapplySeq[T](str: String)(using unapply: String => Option[T]): Option[Seq[T]] = Some(str.split(delimeter).map(_.trim).toSeq.flatMap(unapply)) /** Matches chunks of a generic type separated by commas. */ -object Csv extends Sep: +object Csv extends GenSep: val delimeter = "," /** Matches chunks of a generic type separated by any whitespace. */ -object Wsv extends Sep: +object Wsv extends GenSep: val delimeter = "\\s+" // givens for delimiter-separated values to be generic given (String => Option[String]) = Some(_) +/** Matches each line of a string. + */ +object Lines: + def unapplySeq(str: String): Option[Seq[String]] = Some(str.linesIterator.toSeq) + /** Matches chunks of text separated by two newlines. */ object Chunk: diff --git a/src/main/scala/org/lemon/advent/year2024/Day19.scala b/src/main/scala/org/lemon/advent/year2024/Day19.scala index a5b3974..81b2105 100644 --- a/src/main/scala/org/lemon/advent/year2024/Day19.scala +++ b/src/main/scala/org/lemon/advent/year2024/Day19.scala @@ -9,7 +9,7 @@ private object Day19: def parse(input: String) = import org.lemon.advent.lib.parse.{given, _} input match - case Chunk(Csv[String](towels @ _*), Wsv[String](targets @ _*)) => (towels, targets) + case Chunk(Csv[String](towels @ _*), Lines(targets @ _*)) => (towels, targets) def countLayouts(towels: Seq[String], target: String) = lazy val count: String => Long = memoize {