Skip to content

A01: Algebraic improvements #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/kotlin/common/aocreader/aocreader.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Advent of Code 2024
// Advent of Code 2024.
// By Sebastian Raaphorst, 2024.

package common.aocreader
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/common/collectionops/collectionops.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Advent of Code 2024
// Advent of Code 2024.
// By Sebastian Raaphorst, 2024.

package common.collectionops
Expand Down
25 changes: 13 additions & 12 deletions src/main/kotlin/common/gridalgorithms/floodfill.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// Advent of Code 2024
// Advent of Code 2024.
// By Sebastian Raaphorst, 2024.

package common.gridalgorithms

import common.intpos2d.*
import common.vec2d.*

data class Region(val area: Int, val perimeter: Int, val edges: Int)

fun findRegions(grid: Grid<Char>): List<Region> {
val visited = mutableSetOf<IntPos2D>()
val visited = mutableSetOf<Vec2D<Int>>()
val rows = grid.size
val cols = grid[0].size

fun neighbours(pos: IntPos2D): List<IntPos2D> =
fun neighbours(pos: Vec2DInt): List<Vec2DInt> =
Direction.entries.map { dir ->
pos + dir.delta
}.filter { pos -> pos.first in 0 until rows && pos.second in 0 until cols }
}.filter { pos -> pos.x in 0 until rows && pos.y in 0 until cols }

fun floodFill(start: IntPos2D): Region {
fun floodFill(start: Vec2DInt): Region {
val stack = mutableListOf(start)
var area = 0
var perimeter = 0
val symbol = grid[start.first][start.second]
val symbol = grid[start.x][start.y]
var corners = 0

while (stack.isNotEmpty()) {
Expand All @@ -34,11 +34,11 @@ fun findRegions(grid: Grid<Char>): List<Region> {
// as well as the walls that are out of bounds.
val neighbours = neighbours(pos)
val localPerimeter = neighbours.count { pos2 ->
grid[pos2.first][pos2.second] != symbol
grid[pos2.x][pos2.y] != symbol
}
val outOfBounds = Direction.entries.map { dir -> pos + dir.delta }
.count { pos2 -> pos2.first < 0 || pos2.first >= rows
|| pos2.second < 0 || pos2.second >= cols }
.count { pos2 -> pos2.x < 0 || pos2.x >= rows
|| pos2.y < 0 || pos2.y >= cols }
perimeter += localPerimeter + outOfBounds

// Calculate the corners, which will ultimately give us the number of
Expand All @@ -62,7 +62,7 @@ fun findRegions(grid: Grid<Char>): List<Region> {
// Add valid neighbors to the stack
stack.addAll(
neighbours(pos).filter { pos2 ->
grid[pos2.first][pos2.second] == symbol && pos2 !in visited
grid[pos2.x][pos2.y] == symbol && pos2 !in visited
}
)
}
Expand All @@ -76,7 +76,8 @@ fun findRegions(grid: Grid<Char>): List<Region> {
// Iterate over the grid and find all regions
return (0 until rows).flatMap { x ->
(0 until cols).mapNotNull { y ->
if (IntPos2D(x, y) !in visited) floodFill(x to y) else null
val vec = Vec2D.int(x, y)
if (vec !in visited) floodFill(vec) else null
}
}
}
18 changes: 9 additions & 9 deletions src/main/kotlin/common/gridalgorithms/grid.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// Advent of Code 2024
// Advent of Code 2024.
// By Sebastian Raaphorst, 2024.

package common.gridalgorithms

import common.intpos2d.Direction
import common.intpos2d.*
import common.vec2d.Direction
import common.vec2d.*

typealias Grid<T> = List<List<T>>

operator fun <T> Grid<T>.contains(pos: IntPos2D): Boolean =
pos.first in this.indices && pos.second in this[pos.first].indices
operator fun <T> Grid<T>.contains(pos: Vec2DInt): Boolean =
pos.x in this.indices && pos.y in this[pos.x].indices

operator fun <T> Grid<T>.get(pos: IntPos2D): T? =
if (pos in this) this[pos.first][pos.second] else null
operator fun <T> Grid<T>.get(pos: Vec2DInt): T? =
if (pos in this) this[pos.x][pos.y] else null

fun <T> Grid<T>.neighbourPositions(pos: IntPos2D): Set<IntPos2D> =
fun <T> Grid<T>.neighbourPositions(pos: Vec2DInt): Set<Vec2DInt> =
Direction.entries.map { pos + it.delta }
.filter { it in this }
.toSet()

// Get the value neighbours, and not the position neighbours.
fun <T> Grid<T>.neighbourValues(pos: IntPos2D): Set<T> =
fun <T> Grid<T>.neighbourValues(pos: Vec2DInt): Set<T> =
Direction.entries.mapNotNull { this[pos + it.delta] }.toSet()
79 changes: 0 additions & 79 deletions src/main/kotlin/common/intpos2d/intpos2d.kt

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/kotlin/common/parsing/parsing.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Advent of Code 2024
// Advent of Code 2024.
// By Sebastian Raaphorst, 2024.

package common.parsing
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/common/runner/timed.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Advent of Code 2024
// Advent of Code 2024.
// By Sebastian Raaphorst, 2024.

package common.runner
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/common/stringops.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Advent of Code 2024
// Advent of Code 2024.
// By Sebastian Raaphorst, 2024.

package common
Expand Down
Loading
Loading