Skip to content

Commit

Permalink
feat(2024/12): 2024 day 12
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Dec 14, 2024
1 parent 3333bb6 commit 64f1588
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions 2024/day12.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package `2024`.day12

import prelude.*
import scala.annotation.tailrec

extension (a: Pos)(using grid: Grid[Char])
inline infix def ===(b: Pos) = grid.get(a) == grid.get(b)
inline infix def !==(b: Pos) = !(a === b)

object Garden:
def parse(input: String) =
given grid: Grid[Char] = Grid(input)
val visited = collection.mutable.Set.empty[Pos]

def getRegion(p: Pos): Set[Pos] =
val region = collection.mutable.Set.empty[Pos]

@tailrec def go(xs: Seq[Pos]): Unit = xs match
case Nil =>
case x :: xs if visited.add(x) =>
region += x; go(x.neighbours.filter(_ === p) ++ xs)
case x :: xs => go(xs)

go(Seq(p)); region.toSet

Garden(grid, grid.points.map(getRegion).filter(_.nonEmpty).toSeq)

case class Garden(grid: Grid[Char], regions: Seq[Set[Pos]]):
given Grid[Char] = grid

def perimeter(region: Set[Pos]): Int =
region.sumBy { p => (p !== p.up).toInt + (p !== p.left).toInt } * 2

def corners(p: Pos): Int =
val Seq(nw, w, sw, n, s, ne, e, se) = p.neighbours8.map(_ === p)

(Seq((n && w && !nw), (n && e && !ne), (s && w && !sw), (s && e && !se))
++ Seq(!(n || w), !(n || e), !(s || w), !(s || e))).sumBy(_.toInt)

def part1(input: String): Int =
val garden = Garden.parse(input)

garden.regions.sumBy { xs => garden.perimeter(xs) * xs.size }

def part2(input: String): Int =
val garden = Garden.parse(input)

garden.regions.sumBy { xs => xs.sumBy(garden.corners) * xs.size }

@main def main() =
val input = readInput(this).mkString

println(part1(input))
println(part2(input))

0 comments on commit 64f1588

Please sign in to comment.