Skip to content

Commit e898130

Browse files
committed
Extract Seq removed to library
1 parent 011b240 commit e898130

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object Day2 {
2828

2929
object Part2 extends Part {
3030
override def isSafe(report: Report): Boolean = {
31-
Part1.isSafe(report) || report.indices.exists(i => Part1.isSafe(report.slice(0, i) ++ report.slice(i + 1, report.size))) // TODO: better seq removal? optimize?
31+
Part1.isSafe(report) || report.indices.view.map(report.removed).exists(Part1.isSafe)
3232
}
3333
}
3434

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ object SeqImplicits {
2323

2424
def rotateRight[That](n: Int)(using BuildFrom[Repr, seq.A, That]): That =
2525
rotateLeft(-n)
26+
27+
def removed[That](index: Int)(using bf: BuildFrom[Repr, seq.A, That]): That =
28+
bf.fromSpecific(coll)(seq(coll).patch(index, Nil, 1))
2629
}
2730

2831
// alternate more limited view-less implementation by OlegYch|h on freenode#scala

0 commit comments

Comments
 (0)