Make fast

This commit is contained in:
Christian
2024-12-13 14:50:13 +01:00
parent c9d89a2b7e
commit 9593204edb
2 changed files with 11 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ case class Point(x: Int, y: Int):
enum Direction:
case Up, Right, Down, Left
def turnRight: Direction = Direction.fromOrdinal((ordinal + 1) % 4)
def turnLeft: Direction = Direction.fromOrdinal((ordinal - 1 + 4) % 4)
def toPoint: Point =
this match

View File

@@ -54,13 +54,16 @@ object Day12 extends Solver(12):
plots.sizes.map((k, v) => v * plots.edges(k).length).sum
def part2(plots: Plots): Int =
plots.sizes
.map((id, size) =>
val edge = plots.edges(id).toSet
val corners = edge -- edge.map((pt, dir) => (dir.turnRight(pt), dir))
size * corners.size
)
.sum
def is(pt: Point, id: Int): Boolean = plots.layout(pt).contains(id)
def cost(id: Int, size: Int): Int =
size * plots
.edges(id)
.map((pt, dir) =>
Seq(dir.turnLeft(pt), dir.turnRight(pt))
.count(adj => is(dir(adj), id) || !is(adj, id))
)
.sum / 2
plots.sizes.map(cost).sum
override def run(input: os.ReadablePath): (Timings, Solution) =
val grid = Grid(os.read.lines(input).map(_.toArray).toArray)