Move input files to resources
This commit is contained in:
parent
a0531d2e68
commit
d713d2b776
14 changed files with 84 additions and 82 deletions
5
advent-of-code-2023/src/main/kotlin/ResourceReader.kt
Normal file
5
advent-of-code-2023/src/main/kotlin/ResourceReader.kt
Normal file
|
@ -0,0 +1,5 @@
|
|||
class ResourceReader {
|
||||
fun readFile(filename: String): List<String> {
|
||||
return this::class.java.getResourceAsStream(filename).bufferedReader().readLines();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
object Solution01 {
|
||||
@JvmStatic
|
||||
class Solution01 {
|
||||
fun getCalibrationValues(lines: List<String>, findWords: Boolean): Int {
|
||||
var sum = 0
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import java.io.File
|
||||
import kotlin.math.max
|
||||
|
||||
class CubeCount(var red: Int, var green: Int, var blue: Int) {
|
||||
|
@ -7,12 +6,55 @@ class CubeCount(var red: Int, var green: Int, var blue: Int) {
|
|||
}
|
||||
}
|
||||
|
||||
object Solution02 {
|
||||
@JvmStatic
|
||||
fun readGames(filepath: String): HashMap<Int, ArrayList<CubeCount>> {
|
||||
val games = hashMapOf<Int, ArrayList<CubeCount>>()
|
||||
class Solution02 {
|
||||
// Part 1
|
||||
fun getSumOfCorrectGameIDs(text: List<String>, cubes: CubeCount): Int {
|
||||
val games = readGames(text)
|
||||
var sum = 0
|
||||
|
||||
File(filepath).forEachLine {
|
||||
for (game in games.entries) {
|
||||
var valid = true
|
||||
|
||||
for (count in game.value) {
|
||||
if (!count.lessOrEqualThan(cubes)) {
|
||||
valid = false
|
||||
}
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
sum += game.key
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
// Part 2
|
||||
fun getSumOfPowers(text: List<String>): Int {
|
||||
val games = readGames(text)
|
||||
var sum = 0
|
||||
|
||||
for (game in games.values) {
|
||||
var maxRed = 0
|
||||
var maxGreen = 0
|
||||
var maxBlue = 0
|
||||
|
||||
for (gameSet in game) {
|
||||
maxRed = max(maxRed, gameSet.red)
|
||||
maxGreen = max(maxGreen, gameSet.green)
|
||||
maxBlue = max(maxBlue, gameSet.blue)
|
||||
}
|
||||
|
||||
sum += maxRed * maxGreen * maxBlue
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
private fun readGames(text: List<String>): HashMap<Int, MutableList<CubeCount>> {
|
||||
val games = hashMapOf<Int, MutableList<CubeCount>>()
|
||||
|
||||
text.forEach {
|
||||
// `it` example: "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green"
|
||||
val (game, sets) = it.split(": ")
|
||||
val gameID = game.split(" ")[1].toInt()
|
||||
|
@ -36,46 +78,4 @@ object Solution02 {
|
|||
|
||||
return games
|
||||
}
|
||||
|
||||
// Part 1
|
||||
fun getSumOfCorrectGameIDs(games: HashMap<Int, ArrayList<CubeCount>>, cubes: CubeCount): Int {
|
||||
var sum = 0
|
||||
|
||||
for (game in games.entries) {
|
||||
var valid = true
|
||||
|
||||
for (count in game.value) {
|
||||
if (!count.lessOrEqualThan(cubes)) {
|
||||
valid = false
|
||||
}
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
sum += game.key
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
// Part 2
|
||||
fun getSumOfPowers(games: HashMap<Int, ArrayList<CubeCount>>): Int {
|
||||
var sum = 0
|
||||
|
||||
for (game in games.values) {
|
||||
var maxRed = 0
|
||||
var maxGreen = 0
|
||||
var maxBlue = 0
|
||||
|
||||
for (gameSet in game) {
|
||||
maxRed = max(maxRed, gameSet.red)
|
||||
maxGreen = max(maxGreen, gameSet.green)
|
||||
maxBlue = max(maxBlue, gameSet.blue)
|
||||
}
|
||||
|
||||
sum += maxRed * maxGreen * maxBlue
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
}
|
|
@ -136,8 +136,7 @@ class Solution03(private val lines: List<String>) {
|
|||
|
||||
private fun isSymbol(i: Int, j: Int): Boolean {
|
||||
if (digits.contains(lines[i][j])) return false
|
||||
if (lines[i][j] == '.') return false
|
||||
return true
|
||||
return lines[i][j] != '.'
|
||||
}
|
||||
|
||||
private fun isAsterisk(i: Int, j: Int): Boolean {
|
||||
|
|
|
@ -1,33 +1,32 @@
|
|||
import java.io.File
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class Solution01Test {
|
||||
@Test
|
||||
fun testGetCalibrationValues() {
|
||||
val file = File("inputs/day-01/test-01.txt")
|
||||
val res = Solution01.getCalibrationValues(file.readLines(), false)
|
||||
val text = ResourceReader().readFile("day-01/test-01.txt")
|
||||
val res = Solution01().getCalibrationValues(text, false)
|
||||
assertEquals(142, res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testReplaceStringsAndGetCalibrationValues() {
|
||||
val file = File("inputs/day-01/test-02.txt")
|
||||
val res = Solution01.getCalibrationValues(file.readLines(), true)
|
||||
val text = ResourceReader().readFile("day-01/test-02.txt")
|
||||
val res = Solution01().getCalibrationValues(text, true)
|
||||
assertEquals(281, res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun solvePart1() {
|
||||
val file = File("inputs/day-01/input.txt")
|
||||
val res = Solution01.getCalibrationValues(file.readLines(), false)
|
||||
val text = ResourceReader().readFile("day-01/input.txt")
|
||||
val res = Solution01().getCalibrationValues(text, false)
|
||||
println(res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun solvePart2() {
|
||||
val file = File("inputs/day-01/input.txt")
|
||||
val res = Solution01.getCalibrationValues(file.readLines(), true)
|
||||
val text = ResourceReader().readFile("day-01/input.txt")
|
||||
val res = Solution01().getCalibrationValues(text, true)
|
||||
println(res)
|
||||
}
|
||||
}
|
|
@ -4,29 +4,29 @@ import kotlin.test.assertEquals
|
|||
class Solution02Test {
|
||||
@Test
|
||||
fun testGetSumOfCorrectGameIDs() {
|
||||
val games = Solution02.readGames("inputs/day-02/test.txt")
|
||||
val res = Solution02.getSumOfCorrectGameIDs(games, CubeCount(12, 13, 14))
|
||||
val text = ResourceReader().readFile("day-02/test.txt")
|
||||
val res = Solution02().getSumOfCorrectGameIDs(text, CubeCount(12, 13, 14))
|
||||
assertEquals(8, res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetPowerSum() {
|
||||
val games = Solution02.readGames("inputs/day-02/test.txt")
|
||||
val res = Solution02.getSumOfPowers(games)
|
||||
val text = ResourceReader().readFile("day-02/test.txt")
|
||||
val res = Solution02().getSumOfPowers(text)
|
||||
assertEquals(2286, res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun solvePart1() {
|
||||
val games = Solution02.readGames("inputs/day-02/input.txt")
|
||||
val res = Solution02.getSumOfCorrectGameIDs(games, CubeCount(12, 13, 14))
|
||||
val text = ResourceReader().readFile("day-02/input.txt")
|
||||
val res = Solution02().getSumOfCorrectGameIDs(text, CubeCount(12, 13, 14))
|
||||
println(res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun solvePart2() {
|
||||
val games = Solution02.readGames("inputs/day-02/input.txt")
|
||||
val res = Solution02.getSumOfPowers(games)
|
||||
val text = ResourceReader().readFile("day-02/input.txt")
|
||||
val res = Solution02().getSumOfPowers(text)
|
||||
println(res)
|
||||
}
|
||||
}
|
|
@ -5,32 +5,32 @@ import kotlin.test.assertEquals
|
|||
class Solution03Test {
|
||||
@Test
|
||||
fun testGetSumOfParts() {
|
||||
val file = File("inputs/day-03/test.txt")
|
||||
val instance = Solution03(file.readLines())
|
||||
val text = ResourceReader().readFile("day-03/test.txt")
|
||||
val instance = Solution03(text)
|
||||
val res = instance.getSumOfParts()
|
||||
assertEquals(4361, res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun solvePart1() {
|
||||
val file = File("inputs/day-03/input.txt")
|
||||
val instance = Solution03(file.readLines())
|
||||
val text = ResourceReader().readFile("day-03/input.txt")
|
||||
val instance = Solution03(text)
|
||||
val res = instance.getSumOfParts()
|
||||
println(res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetGearRatio() {
|
||||
val file = File("inputs/day-03/test.txt")
|
||||
val instance = Solution03(file.readLines())
|
||||
val text = ResourceReader().readFile("day-03/test.txt")
|
||||
val instance = Solution03(text)
|
||||
val res = instance.getGearRatioSum()
|
||||
assertEquals(467835, res)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun solvePart2() {
|
||||
val file = File("inputs/day-03/input.txt")
|
||||
val instance = Solution03(file.readLines())
|
||||
val text = ResourceReader().readFile("day-03/input.txt")
|
||||
val instance = Solution03(text)
|
||||
val res = instance.getGearRatioSum()
|
||||
println(res)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue