From bcf89d33d8e3cf648da44a80abb61b1d054b2505 Mon Sep 17 00:00:00 2001 From: Paul Woitaschek Date: Wed, 4 Dec 2024 09:31:18 +0100 Subject: [PATCH] Clean up day 4. --- 2019/src/main/kotlin/aoc/year2019/Day15.kt | 4 +- 2023/src/main/kotlin/aoc/year2023/Day10.kt | 2 +- 2023/src/main/kotlin/aoc/year2023/Day23.kt | 6 +- 2024/src/main/kotlin/aoc/year2024/Day4.kt | 80 ++++---------------- library/src/main/kotlin/aoc/library/Point.kt | 34 ++++----- 5 files changed, 37 insertions(+), 89 deletions(-) diff --git a/2019/src/main/kotlin/aoc/year2019/Day15.kt b/2019/src/main/kotlin/aoc/year2019/Day15.kt index 84010312..b2e94d1b 100644 --- a/2019/src/main/kotlin/aoc/year2019/Day15.kt +++ b/2019/src/main/kotlin/aoc/year2019/Day15.kt @@ -75,7 +75,7 @@ object Day15 : Puzzle(15) { length: Int, ) { if (length > min) return - position.adjacent() + position.adjacentOrthogonal() .filter { it !in visited } .forEach { neighbor -> val value = map.getValue(neighbor) @@ -113,7 +113,7 @@ object Day15 : Puzzle(15) { var minute = 0 while (true) { val new = oxygen - .flatMap { it.adjacent() } + .flatMap { it.adjacentOrthogonal() } .filter { it !in oxygen && map[it] != Tile.Wall } .toSet() if (!oxygen.addAll(new)) { diff --git a/2023/src/main/kotlin/aoc/year2023/Day10.kt b/2023/src/main/kotlin/aoc/year2023/Day10.kt index 059e7972..921ae961 100644 --- a/2023/src/main/kotlin/aoc/year2023/Day10.kt +++ b/2023/src/main/kotlin/aoc/year2023/Day10.kt @@ -24,7 +24,7 @@ object Day10 : Puzzle(day = 10) { else -> path(path + next) } } - return start.adjacent().firstNotNullOf { path(listOf(start, it)) } + return start.adjacentOrthogonal().firstNotNullOf { path(listOf(start, it)) } } override fun solvePart2(input: String): Int { diff --git a/2023/src/main/kotlin/aoc/year2023/Day23.kt b/2023/src/main/kotlin/aoc/year2023/Day23.kt index 417105cf..6f1edcad 100644 --- a/2023/src/main/kotlin/aoc/year2023/Day23.kt +++ b/2023/src/main/kotlin/aoc/year2023/Day23.kt @@ -54,7 +54,7 @@ object Day23 : Puzzle(day = 23) { if (!visited.add(from)) { return } - from.adjacent().forEach { adjacent -> + from.adjacentOrthogonal().forEach { adjacent -> if (adjacent in pointsOfInterest) { visitedPointsOfInterest += adjacent } else { @@ -98,7 +98,7 @@ object Day23 : Puzzle(day = 23) { } } .filter { (point, _) -> - point.adjacent().count { + point.adjacentOrthogonal().count { when (grid[it]) { Tile.Forest -> false Tile.Path -> true @@ -175,7 +175,7 @@ object Day23 : Puzzle(day = 23) { return } } - current.adjacent() + current.adjacentOrthogonal() .forEach { adjacent -> if (adjacent in stopPoints) { return@forEach diff --git a/2024/src/main/kotlin/aoc/year2024/Day4.kt b/2024/src/main/kotlin/aoc/year2024/Day4.kt index 86a3db20..89a842e1 100644 --- a/2024/src/main/kotlin/aoc/year2024/Day4.kt +++ b/2024/src/main/kotlin/aoc/year2024/Day4.kt @@ -8,39 +8,19 @@ object Day4 : Puzzle(day = 4) { override fun solvePart1(input: String): Long { val lines = input.lines() var count = 0L - fun charAt(point: Point): Char? = lines.getOrNull(point.y)?.getOrNull(point.x) + val allDirections = Point(0, 0).adjacent() + lines.forEachIndexed { y, line -> line.forEachIndexed { x, char -> if (char == 'X') { - val allDirections = listOf( - Point(1, -1), - Point(1, 0), - Point(1, 1), - Point(0, -1), - Point(0, 1), - Point(-1, -1), - Point(-1, 0), - Point(-1, 1), - ) val from = Point(x, y) - - allDirections.map { direction -> - val charlist = generateSequence(from) { - Point(it.x + direction.x, it.y + direction.y) - }.take(4) - .mapNotNull { charAt(it) } - .toList() - val result = charlist.joinToString("") - // println(result) - if (result == "XMAS") { - count++ - } - } - - allDirections.map { + count += allDirections.count { direction -> + val result = generateSequence(from) { it + direction } + .take(4) + .mapNotNull { lines.getOrNull(it.y)?.getOrNull(it.x) } + .joinToString("") + result == "XMAS" } - - listOf(Point(x, y), Point(x + 1, y), Point(x + 2, y), Point(x + 3, y)) } } } @@ -51,51 +31,23 @@ object Day4 : Puzzle(day = 4) { override fun solvePart2(input: String): Long { val lines = input.lines() var count = 0L - fun charAt(point: Point): Char? = lines.getOrNull(point.y)?.getOrNull(point.x) + lines.forEachIndexed { y, line -> line.forEachIndexed { x, char -> if (char == 'A') { - val allDirections = listOf( - Point(1, -1), - Point(1, 1), - Point(-1, -1), - Point(-1, 1), - ) - val from = Point(x, y) - - val m = listOf( - listOf(Point(x - 1, y - 1), Point(x + 1, y + 1)), - listOf(Point(x - 1, y + 1), Point(x + 1, y - 1)), - ).all { - val s = it.mapNotNull { charAt(it) }.joinToString("") - // println(s) - s == "SM" || s == "MS" - } - if (m)count++ - - allDirections.map { direction -> - val charlist = generateSequence(from) { - Point(it.x + direction.x, it.y + direction.y) - }.take(2) - .mapNotNull { charAt(it) } - .toList() - val result = charlist.joinToString("") - // println(result) - if (result == "MAS" || result == "SAM") { - // count++ + val topLeftAndBottomRight = listOf(Point(x - 1, y - 1), Point(x + 1, y + 1)) + val bottomLeftAndTopRight = listOf(Point(x - 1, y + 1), Point(x + 1, y - 1)) + val diagonalsOnlySM = listOf(topLeftAndBottomRight, bottomLeftAndTopRight) + .map { points -> + points.map { lines.getOrNull(it.y)?.getOrNull(it.x) }.toSet() } - } + .all { it == setOf('S', 'M') } - allDirections.map { - } - - listOf(Point(x, y), Point(x + 1, y), Point(x + 2, y), Point(x + 3, y)) + if (diagonalsOnlySM) count++ } } } return count } - - data class Point(val x: Int, val y: Int) } diff --git a/library/src/main/kotlin/aoc/library/Point.kt b/library/src/main/kotlin/aoc/library/Point.kt index 1c70fad2..84f5993f 100644 --- a/library/src/main/kotlin/aoc/library/Point.kt +++ b/library/src/main/kotlin/aoc/library/Point.kt @@ -5,25 +5,21 @@ import kotlin.math.atan2 data class Point(val x: Int, val y: Int) { - fun adjacent(includeDiagonal: Boolean = false): List { - val horizontal = listOf( - Point(x = x, y = y - 1), - Point(x = x, y = y + 1), - Point(x = x - 1, y = y), - Point(x = x + 1, y = y), - ) - return if (includeDiagonal) { - val diagonal = listOf( - Point(x = x - 1, y = y - 1), - Point(x = x - 1, y = y + 1), - Point(x = x + 1, y = y - 1), - Point(x = x + 1, y = y + 1), - ) - horizontal + diagonal - } else { - horizontal - } - } + fun adjacent(): List = adjacentOrthogonal() + adjacentDiagonal() + + fun adjacentOrthogonal(): List = listOf( + Point(x = x, y = y - 1), + Point(x = x, y = y + 1), + Point(x = x - 1, y = y), + Point(x = x + 1, y = y), + ) + + fun adjacentDiagonal(): List = listOf( + Point(x = x - 1, y = y - 1), + Point(x = x - 1, y = y + 1), + Point(x = x + 1, y = y - 1), + Point(x = x + 1, y = y + 1), + ) fun manhattanDistanceTo(other: Point): Int = (x - other.x).absoluteValue + (y - other.y).absoluteValue