🎄Advent of Code - Day 2 part 1

This commit is contained in:
Denis-Cosmin Nutiu 2023-12-02 12:01:00 +02:00
parent 64844be1e5
commit 73ada7c76c
10 changed files with 218 additions and 1020 deletions

View file

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration"> <component name="FrameworkDetectionExcludesConfiguration">

View file

@ -1,7 +1,7 @@
import day1.Trebuchet import adventOfCode2023.day2.CubeConundrum
fun main(args: Array<String>) { fun main(args: Array<String>) {
val t = Trebuchet() val t = CubeConundrum()
// t.partOne() t.partOne()
t.partTwo() // t.partTwo()
} }

View file

@ -0,0 +1,18 @@
package adventOfCode
interface Puzzle {
// Part one of the AoC puzzle.
fun partOne()
// Part two of the AoC puzzle.
fun partTwo()
fun readInputFromFile(dayNumber: String): List<String> {
// read calibrations from resources file
this.javaClass.getResourceAsStream("/aoc2023/input_day$dayNumber.txt")?.bufferedReader()?.useLines { lines ->
return lines.toList()
}
throw Exception("Could not read calibrations from file.")
}
}

View file

@ -1,11 +1,11 @@
package day1 package adventOfCode2023.day1
import java.io.File import adventOfCode.Puzzle
class Trebuchet { class Trebuchet : Puzzle {
fun partOne() { override fun partOne() {
// read calibrations from file // read calibrations from file
val calibrations = readCalibrationsFromFile() val calibrations = readInputFromFile("1")
// print calibrations // print calibrations
val calibrationData = calibrations.map { val calibrationData = calibrations.map {
// replace non-digits with empty string // replace non-digits with empty string
@ -20,9 +20,9 @@ class Trebuchet {
println("Part1: The calibration data is $calibrationData.") println("Part1: The calibration data is $calibrationData.")
} }
fun partTwo() { override fun partTwo() {
// read calibrations from file // read calibrations from file
val calibrations = readCalibrationsFromFile() val calibrations = this.readInputFromFile("1")
// print calibrations // print calibrations
val calibrationData = calibrations.map { it -> val calibrationData = calibrations.map { it ->
val digitsData = mapOf( val digitsData = mapOf(
@ -72,12 +72,4 @@ class Trebuchet {
println("Part2: The calibration data is $calibrationData.") println("Part2: The calibration data is $calibrationData.")
} }
private fun readCalibrationsFromFile(): List<String> {
// read calibrations from resources file
this.javaClass.getResourceAsStream("/day1/calibration.txt")?.bufferedReader()?.useLines { lines ->
return lines.toList()
}
throw Exception("Could not read calibrations from file.")
}
} }

View file

@ -0,0 +1,59 @@
package adventOfCode2023.day2
import adventOfCode.Puzzle
import java.util.Scanner
class CubeConundrum : Puzzle {
private fun getGameList(): List<Game> {
val input = this.readInputFromFile("2")
return input.map {
val game = Game(0, null)
val line = it.split(":", limit = 2)
// Get game id
val scanner = Scanner(line[0])
scanner.next() // skip Game
game.id = scanner.nextInt()
// Get cubes numbers.
game.gameData = line[1].split(";")
return@map game
}
}
override fun partOne() {
/*
The Elf would first like to know which games would have been possible if the bag
contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
*/
val games = getGameList();
var gameIdsSum = 0
val gameMaxConstraints = mapOf(
"red" to 12,
"green" to 13,
"blue" to 14
)
games.forEach {
println(it)
var gamePossible = true
it.gameData?.forEach { gameData ->
val cubes = gameData.split(",")
cubes.forEach cubeForEach@ { cubeSet ->
val cubeData = cubeSet.trim().split(" ")
if (cubeData[0].toInt() > gameMaxConstraints.getOrDefault(cubeData[1], Int.MAX_VALUE)) {
gamePossible = false
return@cubeForEach
}
}
}
if (gamePossible) {
gameIdsSum += it.id
}
}
println("The sum of the game ids is $gameIdsSum.")
}
override fun partTwo() {
TODO("Not yet implemented")
}
}

View file

@ -0,0 +1,3 @@
package adventOfCode2023.day2
data class Game(var id: Int, var gameData: List<String>?)

View file

@ -0,0 +1,27 @@
--- Day 2: Cube Conundrum ---
You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over to greet you.
The Elf explains that you've arrived at Snow Island and apologizes for the lack of snow. He'll be happy to explain the situation, but it's a bit of a walk, so you have some time. They don't get many visitors up here; would you like to play a game in the meantime?
As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes.
To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He'll do this a few times per game.
You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11 in Game 11: ...) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue).
For example, the record of a few games might look like this:
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
In game 1, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes.
The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get 8.
Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games?
To play, please identify yourself via one of these services:

View file

@ -0,0 +1,100 @@
Game 1: 4 red, 5 blue, 9 green; 7 green, 7 blue, 3 red; 16 red, 7 blue, 3 green; 11 green, 11 blue, 6 red; 12 red, 14 blue
Game 2: 12 blue, 11 green, 3 red; 6 blue, 5 green, 7 red; 5 red, 11 blue; 2 blue, 8 green
Game 3: 8 blue, 5 green, 2 red; 5 blue, 5 green, 7 red; 7 blue, 1 green, 7 red; 8 green, 14 blue, 7 red; 8 green, 14 blue; 8 blue, 2 green, 8 red
Game 4: 3 red, 14 blue, 15 green; 1 red, 11 green, 14 blue; 14 green, 17 blue
Game 5: 11 green, 2 red, 10 blue; 16 green, 8 blue; 2 blue, 6 green, 1 red; 14 blue, 2 red, 16 green; 3 blue, 18 green; 1 red, 10 blue, 3 green
Game 6: 2 green, 5 red, 17 blue; 12 green, 13 blue, 6 red; 8 red, 9 blue, 7 green
Game 7: 2 blue, 18 green; 4 green, 1 red, 1 blue; 4 blue, 19 green
Game 8: 6 green, 7 blue; 9 green, 6 blue; 7 blue, 1 red, 3 green
Game 9: 4 blue, 12 green, 3 red; 4 green, 3 blue, 3 red; 3 green, 2 red, 3 blue; 1 green, 2 red, 3 blue; 15 red, 10 green, 4 blue; 3 blue, 1 red, 6 green
Game 10: 11 blue, 6 green, 6 red; 15 green, 1 blue; 1 red, 6 blue, 4 green
Game 11: 9 blue, 1 red, 6 green; 6 red, 1 green; 10 blue, 3 green, 6 red
Game 12: 1 blue, 10 red, 1 green; 4 blue, 4 red; 8 red, 3 blue, 1 green; 3 red, 2 green
Game 13: 3 red, 11 green, 18 blue; 11 green, 1 red, 3 blue; 12 blue, 5 red, 2 green; 16 blue, 8 red, 5 green; 8 red, 12 blue, 19 green; 17 blue, 4 green, 6 red
Game 14: 8 red, 4 blue; 1 green, 2 blue, 13 red; 1 green, 1 blue, 17 red; 1 green, 13 red
Game 15: 4 red, 3 blue, 6 green; 4 blue, 3 red, 3 green; 3 green, 6 red, 3 blue; 6 red, 5 blue, 2 green; 6 green, 1 blue; 4 green, 3 red, 2 blue
Game 16: 11 green; 3 green, 1 blue, 1 red; 12 green, 3 blue, 1 red; 1 red, 1 green; 1 red, 3 blue; 2 green, 1 blue, 1 red
Game 17: 12 red, 14 blue, 10 green; 2 red, 6 green, 6 blue; 10 blue, 2 green, 3 red; 1 red, 13 blue, 2 green; 9 green, 16 red, 9 blue
Game 18: 15 red, 8 blue; 16 red, 12 blue; 5 blue, 4 green, 6 red; 8 red, 4 green, 3 blue; 7 red, 5 blue, 2 green; 1 blue, 2 green, 14 red
Game 19: 3 red, 13 blue, 2 green; 8 red, 14 blue; 9 blue, 3 green; 9 blue, 1 green, 7 red; 8 red, 1 green; 8 red, 14 blue, 2 green
Game 20: 6 green, 10 blue, 5 red; 8 green, 9 blue, 7 red; 2 red, 2 green, 7 blue; 7 red, 16 green, 12 blue; 15 green, 3 red; 12 green, 3 red, 6 blue
Game 21: 2 green, 7 blue; 7 blue, 6 red; 6 blue, 2 red, 1 green; 11 blue, 1 red, 3 green
Game 22: 5 red, 1 blue; 1 green, 2 red; 1 blue, 1 green, 5 red; 1 green, 2 blue, 4 red; 1 green, 3 red, 1 blue; 5 red, 3 blue, 1 green
Game 23: 12 green, 7 red; 4 blue, 15 red, 2 green; 2 green, 16 red, 2 blue; 5 red, 10 green, 1 blue; 1 red, 4 green, 7 blue; 9 blue, 4 green, 12 red
Game 24: 7 blue, 11 red, 4 green; 6 blue, 3 green; 1 blue, 14 red, 1 green; 2 blue, 4 green, 15 red; 7 red, 4 blue; 7 red, 2 blue, 3 green
Game 25: 6 green, 10 red, 12 blue; 3 red, 16 blue; 10 blue, 10 red, 1 green; 3 red, 2 green, 13 blue; 2 green, 11 blue, 6 red
Game 26: 6 red, 4 blue, 10 green; 3 blue, 1 green, 4 red; 5 blue; 11 blue, 9 green, 7 red
Game 27: 1 red, 14 green, 9 blue; 13 green, 8 blue, 8 red; 7 red, 8 green, 6 blue; 7 blue, 3 red, 4 green; 18 green, 3 red
Game 28: 2 red, 1 blue, 1 green; 1 blue; 1 red, 6 green, 2 blue; 5 green, 1 blue; 6 green; 2 green, 5 blue, 2 red
Game 29: 2 blue, 2 green, 7 red; 3 red, 5 blue; 7 green, 14 blue, 3 red
Game 30: 5 red, 11 green, 8 blue; 1 blue, 1 red, 15 green; 18 green, 12 blue; 5 red, 6 blue, 16 green; 12 blue, 1 red, 5 green
Game 31: 1 blue, 7 red, 2 green; 8 green, 1 blue; 3 blue, 13 green, 2 red; 3 blue, 7 red
Game 32: 10 green, 7 blue, 4 red; 18 green, 4 blue, 7 red; 5 blue, 6 red; 5 red, 5 blue, 1 green; 12 blue, 5 green, 8 red; 1 red, 6 green, 13 blue
Game 33: 1 green, 2 red, 18 blue; 12 blue, 8 green; 8 green, 1 red, 16 blue; 10 green, 14 blue; 1 red, 3 blue, 8 green
Game 34: 4 red, 4 blue; 10 blue, 8 red; 2 green, 5 blue, 20 red
Game 35: 6 blue, 1 green, 4 red; 1 red, 2 blue, 2 green; 12 blue, 2 red; 11 blue, 1 green, 1 red; 2 blue; 2 red, 10 blue, 3 green
Game 36: 2 green, 9 blue, 11 red; 5 blue, 11 red; 1 green, 1 red, 9 blue; 8 blue, 2 green; 11 red, 4 blue, 1 green; 7 blue, 2 green, 5 red
Game 37: 10 blue, 5 green, 6 red; 5 red, 13 green, 10 blue; 1 green, 7 blue, 4 red; 10 green, 4 blue, 14 red; 13 green, 9 red, 1 blue
Game 38: 5 blue, 4 green, 4 red; 4 blue, 11 green; 4 green, 3 red; 6 green, 7 blue; 6 blue, 1 red, 2 green
Game 39: 8 blue, 1 green, 19 red; 15 red, 2 green, 7 blue; 1 green, 6 blue, 8 red; 16 red, 3 blue, 1 green
Game 40: 4 green, 1 blue; 1 red, 2 blue, 3 green; 4 green, 1 blue, 2 red; 2 green, 1 blue, 2 red; 2 green
Game 41: 3 red, 4 blue; 2 blue, 16 green; 2 red, 5 blue, 11 green; 13 green, 3 red, 6 blue; 3 blue, 19 green, 3 red; 5 green, 1 red, 3 blue
Game 42: 6 green; 13 green, 1 blue; 1 blue, 5 green, 1 red; 1 blue, 6 green, 1 red; 2 red, 2 green, 1 blue
Game 43: 8 green, 11 blue; 11 green, 12 blue; 1 blue, 5 red, 7 green; 1 blue, 11 green; 3 blue, 1 green
Game 44: 3 green, 18 red, 16 blue; 2 blue, 2 green, 14 red; 13 red, 4 green, 17 blue; 3 red, 9 blue, 8 green; 11 red, 1 blue; 5 blue, 3 red, 7 green
Game 45: 11 blue, 2 red; 8 green, 5 blue, 1 red; 14 blue, 5 green; 14 blue, 8 green; 10 blue, 11 green; 5 green, 1 red, 17 blue
Game 46: 3 red, 3 blue, 1 green; 2 green, 7 red, 4 blue; 2 red, 1 green, 2 blue; 9 red, 1 green
Game 47: 3 blue, 4 red, 2 green; 9 blue, 12 green, 11 red; 8 green, 19 red, 7 blue
Game 48: 1 green, 9 red; 7 green, 16 red, 1 blue; 2 blue, 2 red, 5 green; 19 red, 3 blue, 2 green
Game 49: 2 green, 17 blue, 18 red; 4 blue, 19 red, 11 green; 1 green, 5 blue, 15 red; 10 green, 6 red, 1 blue
Game 50: 4 green, 8 blue, 6 red; 1 red, 6 blue, 4 green; 6 red, 5 green, 10 blue; 7 blue, 6 red
Game 51: 2 green, 1 blue, 5 red; 13 blue, 10 green; 6 green, 1 red, 7 blue; 4 red, 3 green, 8 blue; 3 red, 9 blue, 4 green; 2 red, 12 blue, 8 green
Game 52: 1 blue, 4 green, 6 red; 6 green, 6 red, 10 blue; 4 green, 3 red, 5 blue; 3 blue, 2 green, 4 red; 6 green, 5 blue, 9 red; 9 red, 5 green, 8 blue
Game 53: 2 green, 6 blue, 6 red; 1 blue, 5 green, 13 red; 7 red, 5 blue, 1 green
Game 54: 5 green, 6 blue, 2 red; 1 blue, 3 green; 6 green; 1 red, 2 blue, 5 green; 5 blue, 5 green
Game 55: 9 blue, 15 green; 4 red, 1 green; 7 blue, 9 red, 11 green
Game 56: 7 red, 2 blue, 4 green; 2 blue, 6 red, 6 green; 8 red, 7 green; 6 green, 2 red; 3 blue, 2 green, 7 red
Game 57: 1 blue, 1 green, 1 red; 6 red, 2 green, 3 blue; 1 green; 3 blue, 8 red, 2 green
Game 58: 14 blue, 5 red, 14 green; 5 blue, 7 green, 7 red; 19 blue, 10 red, 14 green; 7 green, 5 blue, 10 red; 2 red, 12 green, 2 blue
Game 59: 1 red, 1 green, 1 blue; 10 blue; 3 blue, 1 green, 1 red
Game 60: 6 blue, 6 green; 5 blue; 4 blue, 3 green; 10 green, 1 red, 4 blue
Game 61: 2 green, 3 blue; 3 red, 3 blue; 2 red, 4 green
Game 62: 3 red, 6 green, 2 blue; 6 red, 5 blue, 2 green; 13 green, 9 blue
Game 63: 9 blue, 1 green; 14 blue, 12 red; 1 green, 6 red, 14 blue; 1 green, 2 red, 14 blue; 7 red, 18 blue, 1 green; 14 red, 2 blue
Game 64: 4 blue, 16 red, 4 green; 9 red, 4 green, 3 blue; 2 red, 2 blue, 6 green; 2 blue, 2 green, 12 red
Game 65: 2 red, 2 blue; 4 red, 9 green, 5 blue; 1 blue; 3 green, 1 red; 1 green, 2 blue, 8 red; 1 blue, 8 green, 3 red
Game 66: 1 red, 3 green, 4 blue; 3 green, 5 red, 14 blue; 1 blue, 3 red, 2 green; 4 blue, 1 green, 2 red; 8 red, 2 green, 13 blue
Game 67: 1 green, 1 red, 4 blue; 2 blue, 2 green, 1 red; 3 blue, 2 green, 1 red
Game 68: 1 red, 2 blue; 5 green, 2 red, 2 blue; 7 red, 7 green, 2 blue
Game 69: 15 red, 12 green, 1 blue; 3 green, 3 blue, 6 red; 9 blue, 16 green; 11 blue, 18 green, 7 red
Game 70: 4 blue, 1 green, 6 red; 11 red, 3 green, 4 blue; 3 blue, 1 red, 1 green; 1 green, 11 red; 3 red, 4 blue, 1 green; 3 red, 4 blue, 2 green
Game 71: 11 blue, 1 red; 10 blue, 10 green; 4 green, 11 blue; 5 green, 1 red, 6 blue; 6 green, 2 blue; 4 blue, 3 green
Game 72: 1 red, 7 blue; 3 red, 3 green, 7 blue; 3 red, 3 green, 10 blue; 5 green, 7 blue
Game 73: 2 blue, 1 red; 6 blue, 8 red, 18 green; 4 blue, 18 green, 1 red; 1 red, 5 blue, 2 green; 18 green, 8 red, 8 blue
Game 74: 19 green, 9 blue, 14 red; 11 green, 8 blue, 14 red; 2 green, 17 blue, 14 red; 12 green, 12 blue, 7 red; 6 red, 5 blue, 10 green; 4 blue, 19 green, 15 red
Game 75: 12 red, 3 green, 4 blue; 7 red, 6 green, 2 blue; 7 green, 3 red, 7 blue; 16 red, 3 green; 10 blue, 6 red, 3 green
Game 76: 1 red, 11 blue, 4 green; 11 green, 3 blue, 3 red; 2 red, 14 green, 7 blue; 3 red, 6 blue, 9 green; 9 blue, 14 green; 1 red, 9 green, 3 blue
Game 77: 10 blue, 2 red, 5 green; 5 green, 3 red, 12 blue; 3 green, 8 red, 4 blue; 5 blue, 12 red, 7 green; 18 blue, 7 red; 8 green, 8 blue, 13 red
Game 78: 9 red, 11 green, 4 blue; 4 blue, 14 green; 2 red, 11 green, 6 blue; 2 blue, 5 red, 13 green; 6 red, 2 green
Game 79: 2 green, 3 blue, 1 red; 6 green, 2 blue; 1 blue, 4 green
Game 80: 2 green, 4 red; 1 green, 6 blue, 2 red; 8 blue, 3 red; 6 blue, 1 green; 6 red, 2 blue; 2 green, 2 blue
Game 81: 5 red, 3 blue, 6 green; 3 blue, 17 green, 5 red; 3 red, 2 blue, 14 green; 1 blue, 2 red, 8 green
Game 82: 16 green, 10 blue; 6 blue, 4 green, 2 red; 3 blue, 1 red, 16 green; 1 red, 7 green, 11 blue
Game 83: 8 green, 7 blue; 1 blue, 6 green; 6 blue, 1 red, 3 green; 8 green, 1 red; 1 green, 5 blue
Game 84: 9 blue, 6 red; 5 red, 7 green, 3 blue; 4 blue, 13 green, 2 red; 10 red, 11 green, 6 blue
Game 85: 4 red, 10 blue; 8 green, 1 blue, 1 red; 9 blue, 6 green; 1 red, 4 green; 3 green, 8 blue
Game 86: 7 blue, 9 green; 7 blue, 1 red, 4 green; 4 green, 13 blue
Game 87: 12 red, 9 green, 2 blue; 8 green, 7 red; 11 red, 11 green; 4 blue, 8 green
Game 88: 8 blue, 7 green, 7 red; 5 blue, 9 green; 3 red, 7 green, 6 blue; 1 green, 7 blue, 7 red
Game 89: 7 green, 9 red, 7 blue; 1 green, 18 red; 3 red, 2 blue, 2 green; 15 red, 4 green, 6 blue
Game 90: 4 blue, 5 red, 4 green; 4 green, 6 blue, 3 red; 4 green, 6 blue, 2 red; 8 blue, 4 red, 4 green; 3 blue, 2 red
Game 91: 7 green, 5 red, 2 blue; 12 green, 2 blue, 6 red; 6 green, 1 blue; 1 green, 1 blue; 13 green, 1 red, 3 blue; 5 red, 1 blue, 4 green
Game 92: 2 green; 1 red, 2 blue, 2 green; 2 red, 2 green, 2 blue; 2 blue, 7 red
Game 93: 8 red, 5 blue; 10 red, 4 blue; 3 red, 2 blue; 7 blue, 10 red, 1 green; 6 blue; 10 blue
Game 94: 2 blue, 4 green; 8 green, 9 blue; 2 green, 3 blue; 3 blue, 5 green; 9 blue, 1 red, 3 green; 6 blue, 6 green, 1 red
Game 95: 3 blue, 13 red, 10 green; 4 green, 17 blue, 12 red; 12 red, 10 green, 16 blue; 15 red, 14 green, 2 blue; 12 red, 1 blue, 15 green; 10 green, 13 blue, 19 red
Game 96: 5 green, 9 blue, 16 red; 17 red, 11 green, 9 blue; 10 blue, 13 green, 9 red; 10 blue, 7 red, 13 green; 3 red, 4 blue, 5 green
Game 97: 6 blue, 4 green, 6 red; 4 red, 13 green, 2 blue; 15 green, 2 red; 2 green, 2 red
Game 98: 10 blue, 13 red; 10 blue, 16 red, 4 green; 6 blue, 4 green, 14 red; 4 green, 1 blue, 11 red; 4 red, 4 green
Game 99: 1 red, 4 blue; 5 red, 8 blue; 3 blue, 1 green; 2 red, 6 blue; 8 blue, 2 green, 3 red
Game 100: 5 green, 1 red; 4 blue, 8 red, 4 green; 1 blue, 3 red, 15 green; 1 blue, 15 green, 1 red; 2 red, 13 green

File diff suppressed because it is too large Load diff