Skip to content

Commit

Permalink
Deduplicate in 2023 day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 9, 2023
1 parent f9f5540 commit 264576f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
40 changes: 18 additions & 22 deletions src/main/scala/eu/sim642/adventofcode2023/Day9.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,26 @@ object Day9 {

type History = Seq[Int]

def nextValue(history: History): Int = {
val differences = LazyList.iterate(history)(history =>
(history lazyZip history.tail).map(-_ + _)
)
val nonZeroDifferences = differences.takeWhile(!_.forall(_ == 0))
nonZeroDifferences
.map(_.last)
.sum
def difference(history: History): History = (history.tail lazyZip history).map(_ - _)

trait Part {
def extrapolate(history: History): Int

def sumExtrapolated(histories: Seq[History]): Int = histories.map(extrapolate).sum
}

def sumNextValues(histories: Seq[History]): Int = histories.map(nextValue).sum

def prevValue(history: History): Int = {
// TODO: deduplicate
val differences = LazyList.iterate(history)(history =>
(history lazyZip history.tail).map(-_ + _)
)
val nonZeroDifferences = differences.takeWhile(!_.forall(_ == 0))
nonZeroDifferences
.map(_.head)
.foldRight(0)((a, b) => a - b)
object Part1 extends Part {
override def extrapolate(history: History): Int = {
LazyList.iterate(history)(difference)
.takeWhile(!_.forall(_ == 0))
.map(_.last)
.sum
}
}

def sumPrevValues(histories: Seq[History]): Int = histories.map(prevValue).sum
object Part2 extends Part {
override def extrapolate(history: History): Int = Part1.extrapolate(history.reverse)
}


def parseHistories(input: String): Seq[History] =
Expand All @@ -36,7 +32,7 @@ object Day9 {
lazy val input: String = io.Source.fromInputStream(getClass.getResourceAsStream("day9.txt")).mkString.trim

def main(args: Array[String]): Unit = {
println(sumNextValues(parseHistories(input)))
println(sumPrevValues(parseHistories(input)))
println(Part1.sumExtrapolated(parseHistories(input)))
println(Part2.sumExtrapolated(parseHistories(input)))
}
}
22 changes: 13 additions & 9 deletions src/test/scala/eu/sim642/adventofcode2023/Day9Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,32 @@ class Day9Test extends AnyFunSuite {
|10 13 16 21 30 45""".stripMargin

test("Part 1 examples") {
import Part1._

val histories = parseHistories(exampleInput)

assert(nextValue(histories(0)) == 18)
assert(nextValue(histories(1)) == 28)
assert(nextValue(histories(2)) == 68)
assert(extrapolate(histories(0)) == 18)
assert(extrapolate(histories(1)) == 28)
assert(extrapolate(histories(2)) == 68)

assert(sumNextValues(histories) == 114)
assert(sumExtrapolated(histories) == 114)
}

test("Part 1 input answer") {
assert(sumNextValues(parseHistories(input)) == 1708206096)
assert(Part1.sumExtrapolated(parseHistories(input)) == 1708206096)
}

test("Part 2 examples") {
import Part2._

val histories = parseHistories(exampleInput)

assert(prevValue(histories(0)) == -3)
assert(prevValue(histories(1)) == 0)
assert(prevValue(histories(2)) == 5)
assert(extrapolate(histories(0)) == -3)
assert(extrapolate(histories(1)) == 0)
assert(extrapolate(histories(2)) == 5)
}

test("Part 2 input answer") {
assert(sumPrevValues(parseHistories(input)) == 1050)
assert(Part2.sumExtrapolated(parseHistories(input)) == 1050)
}
}

0 comments on commit 264576f

Please sign in to comment.