Skip to content

Commit

Permalink
Clean up day 4.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Dec 4, 2024
1 parent aff2017 commit bcf89d3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 89 deletions.
4 changes: 2 additions & 2 deletions 2019/src/main/kotlin/aoc/year2019/Day15.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ object Day15 : Puzzle<Int, Int>(15) {
length: Int,
) {
if (length > min) return
position.adjacent()
position.adjacentOrthogonal()
.filter { it !in visited }
.forEach { neighbor ->
val value = map.getValue(neighbor)
Expand Down Expand Up @@ -113,7 +113,7 @@ object Day15 : Puzzle<Int, Int>(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)) {
Expand Down
2 changes: 1 addition & 1 deletion 2023/src/main/kotlin/aoc/year2023/Day10.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object Day10 : Puzzle<Int, Int>(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 {
Expand Down
6 changes: 3 additions & 3 deletions 2023/src/main/kotlin/aoc/year2023/Day23.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object Day23 : Puzzle<Int, Int>(day = 23) {
if (!visited.add(from)) {
return
}
from.adjacent().forEach { adjacent ->
from.adjacentOrthogonal().forEach { adjacent ->
if (adjacent in pointsOfInterest) {
visitedPointsOfInterest += adjacent
} else {
Expand Down Expand Up @@ -98,7 +98,7 @@ object Day23 : Puzzle<Int, Int>(day = 23) {
}
}
.filter { (point, _) ->
point.adjacent().count {
point.adjacentOrthogonal().count {
when (grid[it]) {
Tile.Forest -> false
Tile.Path -> true
Expand Down Expand Up @@ -175,7 +175,7 @@ object Day23 : Puzzle<Int, Int>(day = 23) {
return
}
}
current.adjacent()
current.adjacentOrthogonal()
.forEach { adjacent ->
if (adjacent in stopPoints) {
return@forEach
Expand Down
80 changes: 16 additions & 64 deletions 2024/src/main/kotlin/aoc/year2024/Day4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,19 @@ object Day4 : Puzzle<Long, Long>(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))
}
}
}
Expand All @@ -51,51 +31,23 @@ object Day4 : Puzzle<Long, Long>(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)
}
34 changes: 15 additions & 19 deletions library/src/main/kotlin/aoc/library/Point.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@ import kotlin.math.atan2

data class Point(val x: Int, val y: Int) {

fun adjacent(includeDiagonal: Boolean = false): List<Point> {
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<Point> = adjacentOrthogonal() + adjacentDiagonal()

fun adjacentOrthogonal(): List<Point> = 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<Point> = 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

Expand Down

0 comments on commit bcf89d3

Please sign in to comment.