Day 20 finally
This commit is contained in:
parent
c11231307f
commit
7a871771bb
@ -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<Char>
|
||||
@ -121,126 +123,96 @@ class Day20(@Groups val input: Input<List<List<String>>>) {
|
||||
return grid
|
||||
}
|
||||
|
||||
private fun removeGaps(grid: Grid<Tile?>) {
|
||||
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<ArrayList<Char>> = tile.data
|
||||
val newData = ArrayList<ArrayList<Char>>(oldData.size - 2)
|
||||
oldData.subList(1, oldData.size - 1).forEach { d ->
|
||||
val l = ArrayList<Char>().apply {
|
||||
addAll(d.subList(1, d.size - 1))
|
||||
}
|
||||
newData.add(l)
|
||||
}
|
||||
return Tile(newData)
|
||||
}
|
||||
|
||||
private fun gridToTile(grid: Grid<Tile?>): Tile {
|
||||
val newData = ArrayList<ArrayList<Char>>()
|
||||
for (y in 0 until grid.height) {
|
||||
val row = grid.row(y)
|
||||
for (yy in 0 until row[0]!!.height) {
|
||||
val combinedRow = ArrayList<Char>()
|
||||
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<List<Char>> {
|
||||
val newData = ArrayList<ArrayList<Char>>()
|
||||
for (y in startY until startY + height) {
|
||||
val row = ArrayList<Char>()
|
||||
newData.add(row)
|
||||
for (x in startX until startX + width) {
|
||||
row.add(this[x, y]!!)
|
||||
}
|
||||
}
|
||||
return newData
|
||||
}
|
||||
|
||||
private fun List<List<Char>>.isMonster(monster: List<List<Char>>): 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<List<Char>> = """
|
||||
| # |
|
||||
|# ## ## ###|
|
||||
| # # # # # # |
|
||||
""".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<Day20>(
|
||||
"""
|
||||
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<Day20>()) {
|
||||
println(part1())
|
||||
println(part2())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user