8000 Refactor 2024 day 1 to unzip in parseLists · sim642/adventofcode@8113eb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8113eb1

Browse files
committed
Refactor 2024 day 1 to unzip in parseLists
1 parent 6a06527 commit 8113eb1

File tree

1 file changed

+9
-9
lines changed
  • src/main/scala/eu/sim642/adventofcode2024

1 file changed

+9
-9
lines changed

src/main/scala/eu/sim642/adventofcode2024/Day1.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ import eu.sim642.adventofcodelib.IterableImplicits._
44

55
object Day1 {
66

7-
def totalListDistance(lists: Seq[(Int, Int)]): Int = {
8-
val (list1, list2) = lists.unzip
9-
(list1.sorted lazyZip list2.sorted)
7+
def totalListDistance(lists: (Seq[Int], Seq[Int])): Int = {
8+
val (left, right) = lists
9+
(left.sorted lazyZip right.sorted)
1010
.map((i, j) => (i - j).abs)
1111
.sum
1212
}
1313

14-
def similarityScore(lists: Seq[(Int, Int)]): Int = {
15-
val (list1, list2) = lists.unzip
16-
val list2Count = list2.groupCount(identity).withDefaultValue(0)
17-
list1
18-
.map(i => i * list2Count(i))
14+
def similarityScore(lists: (Seq[Int], Seq[Int])): Int = {
15+
val (left, right) = lists
16+
val rightCount = right.groupCount(identity).withDefaultValue(0)
17+
left
18+
.map(i => i * rightCount(i))
1919
.sum
2020
}
2121

2222
def parsePair(s: String): (Int, Int) = s match {
2323
case s"$i $j" => (i.toInt, j.toInt)
2424
}
2525

26-
def parseLists(input: String): Seq[(Int, Int)] = input.linesIterator.map(parsePair).toSeq
26+
def parseLists(input: String): (Seq[Int], Seq[Int]) = input.linesIterator.map(parsePair).toSeq.unzip
2727

2828
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day1.txt")).mkString.trim
2929

0 commit comments

Comments
 (0)
0