diff --git a/days/src/main/kotlin/Day20.kt b/days/src/main/kotlin/Day20.kt index 556b10d..5421d0f 100644 --- a/days/src/main/kotlin/Day20.kt +++ b/days/src/main/kotlin/Day20.kt @@ -19,7 +19,7 @@ class Day20(@Groups val input: Input>>) { .map { it[0].let { it.substring(5 until it.indexOf(':')).toInt() } to it.drop(1) } .associate { (id, tile) -> id to gridOf(tile) } - private fun Tile.allEdges() = transformations().flatMap { it.edges() } + private fun Tile.allEdges() = listOf(edges(), edges().map { it.reversed() }).flatten() private fun edgesMatch(a: Tile, b: Tile): Boolean { val edges = b.allEdges() @@ -70,13 +70,13 @@ class Day20(@Groups val input: Input>>) { ?.let { top -> candidate.firstRow() == top.lastRow() } ?: true - private fun isBottomOk(grid: Grid<*>, x: Int, y: Int, candidate: Tile, neighbours: MutableMap>) = + private fun isBottomOk(grid: Grid<*>, x: Int, y: Int, candidate: Tile, neighbours: Map>) = if (y == grid.lastRowIndex) true else neighbours[neighboursCount(grid, x, y + 1)]!! .filterNot { it == candidate } .count { it.transformations().any { candidate.lastColumn() == it.firstColumn() } } == 1 - private fun isRightOk(grid: Grid<*>, x: Int, y: Int, candidate: Tile, neighbours: MutableMap>) = + private fun isRightOk(grid: Grid<*>, x: Int, y: Int, candidate: Tile, neighbours: Map>) = if (x == grid.lastColumnIndex) true else neighbours[neighboursCount(grid, x + 1, y)]!! .filterNot { it == candidate } @@ -148,9 +148,8 @@ class Day20(@Groups val input: Input>>) { for (y in 0 until grid.height) { val row = grid.row(y) for (yy in 0 until row[0]!!.height) { - val combinedRow = ArrayList() + val combinedRow = ArrayList().also { newData.add(it) } row.forEach { combinedRow.addAll(it!!.row(yy)) } - newData.add(combinedRow) } } return Tile(newData) @@ -159,8 +158,7 @@ class Day20(@Groups val input: Input>>) { private fun Tile.subGridData(startX: Int, startY: Int, width: Int, height: Int): List> { val newData = ArrayList>() for (y in startY until startY + height) { - val row = ArrayList() - newData.add(row) + val row = ArrayList().also { newData.add(it) } for (x in startX until startX + width) { row.add(this[x, y]!!) } @@ -174,7 +172,7 @@ class Day20(@Groups val input: Input>>) { return monsterRe.matches(str) } - fun part2() { + fun part2(): Int { val grid = assembleGrid() removeGaps(grid) val megaTile = gridToTile(grid) @@ -183,17 +181,14 @@ class Day20(@Groups val input: Input>>) { | # | |# ## ## ###| | # # # # # # | - """.trimMargin("|").lines().map { it.toCharArray().toList().dropLast(1) } - - println(monster) + """.trimMargin("|").lines().map { it.toCharArray().dropLast(1) } val monsterWidth = monster[0].size val monsterHeight = 3 - val monsterSquares = monster.joinToString("") { it.joinToString("") }.count { it == '#' } + val monsterSquares = monster.flatten().count { it == '#' } + val squares = megaTile.data.flatten().count { it == '#' } - val squares = megaTile.data.joinToString("") { it.joinToString("") }.count { it == '#' } - - for (g: Tile in megaTile.transformations()) { + for (g in megaTile.transformations()) { var count = 0 for (y in 0 until g.lastRowIndex - monsterHeight) { for (x in 0 until g.lastColumnIndex - monsterWidth) { @@ -203,11 +198,9 @@ class Day20(@Groups val input: Input>>) { } } } - if (count != 0) { - println(count) - println(squares - (count * monsterSquares)) - } + if (count != 0) return squares - (count * monsterSquares) } + error("No monsters found") } }