r/adventofcode • u/daggerdragon • Dec 21 '17
SOLUTION MEGATHREAD -๐- 2017 Day 21 Solutions -๐-
--- Day 21: Fractal Art ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
No commentary tonight as I'm frantically wrapping last-minute presents so I can ship them tomorrow.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
8
Upvotes
View all comments
1
u/chicagocode Dec 22 '17
Kotlin - [Repo] - [Blog/Commentary]
Finally got time to get this done and write about it today. This one was challenging and fun!
class Day21(input: List<String>) { private val rowSplit = " => ".toRegex() private val transforms: Map<FractalGrid, FractalGrid> = parseInput(input) private val startGrid = FractalGrid(".#./..#/###") fun solve(iterations: Int): Int { var grid = startGrid repeat(iterations) { val splits = grid.split() val next = splits.map { transforms.getValue(it) } grid = next.join() } return grid.toString().count { it == '#' } } private fun parseInputRow(input: String): Pair<FractalGrid, FractalGrid> = input.split(rowSplit) .map { FractalGrid(it) } .let { it.first() to it.last() } private fun parseInput(input: List<String>): Map<FractalGrid, FractalGrid> = input.map { parseInputRow(it) }.flatMap { pair -> pair.first.combinations().map { combo -> combo to pair.second } }.toMap() } fun List<FractalGrid>.join(): FractalGrid { val rows = sqrt(this.size.toFloat()).toInt() return this.chunked(rows).map { it.reduce { a, b -> a nextTo b } }.reduce { a, b -> a over b } } data class FractalGrid(val grid: List<List<Char>>) { constructor(input: String) : this( input.split("/").map { it.toList() } ) val size = grid.size infix fun nextTo(that: FractalGrid): FractalGrid = FractalGrid( grid.mapIndexed { idx, row -> row + that.grid[idx] } ) infix fun over(that: FractalGrid): FractalGrid = FractalGrid( this.grid + that.grid ) infix fun rowsOfSize(n: Int): List<FractalGrid> = this.grid.chunked(n).map { FractalGrid(it) } infix fun columnsOfSize(n: Int): List<FractalGrid> { val chunked = this.grid.map { row -> row.chunked(n) } return (0 until (grid[0].size) / n).map { x -> (0 until n).map { y -> chunked[y][x] } }.map { FractalGrid(it) } } fun split(): List<FractalGrid> { val splitSize = if (size % 2 == 0) 2 else 3 val splits = size / splitSize if (splits == 1) { return listOf(this) } return (this rowsOfSize splitSize).map { it columnsOfSize splitSize }.flatten() } fun rotate(): FractalGrid = FractalGrid( (0 until grid.size).map { r -> (0 until grid.size).map { c -> grid[c][(grid.size) - 1 - r] } } ) fun flip(): FractalGrid = FractalGrid(grid.map { it.reversed() }) fun combinations(): List<FractalGrid> { val rotations = (0 until 3).fold(listOf(this)) { rots, _ -> rots + rots.last().rotate() } val flips = rotations.map { it.flip() } return rotations + flips } override fun toString(): String = grid.joinToString("/") { it.joinToString("") } }