-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay09.scala
27 lines (19 loc) · 955 Bytes
/
Day09.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.lmat.adventofcode.year2023
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.util.Files.readResource
object Day09 extends SimpleCommonPuzzle[List[List[Int]], Long, Long] {
override def parse(resource: String): List[List[Int]] =
readResource(resource).toList.map(parseRow)
def parseRow(row: String): List[Int] =
row.split(" ").flatMap(_.toIntOption).toList
override def part1(oasis: List[List[Int]]): Long =
oasis.map(expand).map(history).sum
def expand(start: List[Int]): List[List[Int]] =
LazyList.iterate(start)(prev => prev.zip(prev.drop(1)).map { case (a, b) => b - a }).takeWhile(_.exists(_ != 0)).toList
def history(history: List[List[Int]]): Int =
history.reverse.map(_.last).sum
override def part2(oasis: List[List[Int]]): Long =
oasis.map(expand).map(history2).sum
def history2(history: List[List[Int]]): Int =
history.reverse.map(_.head).foldLeft(0)((s, c) => c - s)
}