Skip to content

Commit

Permalink
Extract Point class (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
andilau authored Dec 21, 2024
1 parent 9b6d8cd commit a779043
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 48 deletions.
19 changes: 0 additions & 19 deletions src/main/kotlin/days/Day12.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,4 @@ class Day12(val input: List<String>) : Puzzle {
val sides = corners
}

data class Point(val x: Int, val y: Int) {
operator fun plus(other: Point) = Point(x + other.x, y + other.y)
operator fun minus(other: Point) = Point(x - other.x, y - other.y)
operator fun times(o: Int) = Point(x * o, y * o)
operator fun Int.times(p: Point) = Point(p.x * this, p.y * this)
operator fun List<Point>.contains(p: Point) = any { it == p }
fun neighbors(): List<Point> = NEIGHBORS.map { this + it }
fun corners() = CORNERS.map { (a, b) -> listOf(this + a, this + a + b, this + b) }

companion object {
private val NORTH = Point(-1, 0)
private val WEST = Point(0, -1)
private val SOUTH = Point(1, 0)
private val EAST = Point(0, 1)
val NEIGHBORS = listOf(EAST, SOUTH, WEST, NORTH)
val CORNERS = listOf(EAST, SOUTH, WEST, NORTH, EAST).zipWithNext()
}
}

}
25 changes: 4 additions & 21 deletions src/main/kotlin/days/Day18.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class Day18(val input: List<String>) : Puzzle {
private val start = Point(range.start, range.start)
private val exit = Point(range.last, range.last)

override fun partOne() = path(bytes)
override fun partOne() = distance(bytes)

override fun partTwo(): String = (elements..input.lastIndex).firstNotNullOf {
val b = input.take(it).map { line -> Point.from(line) }
try {
path(b)
distance(b)
null
} catch (e: Exception) {
b.last().let { (x, y) -> "${x},${y}" }
Expand All @@ -37,24 +37,7 @@ class Day18(val input: List<String>) : Puzzle {
}
}

data class Point(val x: Int, val y: Int) {
operator fun plus(other: Point) = Point(x + other.x, y + other.y)
operator fun minus(other: Point) = Point(x - other.x, y - other.y)
operator fun times(o: Int) = Point(x * o, y * o)

fun neighbours() = setOf(
copy(x = x + 1),
copy(y = y + 1),
copy(x = x - 1),
copy(y = y - 1),
)

companion object {
fun from(line: String) = line.split(',').map { it.toInt() }.let { (x, y) -> Point(x, y) }
}
}

private fun path(bytes: List<Point>): Int {
private fun distance(bytes: List<Point>): Int {
val queue = PriorityQueue<Pair<Point, Int>> { a, b -> a.second.compareTo(b.second) }
queue.add(start to 0)
val seen = mutableSetOf<Point>()
Expand All @@ -65,7 +48,7 @@ class Day18(val input: List<String>) : Puzzle {
if (next in seen) continue
seen += next
if (next == exit) return cost
next.neighbours()
next.neighbors()
.filter { it.x in range && it.y in range }
.filter { it !in bytes }
.forEach { queue.add(it to cost + 1) }
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/days/Day4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ class Day4(val memory: List<String>) : Puzzle {
return memory[y][x]
}

data class Point(val x: Int, val y: Int)
}
7 changes: 0 additions & 7 deletions src/main/kotlin/days/Day8.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,4 @@ class Day8(val input: List<String>) : Puzzle {
if (inBounds(next)) next else null
}

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

operator fun plus(other: Point) = Point(x + other.x, y + other.y)
operator fun minus(other: Point) = Point(x - other.x, y - other.y)
operator fun times(o: Int) = Point(x * o, y * o)
}

}
22 changes: 22 additions & 0 deletions src/main/kotlin/days/Geometry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package days

data class Point(val x: Int, val y: Int) {
operator fun plus(other: Point) = Point(x + other.x, y + other.y)
operator fun minus(other: Point) = Point(x - other.x, y - other.y)
operator fun times(o: Int) = Point(x * o, y * o)
operator fun Int.times(p: Point) = Point(p.x * this, p.y * this)
operator fun List<Point>.contains(p: Point) = any { it == p }
fun neighbors(): List<Point> = NEIGHBORS.map { this + it }
fun corners() = CORNERS.map { (a, b) -> listOf(this + a, this + a + b, this + b) }

companion object {
private val NORTH = Point(-1, 0)
private val WEST = Point(0, -1)
private val SOUTH = Point(1, 0)
private val EAST = Point(0, 1)
val NEIGHBORS = listOf(EAST, SOUTH, WEST, NORTH)
val CORNERS = listOf(EAST, SOUTH, WEST, NORTH, EAST).zipWithNext()

fun from(line: String) = line.split(',').map { it.toInt() }.let { (x, y) -> Point(x, y) }
}
}

0 comments on commit a779043

Please sign in to comment.