generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.kt
91 lines (78 loc) · 2.51 KB
/
Day02.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* DAY 02 - Cubes in a Bag
*
* This was pretty straightforward
*/
package aoc2023
import utils.println
import utils.readInput
fun main() {
val cubeLimits = mapOf("red" to 12, "green" to 13, "blue" to 14)
/**
* For a given game, find the max of each colour
* Returns id and map of max counts
*/
fun maxCounts(game: String): Pair<Int, Map<String, Int>> {
// "Game XX: subset; subset; subset; subset"
val gameInfo = game.split(": ")
val id = gameInfo[0].substring(5).toInt()
val subsets = gameInfo[1].split("; ")
// Parse subsets to determine max counts for each colour
val maxCount = mutableMapOf("red" to 0, "green" to 0, "blue" to 0)
for (subset in subsets) {
// "X colour, X colour, X colour"
val cubes = subset.split(", ")
for (cube in cubes) {
val cubeInfo = cube.split(" ")
val cubeCount = cubeInfo[0].toInt()
val cubeColour = cubeInfo[1]
if (cubeCount > maxCount[cubeColour]!!) {
maxCount[cubeColour] = cubeCount
}
}
}
return Pair(id, maxCount)
}
/**
* Find total of game IDs that are valid
*/
fun gameIDs(gameList: List<String>): Int {
var total = 0
for (game in gameList) {
val (id, maxCount) = maxCounts(game)
// Check if this subset is possible
var possible = true
for ((colour, count) in maxCount) {
if (count > cubeLimits[colour]!!) {
possible = false
}
}
// If we're good, can add in
if (possible) total += id
}
return total
}
/**
* Find total of game powers (max r * max g * max b)
*/
fun gamePowers(gameList: List<String>): Int {
var total = 0
for (game in gameList) {
val maxCount = maxCounts(game).second
// Calc power of this game = max r * g * b
var power = 1
for (count in maxCount.values) {
power *= count
}
total += power
}
return total
}
// test if implementation meets criteria from the description, like:
val testInput = readInput(2023, "Day02_test")
check(gameIDs(testInput) == 8)
check(gamePowers(testInput) == 2286)
val input = readInput(2023, "Day02")
gameIDs(input).println()
gamePowers(input).println()
}