Skip to content

Commit

Permalink
⚗️ fiddling with parsing util
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 19, 2024
1 parent 45d1e70 commit c05570f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/main/scala/org/lemon/advent/lib/parse.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package org.lemon.advent.lib

import scala.math.BigInt

object Chunk:
def unapplySeq(str: String): Option[Seq[String]] = Some(str.split("\n\n").toSeq)

object Num:
def unapply[T: Numeric](s: String): Option[T] = summon[Numeric[T]].parseString(s)

// givens for delimiter-separated values to be generic
given (String => Option[String]) = Some(_)
given (String => Option[Int]) = Int.unapply
given (String => Option[Long]) = Long.unapply
given (String => Option[BigInt]) = BigInt.unapply

object Csv:
private val delimeter = ","
def unapplySeq[T: Numeric](str: String): Option[Seq[T]] = Some(splitParse(str, delimeter).flatMap(Num.unapply[T]))
def unapplySeq(str: String): Option[Seq[String]] = Some(splitParse(str, delimeter))
def unapplySeq[T](str: String)(using unapply: String => Option[T]): Option[Seq[T]] =
Some(splitParse(str, delimeter).flatMap(unapply))

object Wsv:
private val delimeter = "\\s+"
def unapplySeq[T: Numeric](str: String): Option[Seq[T]] = Some(splitParse(str, delimeter).flatMap(Num.unapply[T]))
def unapplySeq(str: String): Option[Seq[String]] = Some(splitParse(str, delimeter))
def unapplySeq[T](str: String)(using unapply: String => Option[T]): Option[Seq[T]] =
Some(splitParse(str, delimeter).flatMap(unapply))

private def splitParse(str: String, delimeter: String): Seq[String] =
str.split(delimeter).map(_.trim).toSeq
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/org/lemon/advent/year2024/Day18.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import org.lemon.advent.lib.graph._

private object Day18:

def parse(input: String) = input.linesIterator.map(_ match
case s"${Csv[Int](x, y)}" => Coord(x, y)
).toSeq
def parse(input: String) =
import org.lemon.advent.lib.given
input.linesIterator.map(_ match
case s"${Csv[Int](x, y)}" => Coord(x, y)
).toSeq

def adjacency(area: Area, corrupt: Set[Coord])(coord: Coord): Seq[Coord] =
coord.adjacent.filterNot(corrupt).filter(area.contains)
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/org/lemon/advent/year2024/Day19.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import scala.collection.mutable

private object Day19:

def parse(input: String) = input match
case Chunk(towels, targets) => (towels.split(",").map(_.trim).toSeq, targets.linesIterator.toSeq)
def parse(input: String) =
import org.lemon.advent.lib.given
input match
case Chunk(Csv[String](towels @ _*), Wsv[String](targets @ _*)) => (towels, targets)

def countLayouts(towels: Seq[String], target: String) =
val memo = mutable.Map("" -> 1L)
Expand Down

0 comments on commit c05570f

Please sign in to comment.