Skip to content

Commit 513b851

Browse files
committed
feat(day06): both parts
1 parent 16052e4 commit 513b851

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

day06.sc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import common.loadPackets
2+
3+
type Population = Map[Int, Long]
4+
val input = loadPackets(List("day06.txt")).head.split(",").map(_.toInt).toList
5+
6+
val initialState: Population = input.groupBy(identity).view.mapValues(_.size.toLong).toMap
7+
8+
def next(state: Population): Population = state.map {
9+
case (timer, amount) if timer == 0 => List((6, amount), (8, amount))
10+
case (timer, amount) => List((timer - 1, amount))
11+
}.toList.flatten
12+
.foldLeft(Map[Int, Long]()) {
13+
case (map, (timer, amount)) =>
14+
map + (timer -> (map.getOrElse(timer, 0L) + amount))
15+
}
16+
17+
val part1 = Range(0, 80).foldLeft(initialState)((state, _) => next(state)).values.sum
18+
val part2 = Range(0, 256).foldLeft(initialState)((state, _) => next(state)).values.sum
19+

src/main/resources/day06.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5,3,2,2,1,1,4,1,5,5,1,3,1,5,1,2,1,4,1,2,1,2,1,4,2,4,1,5,1,3,5,4,3,3,1,4,1,3,4,4,1,5,4,3,3,2,5,1,1,3,1,4,3,2,2,3,1,3,1,3,1,5,3,5,1,3,1,4,2,1,4,1,5,5,5,2,4,2,1,4,1,3,5,5,1,4,1,1,4,2,2,1,3,1,1,1,1,3,4,1,4,1,1,1,4,4,4,1,3,1,3,4,1,4,1,2,2,2,5,4,1,3,1,2,1,4,1,4,5,2,4,5,4,1,2,1,4,2,2,2,1,3,5,2,5,1,1,4,5,4,3,2,4,1,5,2,2,5,1,4,1,5,1,3,5,1,2,1,1,1,5,4,4,5,1,1,1,4,1,3,3,5,5,1,5,2,1,1,3,1,1,3,2,3,4,4,1,5,5,3,2,1,1,1,4,3,1,3,3,1,1,2,2,1,2,2,2,1,1,5,1,2,2,5,2,4,1,1,2,4,1,2,3,4,1,2,1,2,4,2,1,1,5,3,1,4,4,4,1,5,2,3,4,4,1,5,1,2,2,4,1,1,2,1,1,1,1,5,1,3,3,1,1,1,1,4,1,2,2,5,1,2,1,3,4,1,3,4,3,3,1,1,5,5,5,2,4,3,1,4

0 commit comments

Comments
 (0)