Make fast
This commit is contained in:
@@ -9,6 +9,7 @@ case class Point(x: Int, y: Int):
|
|||||||
enum Direction:
|
enum Direction:
|
||||||
case Up, Right, Down, Left
|
case Up, Right, Down, Left
|
||||||
def turnRight: Direction = Direction.fromOrdinal((ordinal + 1) % 4)
|
def turnRight: Direction = Direction.fromOrdinal((ordinal + 1) % 4)
|
||||||
|
def turnLeft: Direction = Direction.fromOrdinal((ordinal - 1 + 4) % 4)
|
||||||
|
|
||||||
def toPoint: Point =
|
def toPoint: Point =
|
||||||
this match
|
this match
|
||||||
|
|||||||
@@ -54,13 +54,16 @@ object Day12 extends Solver(12):
|
|||||||
plots.sizes.map((k, v) => v * plots.edges(k).length).sum
|
plots.sizes.map((k, v) => v * plots.edges(k).length).sum
|
||||||
|
|
||||||
def part2(plots: Plots): Int =
|
def part2(plots: Plots): Int =
|
||||||
plots.sizes
|
def is(pt: Point, id: Int): Boolean = plots.layout(pt).contains(id)
|
||||||
.map((id, size) =>
|
def cost(id: Int, size: Int): Int =
|
||||||
val edge = plots.edges(id).toSet
|
size * plots
|
||||||
val corners = edge -- edge.map((pt, dir) => (dir.turnRight(pt), dir))
|
.edges(id)
|
||||||
size * corners.size
|
.map((pt, dir) =>
|
||||||
|
Seq(dir.turnLeft(pt), dir.turnRight(pt))
|
||||||
|
.count(adj => is(dir(adj), id) || !is(adj, id))
|
||||||
)
|
)
|
||||||
.sum
|
.sum / 2
|
||||||
|
plots.sizes.map(cost).sum
|
||||||
|
|
||||||
override def run(input: os.ReadablePath): (Timings, Solution) =
|
override def run(input: os.ReadablePath): (Timings, Solution) =
|
||||||
val grid = Grid(os.read.lines(input).map(_.toArray).toArray)
|
val grid = Grid(os.read.lines(input).map(_.toArray).toArray)
|
||||||
|
|||||||
Reference in New Issue
Block a user