Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 9, 2023
1 parent 0a7dfa9 commit cbcb948
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion scala2/src/main/scala/jurisk/adventofcode/Advent00.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object Advent00 {
def parse(s: String): Command =
s match {
case "noop" => Noop
case s"something $rem" => Something(rem.extractInts)
case s"something $rem" => Something(rem.extractIntList)
case s if s.nonEmpty => Other(s)
case _ => s.failedToParse
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Advent10 {

object PointWithVelocity {
def parse(s: String): PointWithVelocity = {
val List(a, b, c, d) = s.extractInts
val List(a, b, c, d) = s.extractIntList
PointWithVelocity(Coords2D.of(a, b), Coords2D.of(c, d))
}
}
Expand Down
10 changes: 5 additions & 5 deletions scala2/src/main/scala/jurisk/adventofcode/y2022/Advent11.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Advent11 {
val Array(m, si, o, t, iT, iF) = s.split("\n")

val logic = MonkeyLogic(
index = m.extractInts.singleElementUnsafe,
index = m.extractIntList.singleElementUnsafe,
operation = o match {
case " Operation: new = old * old" =>
old => old * old
Expand All @@ -47,12 +47,12 @@ object Advent11 {

case _ => sys.error(s"Did not recognize $o")
},
testIsDivisibleBy = t.extractInts.singleElementUnsafe,
ifTrueThrowTo = iT.extractInts.singleElementUnsafe,
ifFalseThrowTo = iF.extractInts.singleElementUnsafe,
testIsDivisibleBy = t.extractIntList.singleElementUnsafe,
ifTrueThrowTo = iT.extractIntList.singleElementUnsafe,
ifFalseThrowTo = iF.extractIntList.singleElementUnsafe,
)

val itemStates = si.extractInts.map(_.toLong) map { x =>
val itemStates = si.extractIntList.map(_.toLong) map { x =>
ItemState(
logic.index,
x,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object Advent04 {
case s"Card$id: $winning | $youHave" =>
Card(
id.trim.toInt,
winning.extractInts.toSet,
youHave.extractInts.toSet,
winning.extractIntList.toSet,
youHave.extractIntList.toSet,
)
case _ => s.failedToParse
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ object Advent05 extends IOApp.Simple {

private object Converter {
def parse(input: String): Converter = {
val List(a, b, c) = input.extractLongs
val List(a, b, c) = input.extractLongList
Converter(a, b, c)
}
}

def parse(input: String): Input = {
val sections = input.split("\n\n").toList
sections match {
case h :: t => Input(h.extractLongs, t map ConversionMap.parse)
case h :: t => Input(h.extractLongList, t map ConversionMap.parse)
case _ => input.failedToParse
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Advent09 {
def part1(input: Input): N = input.map(extrapolatedValue).sum
def part2(input: Input): N = part1(input.map(_.reverse))

def parse(input: String): Input = input.parseLines(_.extractLongs)
def parse(input: String): Input = input.parseLines(_.extractLongList)

def parseFile(fileName: String): Input =
parse(readFileText(fileName))
Expand Down
20 changes: 14 additions & 6 deletions scala2/src/main/scala/jurisk/utils/Parsing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import jurisk.geometry.Coords2D
import jurisk.utils.CollectionOps.IterableOps

import scala.annotation.tailrec
import scala.collection.immutable.ArraySeq
import scala.util.matching.Regex

object Parsing {

Expand Down Expand Up @@ -79,15 +81,21 @@ object Parsing {
parser: String => T
): List[T] = parseList("\n", parser)

def extractInts: List[Int] = {
private def extractDigitStrings: Regex.MatchIterator = {
val RegEx = """([-+]?\d+)""".r
RegEx.findAllIn(s).map(_.toInt).toList
RegEx.findAllIn(s)
}

def extractLongs: List[Long] = {
val RegEx = """([-+]?\d+)""".r
RegEx.findAllIn(s).map(_.toLong).toList
}
def extractInts: Iterator[Int] = extractDigitStrings.map(_.toInt)
def extractLongs: Iterator[Long] = extractDigitStrings.map(_.toLong)

def extractIntList: List[Int] = extractInts.toList
def extractLongList: List[Long] = extractLongs.toList

def extractIntVector: Vector[Int] = extractInts.toVector
def extractLongVector: Vector[Long] = extractLongs.toVector

def extractIntArraySeq: ArraySeq[Int] = ArraySeq.from(extractInts)
def extractLongArraySeq: ArraySeq[Long] = ArraySeq.from(extractLongs)
}
}

0 comments on commit cbcb948

Please sign in to comment.