🎄Advent of Code - Day 3 ✨ part 2
This commit is contained in:
parent
7d5c2834c2
commit
7a06f3bf01
1 changed files with 30 additions and 29 deletions
|
@ -3,6 +3,7 @@ package AdventOfCode2023.day3
|
|||
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<EnginePart>)
|
||||
|
||||
class GearRatios : Puzzle {
|
||||
|
||||
|
@ -98,7 +99,7 @@ class GearRatios : Puzzle {
|
|||
override fun partTwo() {
|
||||
val inputData = this.readInputFromFile("3")
|
||||
val foundEngineParts: List<EnginePart> = scanForEnginePats(inputData)
|
||||
var gears: MutableList<MutableSet<EnginePart>> = mutableListOf()
|
||||
val gears: MutableList<Gear> = mutableListOf()
|
||||
|
||||
inputData.forEachIndexed { row, rowElement ->
|
||||
rowElement.forEachIndexed { col, colElement ->
|
||||
|
@ -114,7 +115,7 @@ class GearRatios : Puzzle {
|
|||
Pair(col, row + 1), // bottom
|
||||
Pair(col - 1, row + 1) // left bottom
|
||||
)
|
||||
movesOfXY.forEach movesForeach@ {
|
||||
movesOfXY.forEach {
|
||||
if ((it.second >= 0 && it.second < inputData.size) && (it.first >= 0 && it.first < inputData[it.second].length)) {
|
||||
val symbol = inputData[it.second][it.first]
|
||||
if (symbol.isDigit()) {
|
||||
|
@ -127,13 +128,13 @@ class GearRatios : Puzzle {
|
|||
}
|
||||
}
|
||||
}
|
||||
gears.add(enginePartsForGear)
|
||||
gears.add(Gear(row, col, enginePartsForGear))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gears.filter { it.size == 2 }.map {
|
||||
it.map { it.number }.reduce { acc, i -> acc * i }
|
||||
gears.filter { it.engineParts.size == 2 }.map {
|
||||
it.engineParts.map { it.number }.reduce { acc, i -> acc * i }
|
||||
}.sum().let {
|
||||
println("The gear ratio is $it")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue