Skip to content

Commit

Permalink
Add corners solution to 2024 day 12 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 12, 2024
1 parent 1fab106 commit 468a33b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
22 changes: 20 additions & 2 deletions src/main/scala/eu/sim642/adventofcode2024/Day12.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ object Day12 {

BFS.components(graphComponents).size
}

def corners: Int = {
(for {
pos <- poss.iterator
offset <- Pos.axisOffsets
rightPos = pos + offset // right of diagPos
leftPos = pos + offset.left // left of diagPos
diagPos = leftPos + offset
if !poss(rightPos) && !poss(leftPos) || poss(rightPos) && poss(leftPos) && !poss(diagPos) // if is external or internal corner
//if !poss(rightPos) && (!poss(leftPos) || poss(diagPos)) // from glguy: actually counts beginning of side, not corner itself
} yield pos).size
}
}

def regions(grid: Grid[Char]): collection.Set[Region] = {
Expand Down Expand Up @@ -78,16 +90,22 @@ object Day12 {
override def regionFencingPrice(region: Region): Int = region.area * region.perimeter
}

object Part2 extends Part {
trait Part2Solution extends Part

object SidesPart2Solution extends Part2Solution {
override def regionFencingPrice(region: Region): Int = region.area * region.sides
}

object CornersPart2Solution extends Part2Solution {
override def regionFencingPrice(region: Region): Int = region.area * region.corners
}

def parseGrid(input: String): Grid[Char] = input.linesIterator.map(_.toVector).toVector

lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day12.txt")).mkString.trim

def main(args: Array[String]): Unit = {
println(Part1.totalFencingPrice(parseGrid(input)))
println(Part2.totalFencingPrice(parseGrid(input)))
println(CornersPart2Solution.totalFencingPrice(parseGrid(input)))
}
}
54 changes: 36 additions & 18 deletions src/test/scala/eu/sim642/adventofcode2024/Day12Test.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package eu.sim642.adventofcode2024

import Day12._
import Day12.*
import Day12Test.*
import org.scalatest.Suites
import org.scalatest.funsuite.AnyFunSuite

class Day12Test extends AnyFunSuite {
class Day12Test extends Suites(
new Part1Test,
new SidesPart2SolutionTest,
new CornersPart2SolutionTest,
)

object Day12Test {

val exampleInput =
"""AAAA
Expand Down Expand Up @@ -45,25 +53,35 @@ class Day12Test extends AnyFunSuite {
|ABBAAA
|AAAAAA""".stripMargin

test("Part 1 examples") {
assert(Part1.totalFencingPrice(parseGrid(exampleInput)) == 140)
assert(Part1.totalFencingPrice(parseGrid(exampleInput2)) == 772)
assert(Part1.totalFencingPrice(parseGrid(exampleInput3)) == 1930)
}
class Part1Test extends AnyFunSuite {

test("Part 1 input answer") {
assert(Part1.totalFencingPrice(parseGrid(input)) == 1433460)
}
test("Part 1 examples") {
assert(Part1.totalFencingPrice(parseGrid(exampleInput)) == 140)
assert(Part1.totalFencingPrice(parseGrid(exampleInput2)) == 772)
assert(Part1.totalFencingPrice(parseGrid(exampleInput3)) == 1930)
}

test("Part 2 examples") {
assert(Part2.totalFencingPrice(parseGrid(exampleInput)) == 80)
assert(Part2.totalFencingPrice(parseGrid(exampleInput2)) == 436)
assert(Part2.totalFencingPrice(parseGrid(exampleInput4)) == 236)
assert(Part2.totalFencingPrice(parseGrid(exampleInput5)) == 368)
assert(Part2.totalFencingPrice(parseGrid(exampleInput3)) == 1206)
test("Part 1 input answer") {
assert(Part1.totalFencingPrice(parseGrid(input)) == 1433460)
}
}

test("Part 2 input answer") {
assert(Part2.totalFencingPrice(parseGrid(input)) == 855082)
abstract class Part2SolutionTest(part2Solution: Part2Solution) extends AnyFunSuite {

test("Part 2 examples") {
assert(part2Solution.totalFencingPrice(parseGrid(exampleInput)) == 80)
assert(part2Solution.totalFencingPrice(parseGrid(exampleInput2)) == 436)
assert(part2Solution.totalFencingPrice(parseGrid(exampleInput4)) == 236)
assert(part2Solution.totalFencingPrice(parseGrid(exampleInput5)) == 368)
assert(part2Solution.totalFencingPrice(parseGrid(exampleInput3)) == 1206)
}

test("Part 2 input answer") {
assert(part2Solution.totalFencingPrice(parseGrid(input)) == 855082)
}
}

class SidesPart2SolutionTest extends Part2SolutionTest(SidesPart2Solution)

class CornersPart2SolutionTest extends Part2SolutionTest(CornersPart2Solution)
}

0 comments on commit 468a33b

Please sign in to comment.