File tree Expand file tree Collapse file tree 3 files changed +13
-8
lines changed Expand file tree Collapse file tree 3 files changed +13
-8
lines changed Original file line number Diff line number Diff line change 11package eu .sim642 .adventofcode2019
22
3+ import eu .sim642 .adventofcodelib .SeqImplicits ._
4+
35import scala .Integral .Implicits ._
46import 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
Original file line number Diff line number Diff line change 11package eu .sim642 .adventofcode2024
22
33import eu .sim642 .adventofcodelib .IteratorImplicits ._
4+ import eu .sim642 .adventofcodelib .SeqImplicits ._
45
56object 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)
Original file line number Diff line number Diff line change 11package eu .sim642 .adventofcodelib
22
33import eu .sim642 .adventofcodelib .IntegralImplicits ._
4+ import IteratorImplicits ._
45
56import scala .collection .BuildFrom
67import scala .collection .generic .IsSeq
78
89object 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)
You can’t perform that action at this time.
0 commit comments