Skip to content

Instantly share code, notes, and snippets.

@RubenVerg
Last active January 18, 2022 06:38
Show Gist options
  • Save RubenVerg/f2b0d19552bd4375fa1a7c84b3b97b54 to your computer and use it in GitHub Desktop.
Save RubenVerg/f2b0d19552bd4375fa1a7c84b3b97b54 to your computer and use it in GitHub Desktop.
day6
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