🎄Advent of Code - Day 3 ✨ part 2
This commit is contained in:
parent
7a06f3bf01
commit
9b5a01aad0
4 changed files with 12 additions and 21 deletions
|
@ -1,16 +1,17 @@
|
||||||
package AdventOfCode
|
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.
|
// Part one of the AoC puzzle.
|
||||||
fun partOne()
|
abstract fun partOne()
|
||||||
|
|
||||||
// Part two of the AoC puzzle.
|
// Part two of the AoC puzzle.
|
||||||
fun partTwo()
|
abstract fun partTwo()
|
||||||
|
|
||||||
fun readInputFromFile(dayNumber: String): List<String> {
|
fun readInputFromFile(): List<String> {
|
||||||
// read calibrations from resources file
|
// 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()
|
return lines.toList()
|
||||||
}
|
}
|
||||||
throw Exception("Could not read calibrations from file.")
|
throw Exception("Could not read calibrations from file.")
|
||||||
|
|
|
@ -2,12 +2,10 @@ package AdventOfCode2023.day1
|
||||||
|
|
||||||
import AdventOfCode.Puzzle
|
import AdventOfCode.Puzzle
|
||||||
|
|
||||||
class Trebuchet : Puzzle {
|
class Trebuchet : Puzzle("2023", "1") {
|
||||||
override fun partOne() {
|
override fun partOne() {
|
||||||
// read calibrations from file
|
|
||||||
val calibrations = readInputFromFile("1")
|
|
||||||
// print calibrations
|
// print calibrations
|
||||||
val calibrationData = calibrations.map {
|
val calibrationData = inputData.map {
|
||||||
// replace non-digits with empty string
|
// replace non-digits with empty string
|
||||||
it.replace(Regex("[^0-9]"), "")
|
it.replace(Regex("[^0-9]"), "")
|
||||||
}.map {
|
}.map {
|
||||||
|
@ -21,10 +19,8 @@ class Trebuchet : Puzzle {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun partTwo() {
|
override fun partTwo() {
|
||||||
// read calibrations from file
|
|
||||||
val calibrations = this.readInputFromFile("1")
|
|
||||||
// print calibrations
|
// print calibrations
|
||||||
val calibrationData = calibrations.map { it ->
|
val calibrationData = inputData.map { it ->
|
||||||
val digitsData = mapOf(
|
val digitsData = mapOf(
|
||||||
"one" to "1",
|
"one" to "1",
|
||||||
"two" to "2",
|
"two" to "2",
|
||||||
|
|
|
@ -3,12 +3,10 @@ package AdventOfCode2023.day2
|
||||||
import AdventOfCode.Puzzle
|
import AdventOfCode.Puzzle
|
||||||
import java.util.Scanner
|
import java.util.Scanner
|
||||||
|
|
||||||
class CubeConundrum : Puzzle {
|
class CubeConundrum : Puzzle("2023", "2") {
|
||||||
|
|
||||||
private fun getGameList(): List<Game> {
|
private fun getGameList(): List<Game> {
|
||||||
val input = this.readInputFromFile("2")
|
return inputData.map {
|
||||||
|
|
||||||
return input.map {
|
|
||||||
val game = Game(0, null)
|
val game = Game(0, null)
|
||||||
val line = it.split(":", limit = 2)
|
val line = it.split(":", limit = 2)
|
||||||
// Get game id
|
// Get game id
|
||||||
|
|
|
@ -5,8 +5,7 @@ import AdventOfCode.Puzzle
|
||||||
data class EnginePart(var number: Int, var row: Int, var startIndex: Int, var endIndex: Int)
|
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<EnginePart>)
|
data class Gear(var row: Int, val column: Int, var engineParts: MutableSet<EnginePart>)
|
||||||
|
|
||||||
class GearRatios : Puzzle {
|
class GearRatios : Puzzle("2023", "3") {
|
||||||
|
|
||||||
private fun scanForEnginePats(data: List<String>): List<EnginePart> {
|
private fun scanForEnginePats(data: List<String>): List<EnginePart> {
|
||||||
val foundEngineParts: MutableList<EnginePart> = mutableListOf()
|
val foundEngineParts: MutableList<EnginePart> = mutableListOf()
|
||||||
|
|
||||||
|
@ -82,8 +81,6 @@ class GearRatios : Puzzle {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun partOne() {
|
override fun partOne() {
|
||||||
val inputData = this.readInputFromFile("3")
|
|
||||||
|
|
||||||
val foundEngineParts: List<EnginePart> = scanForEnginePats(inputData)
|
val foundEngineParts: List<EnginePart> = scanForEnginePats(inputData)
|
||||||
|
|
||||||
foundEngineParts.forEach { it ->
|
foundEngineParts.forEach { it ->
|
||||||
|
@ -97,7 +94,6 @@ class GearRatios : Puzzle {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun partTwo() {
|
override fun partTwo() {
|
||||||
val inputData = this.readInputFromFile("3")
|
|
||||||
val foundEngineParts: List<EnginePart> = scanForEnginePats(inputData)
|
val foundEngineParts: List<EnginePart> = scanForEnginePats(inputData)
|
||||||
val gears: MutableList<Gear> = mutableListOf()
|
val gears: MutableList<Gear> = mutableListOf()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue