1
0
This commit is contained in:
Hubert Van De Walle 2020-12-29 23:01:35 +01:00
parent 7a871771bb
commit 0ad5dac997

View File

@ -19,7 +19,7 @@ class Day20(@Groups val input: Input<List<List<String>>>) {
.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<List<List<String>>>) {
?.let { top -> candidate.firstRow() == top.lastRow() }
?: true
private fun isBottomOk(grid: Grid<*>, x: Int, y: Int, candidate: Tile, neighbours: MutableMap<Int, MutableList<Tile>>) =
private fun isBottomOk(grid: Grid<*>, x: Int, y: Int, candidate: Tile, neighbours: Map<Int, List<Tile>>) =
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<Int, MutableList<Tile>>) =
private fun isRightOk(grid: Grid<*>, x: Int, y: Int, candidate: Tile, neighbours: Map<Int, List<Tile>>) =
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<List<List<String>>>) {
for (y in 0 until grid.height) {
val row = grid.row(y)
for (yy in 0 until row[0]!!.height) {
val combinedRow = ArrayList<Char>()
val combinedRow = ArrayList<Char>().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<List<List<String>>>) {
private fun Tile.subGridData(startX: Int, startY: Int, width: Int, height: Int): List<List<Char>> {
val newData = ArrayList<ArrayList<Char>>()
for (y in startY until startY + height) {
val row = ArrayList<Char>()
newData.add(row)
val row = ArrayList<Char>().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<List<List<String>>>) {
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<List<List<String>>>) {
| # |
|# ## ## ###|
| # # # # # # |
""".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<List<List<String>>>) {
}
}
}
if (count != 0) {
println(count)
println(squares - (count * monsterSquares))
}
if (count != 0) return squares - (count * monsterSquares)
}
error("No monsters found")
}
}