Make fast
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user