Skip to content

Commit 8dd7dac

Browse files
committed
feat(aoc-2023): day 09 part 1 and part 2
1 parent 50a6d06 commit 8dd7dac

4 files changed

Lines changed: 263 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.markkovari.adventofcode.day09
2+
3+
type History = List[Long]
4+
5+
def parse(input: String): Seq[Seq[Int]] =
6+
input.linesIterator
7+
.map(_.split(' ').map(_.toInt).toSeq)
8+
.toSeq
9+
10+
def extrapolate(xs: Seq[Int]): Int =
11+
if xs.forall(_ == xs.head)
12+
then xs.head
13+
else
14+
xs.last + extrapolate(
15+
xs.tail
16+
.lazyZip(xs)
17+
.map(_ - _)
18+
)
19+
20+
def part1(input: String): Int =
21+
parse(input)
22+
.map(extrapolate)
23+
.sum
24+
25+
def part2(input: String): Int =
26+
parse(input)
27+
.map(_.reverse)
28+
.map(extrapolate)
29+
.sum
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 3 6 9 12 15
2+
1 3 6 10 15 21
3+
10 13 16 21 30 45

0 commit comments

Comments
 (0)