From d713d2b776d107dd2e1dfef4fe92df5faac1a5ab Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 9 Jan 2024 11:08:24 +0500 Subject: [PATCH] Move input files to resources --- .../src/main/kotlin/ResourceReader.kt | 5 + .../src/main/kotlin/Solution01.kt | 5 +- .../src/main/kotlin/Solution02.kt | 98 +++++++++---------- .../src/main/kotlin/Solution03.kt | 5 +- .../main/resources}/day-01/input.txt | 0 .../main/resources}/day-01/test-01.txt | 0 .../main/resources}/day-01/test-02.txt | 0 .../main/resources}/day-02/input.txt | 0 .../main/resources}/day-02/test.txt | 0 .../main/resources}/day-03/input.txt | 0 .../main/resources}/day-03/test.txt | 0 .../src/test/kotlin/Solution01Test.kt | 19 ++-- .../src/test/kotlin/Solution02Test.kt | 18 ++-- .../src/test/kotlin/Solution03Test.kt | 16 +-- 14 files changed, 84 insertions(+), 82 deletions(-) create mode 100644 advent-of-code-2023/src/main/kotlin/ResourceReader.kt rename advent-of-code-2023/{inputs => src/main/resources}/day-01/input.txt (100%) rename advent-of-code-2023/{inputs => src/main/resources}/day-01/test-01.txt (100%) rename advent-of-code-2023/{inputs => src/main/resources}/day-01/test-02.txt (100%) rename advent-of-code-2023/{inputs => src/main/resources}/day-02/input.txt (100%) rename advent-of-code-2023/{inputs => src/main/resources}/day-02/test.txt (100%) rename advent-of-code-2023/{inputs => src/main/resources}/day-03/input.txt (100%) rename advent-of-code-2023/{inputs => src/main/resources}/day-03/test.txt (100%) diff --git a/advent-of-code-2023/src/main/kotlin/ResourceReader.kt b/advent-of-code-2023/src/main/kotlin/ResourceReader.kt new file mode 100644 index 0000000..786b8ad --- /dev/null +++ b/advent-of-code-2023/src/main/kotlin/ResourceReader.kt @@ -0,0 +1,5 @@ +class ResourceReader { + fun readFile(filename: String): List { + return this::class.java.getResourceAsStream(filename).bufferedReader().readLines(); + } +} diff --git a/advent-of-code-2023/src/main/kotlin/Solution01.kt b/advent-of-code-2023/src/main/kotlin/Solution01.kt index 394c8b6..aafa966 100644 --- a/advent-of-code-2023/src/main/kotlin/Solution01.kt +++ b/advent-of-code-2023/src/main/kotlin/Solution01.kt @@ -1,5 +1,4 @@ -object Solution01 { - @JvmStatic +class Solution01 { fun getCalibrationValues(lines: List, findWords: Boolean): Int { var sum = 0 @@ -43,4 +42,4 @@ object Solution01 { } return s } -} \ No newline at end of file +} diff --git a/advent-of-code-2023/src/main/kotlin/Solution02.kt b/advent-of-code-2023/src/main/kotlin/Solution02.kt index cd839f4..6fc5bac 100644 --- a/advent-of-code-2023/src/main/kotlin/Solution02.kt +++ b/advent-of-code-2023/src/main/kotlin/Solution02.kt @@ -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> { - val games = hashMapOf>() +class Solution02 { + // Part 1 + fun getSumOfCorrectGameIDs(text: List, 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): 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): HashMap> { + val games = hashMapOf>() + + 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>, 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 { - 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 - } -} \ No newline at end of file +} diff --git a/advent-of-code-2023/src/main/kotlin/Solution03.kt b/advent-of-code-2023/src/main/kotlin/Solution03.kt index 3c9a88a..c1c850d 100644 --- a/advent-of-code-2023/src/main/kotlin/Solution03.kt +++ b/advent-of-code-2023/src/main/kotlin/Solution03.kt @@ -136,8 +136,7 @@ class Solution03(private val lines: List) { 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 { @@ -145,4 +144,4 @@ class Solution03(private val lines: List) { } } -class Coordinates(var i: Int, var j: Int) \ No newline at end of file +class Coordinates(var i: Int, var j: Int) diff --git a/advent-of-code-2023/inputs/day-01/input.txt b/advent-of-code-2023/src/main/resources/day-01/input.txt similarity index 100% rename from advent-of-code-2023/inputs/day-01/input.txt rename to advent-of-code-2023/src/main/resources/day-01/input.txt diff --git a/advent-of-code-2023/inputs/day-01/test-01.txt b/advent-of-code-2023/src/main/resources/day-01/test-01.txt similarity index 100% rename from advent-of-code-2023/inputs/day-01/test-01.txt rename to advent-of-code-2023/src/main/resources/day-01/test-01.txt diff --git a/advent-of-code-2023/inputs/day-01/test-02.txt b/advent-of-code-2023/src/main/resources/day-01/test-02.txt similarity index 100% rename from advent-of-code-2023/inputs/day-01/test-02.txt rename to advent-of-code-2023/src/main/resources/day-01/test-02.txt diff --git a/advent-of-code-2023/inputs/day-02/input.txt b/advent-of-code-2023/src/main/resources/day-02/input.txt similarity index 100% rename from advent-of-code-2023/inputs/day-02/input.txt rename to advent-of-code-2023/src/main/resources/day-02/input.txt diff --git a/advent-of-code-2023/inputs/day-02/test.txt b/advent-of-code-2023/src/main/resources/day-02/test.txt similarity index 100% rename from advent-of-code-2023/inputs/day-02/test.txt rename to advent-of-code-2023/src/main/resources/day-02/test.txt diff --git a/advent-of-code-2023/inputs/day-03/input.txt b/advent-of-code-2023/src/main/resources/day-03/input.txt similarity index 100% rename from advent-of-code-2023/inputs/day-03/input.txt rename to advent-of-code-2023/src/main/resources/day-03/input.txt diff --git a/advent-of-code-2023/inputs/day-03/test.txt b/advent-of-code-2023/src/main/resources/day-03/test.txt similarity index 100% rename from advent-of-code-2023/inputs/day-03/test.txt rename to advent-of-code-2023/src/main/resources/day-03/test.txt diff --git a/advent-of-code-2023/src/test/kotlin/Solution01Test.kt b/advent-of-code-2023/src/test/kotlin/Solution01Test.kt index 7df0ec9..00d9fe3 100644 --- a/advent-of-code-2023/src/test/kotlin/Solution01Test.kt +++ b/advent-of-code-2023/src/test/kotlin/Solution01Test.kt @@ -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) } -} \ No newline at end of file +} diff --git a/advent-of-code-2023/src/test/kotlin/Solution02Test.kt b/advent-of-code-2023/src/test/kotlin/Solution02Test.kt index 6a27c51..ef6c098 100644 --- a/advent-of-code-2023/src/test/kotlin/Solution02Test.kt +++ b/advent-of-code-2023/src/test/kotlin/Solution02Test.kt @@ -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) } -} \ No newline at end of file +} diff --git a/advent-of-code-2023/src/test/kotlin/Solution03Test.kt b/advent-of-code-2023/src/test/kotlin/Solution03Test.kt index 69aacfe..331d82e 100644 --- a/advent-of-code-2023/src/test/kotlin/Solution03Test.kt +++ b/advent-of-code-2023/src/test/kotlin/Solution03Test.kt @@ -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) }