From 7a06f3bf018d19cbf9552379e4d6f916c18b3bc5 Mon Sep 17 00:00:00 2001 From: dnutiu Date: Sun, 3 Dec 2023 18:58:12 +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 --- .../AdventOfCode2023/day3/GearRatios.kt | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt b/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt index fd412af..91adc7b 100644 --- a/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt +++ b/src/main/kotlin/AdventOfCode2023/day3/GearRatios.kt @@ -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) class GearRatios : Puzzle { @@ -12,7 +13,7 @@ class GearRatios : Puzzle { // Step 1: Scan for index parts. data.forEachIndexed { rowIndex, row -> val numberDigits: MutableList = 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 = scanForEnginePats(inputData) - var gears: MutableList> = mutableListOf() + val gears: MutableList = mutableListOf() inputData.forEachIndexed { row, rowElement -> rowElement.forEachIndexed { col, colElement -> if (colElement == '*') { val enginePartsForGear: MutableSet = 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") }