Move input files to resources

This commit is contained in:
Ivan R. 2024-01-09 11:08:24 +05:00
parent a0531d2e68
commit d713d2b776
Signed by: lumin
GPG key ID: 927D3132247BDE4E
14 changed files with 84 additions and 82 deletions

View file

@ -0,0 +1,5 @@
class ResourceReader {
fun readFile(filename: String): List<String> {
return this::class.java.getResourceAsStream(filename).bufferedReader().readLines();
}
}

View file

@ -1,5 +1,4 @@
object Solution01 { class Solution01 {
@JvmStatic
fun getCalibrationValues(lines: List<String>, findWords: Boolean): Int { fun getCalibrationValues(lines: List<String>, findWords: Boolean): Int {
var sum = 0 var sum = 0

View file

@ -1,4 +1,3 @@
import java.io.File
import kotlin.math.max import kotlin.math.max
class CubeCount(var red: Int, var green: Int, var blue: Int) { 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 { class Solution02 {
@JvmStatic // Part 1
fun readGames(filepath: String): HashMap<Int, ArrayList<CubeCount>> { fun getSumOfCorrectGameIDs(text: List<String>, cubes: CubeCount): Int {
val games = hashMapOf<Int, ArrayList<CubeCount>>() 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" // `it` example: "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green"
val (game, sets) = it.split(": ") val (game, sets) = it.split(": ")
val gameID = game.split(" ")[1].toInt() val gameID = game.split(" ")[1].toInt()
@ -36,46 +78,4 @@ object Solution02 {
return games 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
}
} }

View file

@ -136,8 +136,7 @@ class Solution03(private val lines: List<String>) {
private fun isSymbol(i: Int, j: Int): Boolean { private fun isSymbol(i: Int, j: Int): Boolean {
if (digits.contains(lines[i][j])) return false if (digits.contains(lines[i][j])) return false
if (lines[i][j] == '.') return false return lines[i][j] != '.'
return true
} }
private fun isAsterisk(i: Int, j: Int): Boolean { private fun isAsterisk(i: Int, j: Int): Boolean {

View file

@ -1,33 +1,32 @@
import java.io.File
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
class Solution01Test { class Solution01Test {
@Test @Test
fun testGetCalibrationValues() { fun testGetCalibrationValues() {
val file = File("inputs/day-01/test-01.txt") val text = ResourceReader().readFile("day-01/test-01.txt")
val res = Solution01.getCalibrationValues(file.readLines(), false) val res = Solution01().getCalibrationValues(text, false)
assertEquals(142, res) assertEquals(142, res)
} }
@Test @Test
fun testReplaceStringsAndGetCalibrationValues() { fun testReplaceStringsAndGetCalibrationValues() {
val file = File("inputs/day-01/test-02.txt") val text = ResourceReader().readFile("day-01/test-02.txt")
val res = Solution01.getCalibrationValues(file.readLines(), true) val res = Solution01().getCalibrationValues(text, true)
assertEquals(281, res) assertEquals(281, res)
} }
@Test @Test
fun solvePart1() { fun solvePart1() {
val file = File("inputs/day-01/input.txt") val text = ResourceReader().readFile("day-01/input.txt")
val res = Solution01.getCalibrationValues(file.readLines(), false) val res = Solution01().getCalibrationValues(text, false)
println(res) println(res)
} }
@Test @Test
fun solvePart2() { fun solvePart2() {
val file = File("inputs/day-01/input.txt") val text = ResourceReader().readFile("day-01/input.txt")
val res = Solution01.getCalibrationValues(file.readLines(), true) val res = Solution01().getCalibrationValues(text, true)
println(res) println(res)
} }
} }

View file

@ -4,29 +4,29 @@ import kotlin.test.assertEquals
class Solution02Test { class Solution02Test {
@Test @Test
fun testGetSumOfCorrectGameIDs() { fun testGetSumOfCorrectGameIDs() {
val games = Solution02.readGames("inputs/day-02/test.txt") val text = ResourceReader().readFile("day-02/test.txt")
val res = Solution02.getSumOfCorrectGameIDs(games, CubeCount(12, 13, 14)) val res = Solution02().getSumOfCorrectGameIDs(text, CubeCount(12, 13, 14))
assertEquals(8, res) assertEquals(8, res)
} }
@Test @Test
fun testGetPowerSum() { fun testGetPowerSum() {
val games = Solution02.readGames("inputs/day-02/test.txt") val text = ResourceReader().readFile("day-02/test.txt")
val res = Solution02.getSumOfPowers(games) val res = Solution02().getSumOfPowers(text)
assertEquals(2286, res) assertEquals(2286, res)
} }
@Test @Test
fun solvePart1() { fun solvePart1() {
val games = Solution02.readGames("inputs/day-02/input.txt") val text = ResourceReader().readFile("day-02/input.txt")
val res = Solution02.getSumOfCorrectGameIDs(games, CubeCount(12, 13, 14)) val res = Solution02().getSumOfCorrectGameIDs(text, CubeCount(12, 13, 14))
println(res) println(res)
} }
@Test @Test
fun solvePart2() { fun solvePart2() {
val games = Solution02.readGames("inputs/day-02/input.txt") val text = ResourceReader().readFile("day-02/input.txt")
val res = Solution02.getSumOfPowers(games) val res = Solution02().getSumOfPowers(text)
println(res) println(res)
} }
} }

View file

@ -5,32 +5,32 @@ import kotlin.test.assertEquals
class Solution03Test { class Solution03Test {
@Test @Test
fun testGetSumOfParts() { fun testGetSumOfParts() {
val file = File("inputs/day-03/test.txt") val text = ResourceReader().readFile("day-03/test.txt")
val instance = Solution03(file.readLines()) val instance = Solution03(text)
val res = instance.getSumOfParts() val res = instance.getSumOfParts()
assertEquals(4361, res) assertEquals(4361, res)
} }
@Test @Test
fun solvePart1() { fun solvePart1() {
val file = File("inputs/day-03/input.txt") val text = ResourceReader().readFile("day-03/input.txt")
val instance = Solution03(file.readLines()) val instance = Solution03(text)
val res = instance.getSumOfParts() val res = instance.getSumOfParts()
println(res) println(res)
} }
@Test @Test
fun testGetGearRatio() { fun testGetGearRatio() {
val file = File("inputs/day-03/test.txt") val text = ResourceReader().readFile("day-03/test.txt")
val instance = Solution03(file.readLines()) val instance = Solution03(text)
val res = instance.getGearRatioSum() val res = instance.getGearRatioSum()
assertEquals(467835, res) assertEquals(467835, res)
} }
@Test @Test
fun solvePart2() { fun solvePart2() {
val file = File("inputs/day-03/input.txt") val text = ResourceReader().readFile("day-03/input.txt")
val instance = Solution03(file.readLines()) val instance = Solution03(text)
val res = instance.getGearRatioSum() val res = instance.getGearRatioSum()
println(res) println(res)
} }