|
| 1 | +package de.ronny_h.aoc.year2018.day12 |
| 2 | + |
| 3 | +import de.ronny_h.aoc.AdventOfCode |
| 4 | +import de.ronny_h.aoc.extensions.collections.split |
| 5 | + |
| 6 | +fun main() = SubterraneanSustainability().run(3798, 0) |
| 7 | + |
| 8 | +class SubterraneanSustainability : AdventOfCode<Int>(2018, 12) { |
| 9 | + override fun part1(input: List<String>): Int { |
| 10 | + val pots = GameOfPlants(input, 20) |
| 11 | + pots.simulateGenerations() |
| 12 | + return pots.sumOfPlantContainingPotNumbers() |
| 13 | + } |
| 14 | + |
| 15 | + override fun part2(input: List<String>): Int { |
| 16 | + return 0 |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class GameOfPlants(input: List<String>, val generations: Int) { |
| 21 | + private val plant = '#' |
| 22 | + private val noPlant = '.' |
| 23 | + private val initialState = input.first().substringAfter("initial state: ") |
| 24 | + |
| 25 | + // a rule has the form of: LLCRR => N |
| 26 | + private val plantProducingPatterns = input.split()[1] |
| 27 | + .map { it.split(" => ") } |
| 28 | + .filter { it[1] == "$plant" } |
| 29 | + .map { it.first() } |
| 30 | + .toSet() |
| 31 | + private val patternLength = 5 |
| 32 | + private val patternCenter = 2 |
| 33 | + |
| 34 | + // per generation the plants grow max 2 pots wider in each direction |
| 35 | + private val offset = generations * 2 |
| 36 | + private val padding = "$noPlant".repeat(offset) |
| 37 | + |
| 38 | + var currentGeneration = padding + initialState + padding |
| 39 | + private set |
| 40 | + |
| 41 | + fun nextGeneration(): String { |
| 42 | + val nextGen = currentGeneration.toMutableList() |
| 43 | + |
| 44 | + for (index in 0..nextGen.lastIndex - patternLength) { |
| 45 | + nextGen[index + patternCenter] = |
| 46 | + if (currentGeneration.substring(index, index + patternLength) in plantProducingPatterns) { |
| 47 | + plant |
| 48 | + } else { |
| 49 | + noPlant |
| 50 | + } |
| 51 | + } |
| 52 | + currentGeneration = nextGen.joinToString("") |
| 53 | + return currentGeneration |
| 54 | + } |
| 55 | + |
| 56 | + fun sumOfPlantContainingPotNumbers(): Int = |
| 57 | + currentGeneration.foldIndexed(0) { i, acc, pot -> |
| 58 | + if (pot == plant) acc + i - offset else acc |
| 59 | + } |
| 60 | + |
| 61 | + fun simulateGenerations() { |
| 62 | + println("generation 0:$currentGeneration") |
| 63 | + repeat(generations) { |
| 64 | + nextGeneration() |
| 65 | + println("generation ${"%2d".format(it + 1)}: $currentGeneration") |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments