From d878d62593c0419b52e725726691be7e96e339b2 Mon Sep 17 00:00:00 2001 From: GeistInDerSH Date: Sun, 3 Dec 2023 11:39:03 -0700 Subject: [PATCH] Move Overlap Checking to a Separate Function Move the code used to check if the Number/Symbol are overlapping into its own function --- src/main/kotlin/day3/day3.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/day3/day3.kt b/src/main/kotlin/day3/day3.kt index ab70207..5f711e7 100644 --- a/src/main/kotlin/day3/day3.kt +++ b/src/main/kotlin/day3/day3.kt @@ -9,11 +9,7 @@ data class Symbol(val name: Char, val row: Int, val col: Int) { private val touchingNumbers: MutableSet = mutableSetOf() fun addAnyTouchingNumbers(numbers: List) { - val toAdd = numbers.filter { - (it.row in (row - 1..row + 1)) && (max(it.colStart, col - 1) <= min(it.colEnd, col + 1)) - }.map { - it.num - }.toSet() + val toAdd = numbers.filter { hasOverlap(it.row, row, it.colStart, it.colEnd, col) }.map { it.num }.toSet() touchingNumbers.addAll(toAdd) } @@ -28,14 +24,16 @@ data class Number(val num: Int, val row: Int, val colStart: Int, val colEnd: Int if (hasTouchingSymbol) { return } - hasTouchingSymbol = symbols.any { - (it.row in (row - 1..row + 1)) && (max(colStart, it.col - 1) <= min(colEnd, it.col + 1)) - } + hasTouchingSymbol = symbols.any { hasOverlap(it.row, row, colStart, colEnd, it.col) } } fun hasTouchingSymbol() = hasTouchingSymbol } +fun hasOverlap(srcRow: Int, destRow: Int, srcColStart: Int, srcColEnd: Int, destCol: Int): Boolean { + return srcRow in (destRow - 1..destRow + 1) && max(srcColStart, destCol - 1) <= min(srcColEnd, destCol + 1) +} + fun parseInput(fileName: String): Pair, List> { val lines = fileToStream(fileName).toList()