From a779043d9360b0c0cd938a61ec6db2bce22616f0 Mon Sep 17 00:00:00 2001 From: Andreas Lau Date: Sat, 21 Dec 2024 12:46:54 +0100 Subject: [PATCH] Extract Point class (#17) --- src/main/kotlin/days/Day12.kt | 19 ------------------- src/main/kotlin/days/Day18.kt | 25 ++++--------------------- src/main/kotlin/days/Day4.kt | 1 - src/main/kotlin/days/Day8.kt | 7 ------- src/main/kotlin/days/Geometry.kt | 22 ++++++++++++++++++++++ 5 files changed, 26 insertions(+), 48 deletions(-) create mode 100644 src/main/kotlin/days/Geometry.kt diff --git a/src/main/kotlin/days/Day12.kt b/src/main/kotlin/days/Day12.kt index 8784b1c..1c6c2e7 100644 --- a/src/main/kotlin/days/Day12.kt +++ b/src/main/kotlin/days/Day12.kt @@ -46,23 +46,4 @@ class Day12(val input: List) : 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.contains(p: Point) = any { it == p } - fun neighbors(): List = 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() - } - } - } \ No newline at end of file diff --git a/src/main/kotlin/days/Day18.kt b/src/main/kotlin/days/Day18.kt index b286945..923b680 100644 --- a/src/main/kotlin/days/Day18.kt +++ b/src/main/kotlin/days/Day18.kt @@ -17,12 +17,12 @@ class Day18(val input: List) : 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}" } @@ -37,24 +37,7 @@ class Day18(val input: List) : 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): Int { + private fun distance(bytes: List): Int { val queue = PriorityQueue> { a, b -> a.second.compareTo(b.second) } queue.add(start to 0) val seen = mutableSetOf() @@ -65,7 +48,7 @@ class Day18(val input: List) : 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) } diff --git a/src/main/kotlin/days/Day4.kt b/src/main/kotlin/days/Day4.kt index c6b57b3..27deaa9 100644 --- a/src/main/kotlin/days/Day4.kt +++ b/src/main/kotlin/days/Day4.kt @@ -43,5 +43,4 @@ class Day4(val memory: List) : Puzzle { return memory[y][x] } - data class Point(val x: Int, val y: Int) } \ No newline at end of file diff --git a/src/main/kotlin/days/Day8.kt b/src/main/kotlin/days/Day8.kt index 5cf88d7..52e7461 100644 --- a/src/main/kotlin/days/Day8.kt +++ b/src/main/kotlin/days/Day8.kt @@ -41,11 +41,4 @@ class Day8(val input: List) : 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) - } - } \ No newline at end of file diff --git a/src/main/kotlin/days/Geometry.kt b/src/main/kotlin/days/Geometry.kt new file mode 100644 index 0000000..bb6e0e2 --- /dev/null +++ b/src/main/kotlin/days/Geometry.kt @@ -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.contains(p: Point) = any { it == p } + fun neighbors(): List = 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) } + } +} \ No newline at end of file