|
| 1 | +package de.ronny_h.aoc.year2017.day21 |
| 2 | + |
| 3 | +import de.ronny_h.aoc.AdventOfCode |
| 4 | +import de.ronny_h.aoc.extensions.asList |
| 5 | +import de.ronny_h.aoc.extensions.grids.Coordinates |
| 6 | +import de.ronny_h.aoc.extensions.grids.Grid |
| 7 | + |
| 8 | +fun main() = FractalArt().run(150, 2606275) |
| 9 | + |
| 10 | +class FractalArt : AdventOfCode<Int>(2017, 21) { |
| 11 | + override fun part1(input: List<String>): Int = applyTheRulesToTheGivenInitConfiguration(input, 5) |
| 12 | + override fun part2(input: List<String>): Int = applyTheRulesToTheGivenInitConfiguration(input, 18) |
| 13 | + |
| 14 | + fun applyTheRulesToTheGivenInitConfiguration(input: List<String>, iterations: Int): Int { |
| 15 | + val rules = input.parseEnhancementRules() |
| 16 | + val initConfiguration = """ |
| 17 | + .#. |
| 18 | + ..# |
| 19 | + ### |
| 20 | + """.asList() |
| 21 | + var grid = FractalArtGrid(initConfiguration) |
| 22 | + repeat(iterations) { |
| 23 | + grid = grid.applyRules(rules) |
| 24 | + } |
| 25 | + return grid.countPixelsThatAreOn() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +data class Rule(val pattern: List<String>, val onCount: Int, val replacement: List<String>) |
| 30 | +data class RuleSet(val even: List<Rule>, val odd: List<Rule>) |
| 31 | + |
| 32 | +private const val on = '#' |
| 33 | +private const val off = '.' |
| 34 | + |
| 35 | +fun List<String>.parseEnhancementRules(): RuleSet { |
| 36 | + val rules = map { input -> |
| 37 | + val (p, r) = input.split(" => ") |
| 38 | + Rule(pattern = p.split("/"), onCount = p.count { it == on }, replacement = r.split("/")) |
| 39 | + }.partition { it.pattern.size % 2 == 0 } |
| 40 | + return RuleSet(even = rules.first, odd = rules.second) |
| 41 | +} |
| 42 | + |
| 43 | +class FractalArtGrid(size: Int) : Grid<Char>(size, size, off) { |
| 44 | + |
| 45 | + constructor(initConfiguration: List<String>) : this(initConfiguration.size) { |
| 46 | + check(initConfiguration.size == initConfiguration[0].length) { "FractalArtGrid has to be square" } |
| 47 | + initGrid(initConfiguration) |
| 48 | + } |
| 49 | + |
| 50 | + override fun Char.toElementType(): Char = this |
| 51 | + |
| 52 | + fun countPixelsThatAreOn(): Int = forEachElement { _, _, e -> e }.filter { it == on }.count() |
| 53 | + |
| 54 | + fun applyRules(rules: RuleSet): FractalArtGrid { |
| 55 | + val squareSize = if (height % 2 == 0) 2 else 3 |
| 56 | + val numberOfSquares = height / squareSize |
| 57 | + val newGrid = FractalArtGrid(numberOfSquares * (squareSize + 1)) |
| 58 | + val rulesToApply = if (height % 2 == 0) rules.even else rules.odd |
| 59 | + forEachSquareOfSize(squareSize) { start, square -> |
| 60 | + var applied = false |
| 61 | + for (rule in rulesToApply) { |
| 62 | + if (rule.onCount != square.countPixelsThatAreOn()) { |
| 63 | + // filter out bad candidates early |
| 64 | + continue |
| 65 | + } |
| 66 | + if (squareSize == 2 && rule.onCount != 2 || rule.ruleMatches(square) || rule.ruleMatches(square.reversed())) { |
| 67 | + newGrid.applyRuleAt( |
| 68 | + rule, |
| 69 | + Coordinates(start.row + (start.row / squareSize), start.col + (start.col / squareSize)) |
| 70 | + ) |
| 71 | + applied = true |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + if (!applied) { |
| 76 | + throw RuntimeException("No rule to apply for square $square at $start.") |
| 77 | + } |
| 78 | + } |
| 79 | + return newGrid |
| 80 | + } |
| 81 | + |
| 82 | + private fun Rule.ruleMatches(square: List<List<Char>>): Boolean = |
| 83 | + // exact match |
| 84 | + square.allMatch { row, col, value -> pattern[row][col] == value } || |
| 85 | + |
| 86 | + // rotate 90° right |
| 87 | + // 0 1 |
| 88 | + // 0 [ 0 , 1 ] -> [ 2 , 0 ] = [ 1,0 , 0,0 ] = [ size-col,row , size-col,row ] |
| 89 | + // 1 [ 2 , 3 ] [ 3 , 1 ] [ 1,1 , 0,1 ] [ size-col,row , size-col,row ] |
| 90 | + square.allMatch { row, col, value -> pattern[square.lastIndex - col][row] == value } || |
| 91 | + |
| 92 | + // rotate 180° |
| 93 | + // 0 1 |
| 94 | + // 0 [ 0 , 1 ] -> [ 3 , 2 ] = [ 1,1 , 1,0 ] = [ size-row,size-col , size-row,size-col ] |
| 95 | + // 1 [ 2 , 3 ] [ 1 , 0 ] [ 0,1 , 0,0 ] [ size-row,size-col , size-row,size-col ] |
| 96 | + square.allMatch { row, col, value -> pattern[square.lastIndex - row][square.lastIndex - col] == value } || |
| 97 | + |
| 98 | + // rotate 90° left |
| 99 | + // 0 1 |
| 100 | + // 0 [ 0 , 1 ] -> [ 1 , 3 ] = [ 0,1 , 1,1 ] = [ col,size-row , col,size-row ] |
| 101 | + // 1 [ 2 , 3 ] [ 0 , 2 ] [ 0,0 , 1,0 ] [ col,size-row , col,size-row ] |
| 102 | + square.allMatch { row, col, value -> pattern[col][square.lastIndex - row] == value } |
| 103 | + |
| 104 | + private fun List<List<Char>>.allMatch(condition: (Int, Int, Char) -> Boolean): Boolean { |
| 105 | + forEachIndexed { row, rowList -> |
| 106 | + rowList.forEachIndexed { col, value -> |
| 107 | + if (!condition(row, col, value)) { |
| 108 | + return false |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return true |
| 113 | + } |
| 114 | + |
| 115 | + private fun applyRuleAt(rule: Rule, start: Coordinates) { |
| 116 | + rule.replacement.forEachIndexed { row, rowString -> |
| 117 | + rowString.forEachIndexed { col, value -> |
| 118 | + this[start.row + row, start.col + col] = value |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private fun forEachSquareOfSize(size: Int, block: (Coordinates, List<List<Char>>) -> Unit) { |
| 124 | + for (row in 0..<height step size) { |
| 125 | + for (col in 0..<height step size) { |
| 126 | + block(Coordinates(row, col), subGridAt(row, col, size)) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +private fun List<List<Char>>.countPixelsThatAreOn() = sumOf { row -> |
| 133 | + row.count { it == on } |
| 134 | +} |
0 commit comments