Skip to content

Commit 011b240

Browse files
committed
Extract isSorted to library
1 parent ce56391 commit 011b240

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/main/scala/eu/sim642/adventofcode2019/Day4.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package eu.sim642.adventofcode2019
22

3+
import eu.sim642.adventofcodelib.SeqImplicits._
4+
35
import scala.Integral.Implicits._
46
import scala.annotation.tailrec
57

@@ -43,23 +45,19 @@ object Day4 {
4345
}
4446
}
4547

46-
def isSorted(list: List[Int]): Boolean = {
47-
(list lazyZip list.tail).forall(_ <= _)
48-
}
49-
5048
object Part1 extends Part {
5149
override def isPassword(number: Int): Boolean = {
5250
val digits = toDigitList(number)
5351
// seems a bit faster to short-circuit isSorted first
54-
isSorted(digits) && runLengthsReverse(digits).exists(_ >= 2)
52+
digits.isSorted && runLengthsReverse(digits).exists(_ >= 2)
5553
}
5654
}
5755

5856
object Part2 extends Part {
5957
override def isPassword(number: Int): Boolean = {
6058
val digits = toDigitList(number)
6159
// seems a bit faster to short-circuit isSorted first
62-
isSorted(digits) && runLengthsReverse(digits).contains(2)
60+
digits.isSorted && runLengthsReverse(digits).contains(2)
6361
}
6462
}
6563

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package eu.sim642.adventofcode2024
22

33
import eu.sim642.adventofcodelib.IteratorImplicits._
4+
import eu.sim642.adventofcodelib.SeqImplicits._
45

56
object Day2 {
67

@@ -15,8 +16,8 @@ object Day2 {
1516
object Part1 extends Part {
1617
override def isSafe(report: Report): Boolean = {
1718
val sorted = report.sorted
18-
val increasing = report == sorted // TODO: add isSorted to library
19-
val decreasing = report.reverse == sorted // TODO: add isSorted to library (with Ordering)
19+
val increasing = report.isSorted
20+
val decreasing = report.isSorted(using Ordering[Int].reverse)
2021
val monotonic = increasing || decreasing
2122
val safeDifferences = report.iterator.zipWithTail.forall({ case (a, b) =>
2223
(1 to 3).contains((a - b).abs)

src/main/scala/eu/sim642/adventofcodelib/SeqImplicits.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package eu.sim642.adventofcodelib
22

33
import eu.sim642.adventofcodelib.IntegralImplicits._
4+
import IteratorImplicits._
45

56
import scala.collection.BuildFrom
67
import scala.collection.generic.IsSeq
78

89
object SeqImplicits {
910

11+
extension [A](seq: Seq[A]) {
12+
def isSorted(using ord: Ordering[A]): Boolean =
13+
seq.iterator.zipWithTail.forall(ord.lteq)
14+
}
15+
1016
extension [Repr](coll: Repr)(using seq: IsSeq[Repr]) {
1117
def rotateLeft[That](n: Int)(using bf: BuildFrom[Repr, seq.A, That]): That = {
1218
val seqOps = seq(coll)

0 commit comments

Comments
 (0)