Skip to content

Commit

Permalink
✨ add Lines extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 19, 2024
1 parent 2898e4d commit f243963
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/main/scala/org/lemon/advent/lib/parse/csv.scala
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/lemon/advent/year2024/Day19.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f243963

Please sign in to comment.