From 9b5a01aad0c122e068e0340d447f9814d9d8c1bb Mon Sep 17 00:00:00 2001 From: dnutiu Date: Sun, 3 Dec 2023 19:18:35 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=84Advent=20of=20Code=20-=20Day=203=20?= =?UTF-8?q?=E2=9C=A8=20part=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/AdventOfCode/Puzzle.kt | 11 ++++++----- src/main/kotlin/AdventOfCode2023/day1/Trebuchet.kt | 10 +++------- .../kotlin/AdventOfCode2023/day2/CubeConundrum.kt | 6 ++---- src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt | 6 +----- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/AdventOfCode/Puzzle.kt b/src/main/kotlin/AdventOfCode/Puzzle.kt index 93f4a5a..8b481d5 100644 --- a/src/main/kotlin/AdventOfCode/Puzzle.kt +++ b/src/main/kotlin/AdventOfCode/Puzzle.kt @@ -1,16 +1,17 @@ package AdventOfCode -interface Puzzle { +abstract class Puzzle(private val year: String, private val day: String) { + protected val inputData = this.readInputFromFile() // Part one of the AoC puzzle. - fun partOne() + abstract fun partOne() // Part two of the AoC puzzle. - fun partTwo() + abstract fun partTwo() - fun readInputFromFile(dayNumber: String): List { + fun readInputFromFile(): List { // read calibrations from resources file - this.javaClass.getResourceAsStream("/aoc2023/input_day$dayNumber.txt")?.bufferedReader()?.useLines { lines -> + this.javaClass.getResourceAsStream("/aoc$year/input_day$day.txt")?.bufferedReader()?.useLines { lines -> return lines.toList() } throw Exception("Could not read calibrations from file.") diff --git a/src/main/kotlin/AdventOfCode2023/day1/Trebuchet.kt b/src/main/kotlin/AdventOfCode2023/day1/Trebuchet.kt index 095792c..91d8a53 100644 --- a/src/main/kotlin/AdventOfCode2023/day1/Trebuchet.kt +++ b/src/main/kotlin/AdventOfCode2023/day1/Trebuchet.kt @@ -2,12 +2,10 @@ package AdventOfCode2023.day1 import AdventOfCode.Puzzle -class Trebuchet : Puzzle { +class Trebuchet : Puzzle("2023", "1") { override fun partOne() { - // read calibrations from file - val calibrations = readInputFromFile("1") // print calibrations - val calibrationData = calibrations.map { + val calibrationData = inputData.map { // replace non-digits with empty string it.replace(Regex("[^0-9]"), "") }.map { @@ -21,10 +19,8 @@ class Trebuchet : Puzzle { } override fun partTwo() { - // read calibrations from file - val calibrations = this.readInputFromFile("1") // print calibrations - val calibrationData = calibrations.map { it -> + val calibrationData = inputData.map { it -> val digitsData = mapOf( "one" to "1", "two" to "2", diff --git a/src/main/kotlin/AdventOfCode2023/day2/CubeConundrum.kt b/src/main/kotlin/AdventOfCode2023/day2/CubeConundrum.kt index 683b97a..992e4f7 100644 --- a/src/main/kotlin/AdventOfCode2023/day2/CubeConundrum.kt +++ b/src/main/kotlin/AdventOfCode2023/day2/CubeConundrum.kt @@ -3,12 +3,10 @@ package AdventOfCode2023.day2 import AdventOfCode.Puzzle import java.util.Scanner -class CubeConundrum : Puzzle { +class CubeConundrum : Puzzle("2023", "2") { private fun getGameList(): List { - val input = this.readInputFromFile("2") - - return input.map { + return inputData.map { val game = Game(0, null) val line = it.split(":", limit = 2) // Get game id diff --git a/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt b/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt index 91adc7b..a92af1e 100644 --- a/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt +++ b/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt @@ -5,8 +5,7 @@ import AdventOfCode.Puzzle data class EnginePart(var number: Int, var row: Int, var startIndex: Int, var endIndex: Int) data class Gear(var row: Int, val column: Int, var engineParts: MutableSet) -class GearRatios : Puzzle { - +class GearRatios : Puzzle("2023", "3") { private fun scanForEnginePats(data: List): List { val foundEngineParts: MutableList = mutableListOf() @@ -82,8 +81,6 @@ class GearRatios : Puzzle { } override fun partOne() { - val inputData = this.readInputFromFile("3") - val foundEngineParts: List = scanForEnginePats(inputData) foundEngineParts.forEach { it -> @@ -97,7 +94,6 @@ class GearRatios : Puzzle { } override fun partTwo() { - val inputData = this.readInputFromFile("3") val foundEngineParts: List = scanForEnginePats(inputData) val gears: MutableList = mutableListOf()