🎄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 {
|
||||
|
||||
|
@ -12,7 +13,7 @@ class GearRatios : Puzzle {
|
|||
// Step 1: Scan for index parts.
|
||||
data.forEachIndexed { rowIndex, row ->
|
||||
val numberDigits: MutableList<Char> = mutableListOf()
|
||||
var currentEnginePart = EnginePart(0, 0, 0,0)
|
||||
var currentEnginePart = EnginePart(0, 0, 0, 0)
|
||||
var foundNumber = false
|
||||
row.forEachIndexed { colIndex, col ->
|
||||
// first digit
|
||||
|
@ -23,23 +24,23 @@ class GearRatios : Puzzle {
|
|||
numberDigits.add(col)
|
||||
}
|
||||
// inside number
|
||||
else if(col.isDigit() && foundNumber) {
|
||||
else if (col.isDigit() && foundNumber) {
|
||||
currentEnginePart.endIndex = colIndex
|
||||
numberDigits.add(col)
|
||||
}
|
||||
// exit number
|
||||
else if(!col.isDigit() && foundNumber) {
|
||||
else if (!col.isDigit() && foundNumber) {
|
||||
currentEnginePart.number = numberDigits.joinToString(separator = "").toInt()
|
||||
currentEnginePart.endIndex = colIndex-1
|
||||
currentEnginePart.endIndex = colIndex - 1
|
||||
foundEngineParts.add(currentEnginePart)
|
||||
// reset
|
||||
currentEnginePart = EnginePart(0, 0, 0,0)
|
||||
currentEnginePart = EnginePart(0, 0, 0, 0)
|
||||
numberDigits.clear()
|
||||
foundNumber = false
|
||||
}
|
||||
|
||||
// Number is at edge
|
||||
if (foundNumber && colIndex == row.length-1) {
|
||||
if (foundNumber && colIndex == row.length - 1) {
|
||||
currentEnginePart.number = numberDigits.joinToString(separator = "").toInt()
|
||||
currentEnginePart.endIndex = colIndex
|
||||
foundEngineParts.add(currentEnginePart)
|
||||
|
@ -56,16 +57,16 @@ class GearRatios : Puzzle {
|
|||
engineParts.forEach { enginePart ->
|
||||
for (i in enginePart.startIndex..enginePart.endIndex) {
|
||||
val movesOfXY = listOf(
|
||||
Pair(i-1, enginePart.row), // left
|
||||
Pair(i-1, enginePart.row-1), // let top
|
||||
Pair(i, enginePart.row-1),// top
|
||||
Pair(i+1, enginePart.row-1), // right top
|
||||
Pair(i+1, enginePart.row), // right
|
||||
Pair(i+1, enginePart.row+1), // right bottom
|
||||
Pair(i, enginePart.row+1), // bottom
|
||||
Pair(i-1, enginePart.row+1) // left bottom
|
||||
Pair(i - 1, enginePart.row), // left
|
||||
Pair(i - 1, enginePart.row - 1), // let top
|
||||
Pair(i, enginePart.row - 1),// top
|
||||
Pair(i + 1, enginePart.row - 1), // right top
|
||||
Pair(i + 1, enginePart.row), // right
|
||||
Pair(i + 1, enginePart.row + 1), // right bottom
|
||||
Pair(i, enginePart.row + 1), // bottom
|
||||
Pair(i - 1, enginePart.row + 1) // left bottom
|
||||
)
|
||||
movesOfXY.forEach movesForeach@ {
|
||||
movesOfXY.forEach movesForeach@{
|
||||
if ((it.second >= 0 && it.second < data.size) && (it.first >= 0 && it.first < data[it.second].length)) {
|
||||
val symbol = data[it.second][it.first]
|
||||
if (!symbol.isDigit() && symbol != '.') {
|
||||
|
@ -98,28 +99,28 @@ 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 ->
|
||||
if (colElement == '*') {
|
||||
val enginePartsForGear: MutableSet<EnginePart> = mutableSetOf()
|
||||
val movesOfXY = listOf(
|
||||
Pair(col-1, row), // left
|
||||
Pair(col-1, row-1), // let top
|
||||
Pair(col, row-1),// top
|
||||
Pair(col+1, row-1), // right top
|
||||
Pair(col+1, row), // right
|
||||
Pair(col+1, row+1), // right bottom
|
||||
Pair(col, row+1), // bottom
|
||||
Pair(col-1, row+1) // left bottom
|
||||
Pair(col - 1, row), // left
|
||||
Pair(col - 1, row - 1), // let top
|
||||
Pair(col, row - 1),// top
|
||||
Pair(col + 1, row - 1), // right top
|
||||
Pair(col + 1, row), // right
|
||||
Pair(col + 1, row + 1), // right bottom
|
||||
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()) {
|
||||
// search engine part
|
||||
foundEngineParts.forEach {part ->
|
||||
foundEngineParts.forEach { part ->
|
||||
if (part.row == it.second && (it.first >= part.startIndex && it.first <= part.endIndex)) {
|
||||
enginePartsForGear.add(part)
|
||||
}
|
||||
|
@ -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