Skip to content

Commit

Permalink
2024 - Day 12 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 12, 2024
1 parent e81f7fe commit 401b7db
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/main/kotlin/no/rodland/advent_2024/Day12.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package no.rodland.advent_2024

import no.rodland.advent.Day
import no.rodland.advent.Pos

// template generated: 12/12/2024
// Fredrik Rødland 2024

class Day12(val input: List<String>) : Day<Int, Int, Pair<Array<CharArray>, Array<CharArray>>> {

private val parsed = input.parse()
private val grid = parsed.first
private val rotated = parsed.second

data class Region(val c: Char, val positions: Set<Pos>) {
operator fun contains(pos: Pos): Boolean = pos in positions
fun area() = positions.size
fun neighbours() = positions.flatMap { it.neighbourCellsUDLR() }.filterNot { it in positions }
fun perimeter(): Int = neighbours().size
}

override fun partOne(): Int {
return findAllRegions().sumOf {
it.area() * it.perimeter()
}
}

private fun findAllRegions(): List<Region> {
val visited = mutableSetOf<Pos>()
return grid.flatMapIndexed { y, row ->
row.mapIndexed { x, c ->
val pos = Pos(x, y)
if (pos !in visited) {
Region(c, grid.getRegion(pos, visited))
} else {
null
}
}.filterNotNull()
}
}

@Suppress("ConvertCallChainIntoSequence")
private fun Array<CharArray>.getRegion(pos: Pos, visited: MutableSet<Pos>): Set<Pos> {
return (setOf(pos) + pos.neighbourCellsUDLR()
.filter { it !in visited }
.filter { it in grid }
.filter { grid[it] == grid[pos] }
.onEach { visited.add(it) }
.flatMap { getRegion(it, visited) }
.toList()).toSet()
}


override fun partTwo(): Int {
return 2
}

operator fun Grid.contains(pos: Pos): Boolean = pos.x >= 0 && pos.x < this[0].size && pos.y >= 0 && pos.y < this.size

operator fun Grid.get(pos: Pos): Char = this[pos.y][pos.x]

override fun List<String>.parse(): Pair<Array<CharArray>, Array<CharArray>> {
val upright = indices.map { y -> indices.map { x -> this[y][x] }.toCharArray() }.toTypedArray()
val rotated = indices.map { y -> indices.map { x -> this[x][y] }.toCharArray() }.toTypedArray()
return upright to rotated
}


override val day = "12".toInt()

// private fun Grid.fences(): Int {
// flatMap { row ->
// val windowed = row.toList().windowed(2).map { (c1, c2) ->
// if (c1 == c2) 0 else 2
// }
// windowed
// }
// return 2
// }


}


83 changes: 83 additions & 0 deletions src/test/kotlin/no/rodland/advent_2024/Day12Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package no.rodland.advent_2024

import no.rodland.advent.*
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import readFile

//
// run: download_aoc_input.sh to download input
//

@Suppress("ClassName")
@DisableSlow
internal class Day12Test {
private val data12 = "2024/input_12.txt".readFile()
private val test12 = "2024/input_12_test.txt".readFile()

private val resultTestOne = 1930
private val resultTestTwo = 2
private val resultOne = 1424472
private val resultTwo = 2

val test = defaultTestSuiteParseOnInit(
Day12(data12),
Day12(test12),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day12(data12) },
{ Day12(test12) },
numTestPart1 = 5
)

@Nested
inner class Init {
@Test
fun `12,-,example,1`() {
report(AOCTest({ "123".toInt() }, Unit, 123, 5, "12".toInt(), Part.TWO, false, "example"))
}

@Test
fun `12,-,example,2`() {
report(test.initTest.copy())
}

@Test
fun `12,-,test,init`() {
report(test.initTest)
}

@Test
fun `12,-,live,init`() {
report(test.initLive)
}
}

@Nested
inner class `Part 1` {
@Test
fun `12,1,test`() {
report(test.testPart1)
}

@Test
fun `12,1,live,1`() {
report(test.livePart1)
}
}

@Nested
inner class `Part 2` {
@Test
fun `12,2,test`() {
report(test.testPart2)
}

@Test
fun `12,2,live,1`() {
report(test.livePart2)
}
}
}
10 changes: 10 additions & 0 deletions src/test/resources/2024/input_12_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE

0 comments on commit 401b7db

Please sign in to comment.