From 8d10405950b04e0eb54530de884e940b6bcd5382 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sat, 2 Dec 2023 16:03:20 -0700 Subject: [PATCH] Add solution for day 2 --- src/Day02.kt | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/Day02.kt diff --git a/src/Day02.kt b/src/Day02.kt new file mode 100644 index 0000000..191755d --- /dev/null +++ b/src/Day02.kt @@ -0,0 +1,72 @@ +fun main() { + fun part1(input: List): Int { + val maxCubes = mapOf( + "red" to 12, + "green" to 13, + "blue" to 14 + ) + return input.map { Game.parse(it) } + .sumOf { game -> + val validGame = game.subsets.all { subset -> + subset.none { (color, count) -> + (count > maxCubes[color]!!) + } + } + if (validGame) { + game.id + } else { + 0 + } + } + } + + fun part2(input: List): Int { + return input.map { Game.parse(it) } + .sumOf { game -> + val minNecessary = mutableMapOf( + "blue" to 0, + "green" to 0, + "red" to 0 + ) + game.subsets.forEach { subset -> + subset.forEach { (color, count) -> + if (count > minNecessary[color]!!) { + minNecessary[color] = count + } + } + } + var power = 1 + minNecessary.values.forEach { power *= it } + power + } + } + + // test if implementation meets criteria from the description, like: + val testInput = readInput("Day02_test") + check(part1(testInput) == 8) + check(part2(testInput) == 2286) + + val input = readInput("Day02") + part1(input).println() + part2(input).println() +} + +data class Game(val id: Int, val subsets: Set>) { + companion object { + fun parse(s: String): Game { + val colonPosition = s.indexOf(':') + val id = s.substring(5, colonPosition).toInt() + val subsets = s.substring(colonPosition + 2) + .split(';') + .map { set -> + set.split(',') + .associate { + val parts = it.trim().split(' ') + parts[1] to parts[0].toInt() + } + } + .toSet() + return Game(id, subsets) + } + } +}