Advent of Code 2023 day 2
12/02/2023, 5:00 AMKroppeb
12/02/2023, 5:08 AMKroppeb
12/02/2023, 5:08 AMKroppeb
12/02/2023, 5:17 AMMarcin Wisniowski
12/02/2023, 5:22 AMMarcin Wisniowski
12/02/2023, 5:28 AMMarcin Wisniowski
12/02/2023, 5:31 AMJoep Eding
12/02/2023, 5:31 AMsubstringBefore
(and after). That's nice! πDan Fingal-Surma
12/02/2023, 5:38 AMNeil Banman
12/02/2023, 5:44 AMGrzegorz Aniol
12/02/2023, 5:54 AMMichael BΓΆiers
12/02/2023, 5:58 AMNorbert Kiesel
12/02/2023, 6:10 AMNeil Banman
12/02/2023, 6:11 AMPoisonedYouth
12/02/2023, 6:20 AMPeter Duong
12/02/2023, 6:27 AMPeter Duong
12/02/2023, 6:28 AMJonathan Kolberg
12/02/2023, 6:37 AMPoisonedYouth
12/02/2023, 6:42 AMmaiatoday
12/02/2023, 6:42 AMMofe Ejegi
12/02/2023, 6:59 AMNeil Banman
12/02/2023, 7:07 AMritesh
12/02/2023, 7:42 AMPaul Woitaschek
12/02/2023, 8:13 AMPaul Woitaschek
12/02/2023, 8:13 AMPaul Woitaschek
12/02/2023, 8:13 AMephemient
12/02/2023, 8:22 AMphldavies
12/02/2023, 8:33 AMphldavies
12/02/2023, 8:34 AMNikita Merinov
12/02/2023, 8:36 AMmerge
method of mutable map.
So you can do like on screen 1 (for
loops will depend on your data structure).
Alternatively, without using buildMap
builder and mutable map inside, you can use groupBy
- ineffective as it will collect list of values, before you will reduce it to max, or groupingBy
with fold
- screen 2 - more effective, as it will reduce on the fly, but I find it harder to udnerstand.Nikita Merinov
12/02/2023, 8:39 AMRiccardo Lippolis
12/02/2023, 8:40 AMList<Map<String, Int>>
π
(probably could have shortened the parsing code by using regex, but it was not a regex morning for me π¬)phldavies
12/02/2023, 8:41 AMmerge
and compute
on the MutableMap
interface.
Also for some reason this morning I was getting
IllegalStateException: CallableReferencesCandidateFactory doesn't support candidates with multiple extension receiver candidates
when using merge
in a buildMap
so opted for compute
π€·Michael de Kaste
12/02/2023, 8:51 AMobject Day2 : Challenge() {
private val parsed: List<Game> = input.lines().map { line ->
line.split(": ").let { (game, subgame) ->
val gameId = game.substringAfter("Game ").toInt()
val subgames = subgame.split("; ").map { cubes ->
SubGame(
cubes.split(", ").map { cube ->
cube.split(" ").let { (amount, color) ->
amount.toInt() to color
}
}.associateBy({ it.second }, { it.first }).withDefault { 0 },
)
}
Game(gameId, subgames)
}
}
class Game(
val id: Int,
private val subGames: List<SubGame>,
) {
fun isValidGame(red: Int, green: Int, blue: Int) =
subGames.all { subgame -> subgame.red <= red && subgame.green <= green && subgame.blue <= blue }
val power = subGames.fold(Triple(0, 0, 0)) { (maxRed, maxBlue, maxGreen), subGame ->
Triple(
max(maxRed, subGame.red),
max(maxBlue, subGame.blue),
max(maxGreen, subGame.green),
)
}.let { (red, green, blue) -> red * green * blue }
}
class SubGame(map: Map<String, Int>) {
val red by map
val green by map
val blue by map
}
override fun part1() = parsed.filter { it.isValidGame(12, 13, 14) }.sumOf { it.id }
override fun part2() = parsed.sumOf(Game::power)
}
Michael de Kaste
12/02/2023, 8:51 AMNikita Merinov
12/02/2023, 8:57 AMval red by map
props.Davio
12/02/2023, 8:57 AMmaxmil
12/02/2023, 9:08 AMknthmn
12/02/2023, 9:55 AMJonathan Kolberg
12/02/2023, 10:13 AMphldavies
12/02/2023, 10:14 AMLovro Ludvig
12/02/2023, 10:45 AMimport java.util.Collections.max
fun main() {
val gameNumberRegex = """(?<=Game )\d+"""
val redRegex = """\d+(?= red)"""
val greenRegex = """\d+(?= green)"""
val blueRegex = """\d+(?= blue)"""
val legalRedNumber = 12
val legalGreenNumber = 13
val legalBlueNumber = 14
fun String.gameNumber() = gameNumberRegex.toRegex().find(this)!!.value.toInt()
fun String.findReds() = redRegex.toRegex().findAll(this).map { it.value.toInt() }.toList()
fun String.findGreens() = greenRegex.toRegex().findAll(this).map { it.value.toInt() }.toList()
fun String.findBlues() = blueRegex.toRegex().findAll(this).map { it.value.toInt() }.toList()
fun checkLegalGames(games: List<String>): Long {
var sum = 0L
games.forEach { game ->
val isLegalGame = game.findReds().all { it <= legalRedNumber }
&& game.findGreens().all { it <= legalGreenNumber }
&& game.findBlues().all { it <= legalBlueNumber }
if (isLegalGame) sum += game.gameNumber()
}
return sum
}
fun powerOfSets(games: List<String>): Long {
var sum = 0L
games.forEach { game ->
val minReds = max(game.findReds())
val minGreens = max(game.findGreens())
val minBlues = max(game.findBlues())
sum += minReds * minGreens * minBlues
}
return sum
}
val games = readInput("Day_02_games")
println(checkLegalGames(games))
println(powerOfSets(games))
}
Rafs
12/02/2023, 10:59 AMdata class GameRecord(val id: Int, val sets: Map<String, Int>)
val adventOfCodeTwo = object : AdventOfCode {
fun List<String>.toGameRecords(): List<GameRecord> = mapIndexed { gameIndex, input ->
val processed = input.substringAfter(":").split(";").map {
it.split(",")
}
val sets = mutableMapOf<String, Int>()
processed.mapIndexed { index, strings ->
strings.map {
val (count, color) = it.trim().split(" ")
sets.put("$index$color", count.toInt())
}
}
GameRecord(id = gameIndex + 1, sets = sets)
}
override fun part1(input: List<String>): Int {
val totalGames = input.toGameRecords()
return totalGames.filter { game ->
game.sets.all { (colorCode, score) ->
val color = colorCode.substring(1)
when (color) {
"red" -> score <= 12
"green" -> score <= 13
"blue" -> score <= 14
else -> true
}
}
}.sumOf { it.id }
}
override fun part2(input: List<String>): Int {
val totalGames = input.toGameRecords()
return totalGames.sumOf { game ->
var maxRed = 0
var maxGreen = 0
var maxBlue = 0
game.sets.forEach { (t, count) ->
val color = t.substring(1)
when (color) {
"red" -> maxRed = max(maxRed, count)
"green" -> maxGreen = max(maxGreen, count)
"blue" -> maxBlue = max(maxBlue, count)
}
}
maxRed * maxGreen * maxBlue
}
}
}
Tolly Kulczycki
12/02/2023, 11:12 AMphldavies
12/02/2023, 11:16 AMOzioma Ogbe
12/02/2023, 11:32 AMfun main() {
println(File("input1.txt").useLines { lines ->
lines.map { line ->
val splitData = line.split(":")[1]
(Regex("\\d+(?= green)")).findAll(splitData).map { it.value.toInt() }.max() *
(Regex("\\d+(?= red)")).findAll(splitData).map { it.value.toInt() }
.max() * (Regex("\\d+(?= blue)")).findAll(splitData).map { it.value.toInt() }.max()
}.sum()
})
}
xxfast
12/02/2023, 11:33 AMraphadlts
12/02/2023, 1:14 PMJakub GwΓ³ΕΊdΕΊ
12/02/2023, 2:22 PMkotlinx.serialization
style (probably not the best approach, but my first π )Ozioma Ogbe
12/02/2023, 3:35 PMStefano Sansone
12/02/2023, 4:34 PMfun main() {
val games = readInput("Day02")
val validGamesSum = games.sumOf { game -> getValidGameId(game) }
println("Part 1 answer: $validGamesSum")
val power = games.sumOf { game -> getPowerForGame(game) }
println("Part 2 answer: $power")
}
val cubesLimit = mapOf(
"red" to 12,
"green" to 13,
"blue" to 14
)
val cubesPower = mutableMapOf<String, Int>()
fun getValidGameId(game: String): Int {
println(game.parseGameSets())
game.parseGameSets().forEach { (color, count) ->
if (count > (cubesLimit[color] ?: 0)) return 0
}
return game.substringBefore(":").filter { it.isDigit() }.toInt()
}
fun getPowerForGame(game: String): Int {
game.parseGameSets().forEach { (color, count) ->
cubesPower[color] = maxOf(cubesPower.getOrDefault(color, 0), count)
}
return cubesPower.values.fold(1) { acc, value -> acc * value }.also { cubesPower.clear() }
}
fun String.parseGameSets(): List<Pair<String, Int>> =
split(":").last().split(";")
.flatMap { set ->
set.split(",").map { cubes ->
val color = cubes.filter { it.isLetter() }
val count = cubes.filter { it.isDigit() }.toInt()
color to count
}
}
Davio
12/02/2023, 5:29 PMs.split("; ")
will work just fineraphadlts
12/02/2023, 8:43 PMphldavies
12/02/2023, 8:47 PMjoney
12/03/2023, 12:09 AMgroupingBy
& aggregate
KJacek Rondio
12/03/2023, 1:08 PM