-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.kt
72 lines (65 loc) · 2.19 KB
/
Day2.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
package aoc2023.day02
fun parse(lines: List<String>): List<Game> {
val games = mutableListOf<Game>()
lines.forEach { line ->
val gameData = line.split(": ")
val roundData = gameData[1].split("; ")
val gameRounds = mutableListOf<Round>()
roundData.forEach { round ->
val colors = round.split(", ")
val gameRound = Round()
colors.forEach { color ->
if (color.contains("blue")) {
gameRound.numBlue = extractNum(color)
} else if (color.contains("green")) {
gameRound.numGreen = extractNum(color)
} else {
gameRound.numRed = extractNum(color)
}
}
gameRounds.add(gameRound)
}
games.add(Game(extractNum(gameData[0]), gameRounds))
}
return games
}
fun part1(games: List<Game>): Int {
val bagContent = Round(14, 13, 12)
var sum = 0
games.forEach { game ->
val highestVals = game.findHighestValues()
if (highestVals["maxBlue"]!! <= bagContent.numBlue!!
&& highestVals["maxGreen"]!! <= bagContent.numGreen!!
&& highestVals["maxRed"]!! <= bagContent.numRed!!) {
sum += game.id!!
}
}
return sum
}
fun part2(games: List<Game>): Int {
var sum = 0
games.forEach { game ->
val highestVals = game.findHighestValues()
sum += highestVals["power"]!!
}
return sum
}
data class Game(val id: Int?, val rounds: List<Round>) {
fun findHighestValues(): Map<String, Int> {
val maxBlue = rounds.maxOfOrNull { it.numBlue ?: 0 } ?: 0
val maxGreen = rounds.maxOfOrNull { it.numGreen ?: 0 } ?: 0
val maxRed = rounds.maxOfOrNull { it.numRed ?: 0 } ?: 0
return mapOf(
"maxBlue" to maxBlue,
"maxGreen" to maxGreen,
"maxRed" to maxRed,
"power" to maxBlue * maxGreen * maxRed
)
}
}
data class Round(var numBlue: Int? = 0, var numGreen: Int? = 0, var numRed: Int? = 0)
fun extractNum(input: String): Int? {
val regex = Regex("\\d+")
val matchResult = regex.find(input)
return matchResult?.value?.toInt()
}