Clean
This commit is contained in:
parent
7a871771bb
commit
0ad5dac997
@ -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) }
|
.map { it[0].let { it.substring(5 until it.indexOf(':')).toInt() } to it.drop(1) }
|
||||||
.associate { (id, tile) -> id to gridOf(tile) }
|
.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 {
|
private fun edgesMatch(a: Tile, b: Tile): Boolean {
|
||||||
val edges = b.allEdges()
|
val edges = b.allEdges()
|
||||||
@ -70,13 +70,13 @@ class Day20(@Groups val input: Input<List<List<String>>>) {
|
|||||||
?.let { top -> candidate.firstRow() == top.lastRow() }
|
?.let { top -> candidate.firstRow() == top.lastRow() }
|
||||||
?: true
|
?: 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
|
if (y == grid.lastRowIndex) true
|
||||||
else neighbours[neighboursCount(grid, x, y + 1)]!!
|
else neighbours[neighboursCount(grid, x, y + 1)]!!
|
||||||
.filterNot { it == candidate }
|
.filterNot { it == candidate }
|
||||||
.count { it.transformations().any { candidate.lastColumn() == it.firstColumn() } } == 1
|
.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
|
if (x == grid.lastColumnIndex) true
|
||||||
else neighbours[neighboursCount(grid, x + 1, y)]!!
|
else neighbours[neighboursCount(grid, x + 1, y)]!!
|
||||||
.filterNot { it == candidate }
|
.filterNot { it == candidate }
|
||||||
@ -148,9 +148,8 @@ class Day20(@Groups val input: Input<List<List<String>>>) {
|
|||||||
for (y in 0 until grid.height) {
|
for (y in 0 until grid.height) {
|
||||||
val row = grid.row(y)
|
val row = grid.row(y)
|
||||||
for (yy in 0 until row[0]!!.height) {
|
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)) }
|
row.forEach { combinedRow.addAll(it!!.row(yy)) }
|
||||||
newData.add(combinedRow)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Tile(newData)
|
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>> {
|
private fun Tile.subGridData(startX: Int, startY: Int, width: Int, height: Int): List<List<Char>> {
|
||||||
val newData = ArrayList<ArrayList<Char>>()
|
val newData = ArrayList<ArrayList<Char>>()
|
||||||
for (y in startY until startY + height) {
|
for (y in startY until startY + height) {
|
||||||
val row = ArrayList<Char>()
|
val row = ArrayList<Char>().also { newData.add(it) }
|
||||||
newData.add(row)
|
|
||||||
for (x in startX until startX + width) {
|
for (x in startX until startX + width) {
|
||||||
row.add(this[x, y]!!)
|
row.add(this[x, y]!!)
|
||||||
}
|
}
|
||||||
@ -174,7 +172,7 @@ class Day20(@Groups val input: Input<List<List<String>>>) {
|
|||||||
return monsterRe.matches(str)
|
return monsterRe.matches(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun part2() {
|
fun part2(): Int {
|
||||||
val grid = assembleGrid()
|
val grid = assembleGrid()
|
||||||
removeGaps(grid)
|
removeGaps(grid)
|
||||||
val megaTile = gridToTile(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) }
|
""".trimMargin("|").lines().map { it.toCharArray().dropLast(1) }
|
||||||
|
|
||||||
println(monster)
|
|
||||||
|
|
||||||
val monsterWidth = monster[0].size
|
val monsterWidth = monster[0].size
|
||||||
val monsterHeight = 3
|
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 in megaTile.transformations()) {
|
||||||
|
|
||||||
for (g: Tile in megaTile.transformations()) {
|
|
||||||
var count = 0
|
var count = 0
|
||||||
for (y in 0 until g.lastRowIndex - monsterHeight) {
|
for (y in 0 until g.lastRowIndex - monsterHeight) {
|
||||||
for (x in 0 until g.lastColumnIndex - monsterWidth) {
|
for (x in 0 until g.lastColumnIndex - monsterWidth) {
|
||||||
@ -203,11 +198,9 @@ class Day20(@Groups val input: Input<List<List<String>>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (count != 0) {
|
if (count != 0) return squares - (count * monsterSquares)
|
||||||
println(count)
|
|
||||||
println(squares - (count * monsterSquares))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
error("No monsters found")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user