Skip to content

Commit

Permalink
Day 1 improvements: unzip and map+sum to foldMap
Browse files Browse the repository at this point in the history
  • Loading branch information
horothesun committed Dec 1, 2024
1 parent dc7c39c commit edbf0aa
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/scala/Day01.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ object Day01:

case class Input(left: List[LocationId], right: List[LocationId]):

def totalDistance: Long = left.sorted.zip(right.sorted).map((l, r) => Math.abs(l.value - r.value).toLong).sum
def totalDistance: Long = left.sorted.zip(right.sorted).foldMap((l, r) => Math.abs(l.value - r.value).toLong)

def similarityScore: Long =
val rightCount = right.groupBy(identity).map((k, vs) => (k, vs.length))
left.map(l => (l.value * rightCount.getOrElse(l, 0)).toLong).sum
left.foldMap(l => (l.value * rightCount.getOrElse(l, 0)).toLong)

object Input:
def parse(inputs: List[String]): Option[Input] =
Expand All @@ -22,10 +22,8 @@ object Day01:
case Array(l, r) => (l.toIntOption, r.toIntOption).tupled
case _ => None
}.map { lrs =>
Input(
left = lrs.map(_._1).map(LocationId.apply),
right = lrs.map(_._2).map(LocationId.apply)
)
val (l, r) = lrs.unzip
Input(left = l.map(LocationId.apply), right = r.map(LocationId.apply))
}

def totalDistance(inputs: List[String]): Option[Long] = Input.parse(inputs).map(_.totalDistance)
Expand Down

0 comments on commit edbf0aa

Please sign in to comment.