Last active
January 18, 2022 06:38
-
-
Save RubenVerg/f2b0d19552bd4375fa1a7c84b3b97b54 to your computer and use it in GitHub Desktop.
day6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rubenverg.aoc2021 | |
import utils.* | |
object Day6 extends Day: | |
def run(iterations: Int, input: String) = | |
// `Int#times` just takes an initial input and then calls the given function that many times | |
// that weird `groupBy` and `view` and `mapValue` stuff essentially just takes the list and turns it into a map of item -> count | |
iteration.times(input.split(',').map(_.trim.toInt).groupBy(identity).view.mapValues(s => BigInt(s.size)).toMap) { map => | |
Map( | |
0 -> map.getOrElse(1, BigInt(0)), | |
1 -> map.getOrElse(2, BigInt(0)), | |
2 -> map.getOrElse(3, BigInt(0)), | |
3 -> map.getOrElse(4, BigInt(0)), | |
4 -> map.getOrElse(5, BigInt(0)), | |
5 -> map.getOrElse(6, BigInt(0)), | |
6 -> (map.getOrElse(7, BigInt(0)) + map.getOrElse(0, BigInt(0))), | |
7 -> map.getOrElse(8, BigInt(0)), | |
8 -> map.getOrElse(0, BigInt(0)) | |
) | |
} | |
def part1(input: String) = | |
run(80, input).values.sum | |
def part2(input: String) = | |
run(256, input).values.sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment