diff --git a/days/src/main/kotlin/Day20.kt b/days/src/main/kotlin/Day20.kt index cd45d88..556b10d 100644 --- a/days/src/main/kotlin/Day20.kt +++ b/days/src/main/kotlin/Day20.kt @@ -7,6 +7,8 @@ import be.vandewalleh.aoc.utils.input.Day import be.vandewalleh.aoc.utils.input.Groups import be.vandewalleh.aoc.utils.input.Input import be.vandewalleh.aoc.utils.input.createDay +import java.util.* +import kotlin.collections.ArrayList import kotlin.math.sqrt private typealias Tile = Grid @@ -121,126 +123,96 @@ class Day20(@Groups val input: Input>>) { return grid } + private fun removeGaps(grid: Grid) { + for (y in 0 until grid.height) { + for (x in 0 until grid.width) { + grid[x, y] = removeGaps(grid[x, y]!!) + } + } + } + + private fun removeGaps(tile: Tile): Tile { + val oldData: ArrayList> = tile.data + val newData = ArrayList>(oldData.size - 2) + oldData.subList(1, oldData.size - 1).forEach { d -> + val l = ArrayList().apply { + addAll(d.subList(1, d.size - 1)) + } + newData.add(l) + } + return Tile(newData) + } + + private fun gridToTile(grid: Grid): Tile { + val newData = ArrayList>() + for (y in 0 until grid.height) { + val row = grid.row(y) + for (yy in 0 until row[0]!!.height) { + val combinedRow = ArrayList() + row.forEach { combinedRow.addAll(it!!.row(yy)) } + newData.add(combinedRow) + } + } + return Tile(newData) + } + + 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) + for (x in startX until startX + width) { + row.add(this[x, y]!!) + } + } + return newData + } + + private fun List>.isMonster(monster: List>): Boolean { + val monsterRe = monster.joinToString("") { it.joinToString("") }.replace(" ", ".").toRegex() + val str = this.joinToString("") { it.joinToString("") } + return monsterRe.matches(str) + } + fun part2() { val grid = assembleGrid() - TODO() + removeGaps(grid) + val megaTile = gridToTile(grid) + + val monster: List> = """ + | # | + |# ## ## ###| + | # # # # # # | + """.trimMargin("|").lines().map { it.toCharArray().toList().dropLast(1) } + + println(monster) + + val monsterWidth = monster[0].size + val monsterHeight = 3 + val monsterSquares = monster.joinToString("") { it.joinToString("") }.count { it == '#' } + + val squares = megaTile.data.joinToString("") { it.joinToString("") }.count { it == '#' } + + for (g: Tile in megaTile.transformations()) { + var count = 0 + for (y in 0 until g.lastRowIndex - monsterHeight) { + for (x in 0 until g.lastColumnIndex - monsterWidth) { + val subgrid = g.subGridData(x, y, monsterWidth, monsterHeight) + if (subgrid.isMonster(monster)) { + count++ + } + } + } + if (count != 0) { + println(count) + println(squares - (count * monsterSquares)) + } + } } } -fun main() { - with(createDay( - """ - Tile 2311: - ..##.#..#. - ##..#..... - #...##..#. - ####.#...# - ##.##.###. - ##...#.### - .#.#.#..## - ..#....#.. - ###...#.#. - ..###..### - - Tile 1951: - #.##...##. - #.####...# - .....#..## - #...###### - .##.#....# - .###.##### - ###.##.##. - .###....#. - ..#.#..#.# - #...##.#.. - - Tile 1171: - ####...##. - #..##.#..# - ##.#..#.#. - .###.####. - ..###.#### - .##....##. - .#...####. - #.##.####. - ####..#... - .....##... - - Tile 1427: - ###.##.#.. - .#..#.##.. - .#.##.#..# - #.#.#.##.# - ....#...## - ...##..##. - ...#.##### - .#.####.#. - ..#..###.# - ..##.#..#. - - Tile 1489: - ##.#.#.... - ..##...#.. - .##..##... - ..#...#... - #####...#. - #..#.#.#.# - ...#.#.#.. - ##.#...##. - ..##.##.## - ###.##.#.. - - Tile 2473: - #....####. - #..#.##... - #.##..#... - ######.#.# - .#...#.#.# - .######### - .###.#..#. - ########.# - ##...##.#. - ..###.#.#. - - Tile 2971: - ..#.#....# - #...###... - #.#.###... - ##.##..#.. - .#####..## - .#..####.# - #..#.#..#. - ..####.### - ..#.#.###. - ...#.#.#.# - - Tile 2729: - ...#.#.#.# - ####.#.... - ..#.#..... - ....#..#.# - .##..##.#. - .#.####... - ####.#.#.. - ##.####... - ##..#.##.. - #.##...##. - - Tile 3079: - #.#.#####. - .#..###### - ..#....... - ######.... - ####.#..#. - .#...#.##. - #.#####.## - ..#.###... - ..#....... - ..#.###... - """.trimIndent() - )) { - println(part1()) - println(part2()) - } +fun main() = with(createDay()) { + println(part1()) + println(part2()) }