Last active
January 14, 2022 13:43
-
-
Save sungkmi/cc7bfc5f1e177980b14d8c39879aa1ad to your computer and use it in GitHub Desktop.
This file contains 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 lascala.aoc2021.day1_10.day6 | |
opaque type School = Map[Int, BigInt] | |
extension (s: School) | |
def next: School = | |
val current: School = for (remainingDates, count) <- s if remainingDates > 0 | |
yield (remainingDates - 1, count) | |
s.get(0).fold(current) { zeroesCount => | |
current + | |
(6 -> (zeroesCount + current.getOrElse(6, 0))) + | |
(8 -> zeroesCount) | |
} | |
end next | |
def total: BigInt = s.values.sum | |
def day(n: Int): School = | |
(0 until n).foldLeft(s)((school, _) => school.next) | |
end extension | |
object School: | |
def parse(s: String): School = | |
s.split(",").groupBy(_.toInt).mapValues(x => BigInt(x.size)).toMap | |
def solve1(s: String): BigInt = | |
School.parse(s).day(80).total | |
end solve1 | |
def solve2(s: String): BigInt = | |
School.parse(s).day(256).total | |
end solve2 | |
@main def part1: Unit = | |
val ans = solve1(input) | |
println(ans) | |
@main def part2: Unit = | |
val ans = solve2(input) | |
println(ans) | |
// val input = | |
This file contains 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 lascala.aoc2021.day1_10.day6 | |
import minitest.SimpleTestSuite | |
import hedgehog.minitest.HedgehogSupport | |
import hedgehog.* | |
object Day6Test extends SimpleTestSuite with HedgehogSupport: | |
val testInput = """3,4,3,1,2""" | |
example("day6 - solve1") { | |
solve1(testInput) ==== 5934 | |
} | |
example("day6 - solve2") { | |
solve2(testInput) ==== BigInt("26984457539") | |
} | |
end Day6Test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment