Day 14, that wasn't easy to get correct

This commit is contained in:
2019-12-15 16:51:41 +01:00
parent 190dd4bc9b
commit fd597fc181
7 changed files with 255 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
package com.basdado.adventofcode.day14
import com.basdado.adventofcode.day14.Day14.Recipe.Companion.parse
import com.basdado.adventofcode.lines
import java.util.stream.Collectors
import kotlin.math.ceil
import kotlin.math.min
const val DAY14_INPUT_PATH = "/day/14/input.txt"
fun main() {
Day14.puzzle1()
Day14.puzzle2()
}
object Day14 {
fun puzzle1() {
val recipes = lines(DAY14_INPUT_PATH)
.map { parse(it) }
.collect(Collectors.toMap<Recipe, String, Recipe>({ it.output.first }, {it}))
println(calculateOreRequired(recipes))
}
fun puzzle2() {
val recipes = lines(DAY14_INPUT_PATH)
.map { parse(it) }
.collect(Collectors.toMap<Recipe, String, Recipe>({ it.output.first }, {it}))
var oreRequired = 0L
var stepSize = 1
val maxOre = 1_000_000_000_000L
var min = 1L
var max = 1_000_000_000_000L
while(min != max - 1) {
val guess = min + ((max - min) / 2)
if (calculateOreRequired(recipes, guess) > maxOre) {
max = guess
} else {
min = guess
}
// println("Result between $min and $max")
}
println(min)
}
private fun calculateOreRequired(recipes: MutableMap<String, Recipe>, fuelAmount: Long = 1L): Long {
var res = recipes["FUEL"]!!
if (fuelAmount > 1) {
res = Recipe(res.input.mapValues { it.value * fuelAmount }, Pair(res.output.first, res.output.second * fuelAmount))
// println("Required root recipe: $res")
}
// keep track of the remaining resources per type due to other reactions
val remaining = mutableMapOf<String, Long>()
val ingredientsRequiredCache = mutableMapOf<String, Set<String>>(Pair("ORE", emptySet()))
// Tactic: we keep simplifying the recipe until we are left with a recipe from ORE to FUEL
while (res.input.any { it.key != "ORE" }) {
// println(res)
// println("remaining: $remaining")
// We replace the ingredient that is not required by any other ingredients in the current recipe
val ingredientToReplace = res.input.keys.first {
res.input.keys.filter { o -> o != it }
.none { o -> calculateIngredientsRequired(o, recipes, ingredientsRequiredCache).contains(it) }
}
// println("Replacing $ingredientToReplace")
val inputRequiredAmount =
res.input[ingredientToReplace] ?: error("Wait? The input $ingredientToReplace can't be found?")
val replacementRecipe = recipes[ingredientToReplace]!!
val replacementRecipeTimes =
ceil(inputRequiredAmount.toDouble() / replacementRecipe.output.second.toDouble()).toLong()
// If the recipe will produce more than we need, at it to the remaining
remaining[replacementRecipe.output.first] = (remaining[replacementRecipe.output.first] ?: 0L) + (replacementRecipeTimes * replacementRecipe.output.second) - inputRequiredAmount
val newInput = res.input.toMutableMap()
newInput.remove(ingredientToReplace)
replacementRecipe.input.entries.forEach { replacementIngredient ->
// Be greedy here: if the "remaining" for the replacement ingredient is enough to produce the recipe, we use it
// Shit...greedy appears to not be optimal for larger cases...
val replacementIngredientRemaining = remaining[replacementIngredient.key] ?: 0
val useRemainingTimes =
min(replacementRecipeTimes, replacementIngredientRemaining / replacementIngredient.value)
if (useRemainingTimes > 0) {
remaining[replacementIngredient.key] =
remaining[replacementIngredient.key]!! - (useRemainingTimes * replacementIngredient.value)
}
newInput[replacementIngredient.key] = (newInput[replacementIngredient.key]
?: 0) + ((replacementRecipeTimes - useRemainingTimes) * replacementIngredient.value)
}
res = Recipe(newInput, res.output)
}
return res.input["ORE"] ?: error("Errore (hahah ore!)")
}
private fun calculateIngredientsRequired(ingredient: String, recipes: Map<String, Recipe>, cache: MutableMap<String, Set<String>>): Set<String> {
if (cache.containsKey(ingredient)) return cache[ingredient]!!
val res = mutableSetOf<String>()
val requiredIngredients = recipes[ingredient]?.input ?: return emptySet()
res.addAll(requiredIngredients.keys)
res.addAll(requiredIngredients.keys.flatMap { calculateIngredientsRequired(it, recipes, cache) })
cache[ingredient] = res
return res
}
data class Recipe(val input: Map<String, Long>, val output: Pair<String, Long>) {
companion object {
private val ingredientParser = Regex("^(\\d+)\\s+([A-Z]+)$")
fun parse(str: String): Recipe {
val split = str.split("=>").map { it.trim() }
val input = split[0].split(",")
.map { parseIngredient(it) }
.toMap()
val outputMatch = ingredientParser.matchEntire(split[1].trim())!!
val output = parseIngredient(split[1])
return Recipe(input, output)
}
private fun parseIngredient(str: String): Pair<String, Long> {
val match = ingredientParser.matchEntire(str.trim())!!
return Pair(match.groups[2]!!.value, match.groups[1]!!.value.toLong())
}
}
}
}

View File

@@ -0,0 +1,6 @@
10 ORE => 10 A
1 ORE => 1 B
7 A, 1 B => 1 C
7 A, 1 C => 1 D
7 A, 1 D => 1 E
7 A, 1 E => 1 FUEL

View File

@@ -0,0 +1,7 @@
9 ORE => 2 A
8 ORE => 3 B
7 ORE => 5 C
3 A, 4 B => 1 AB
5 B, 7 C => 1 BC
4 C, 1 A => 1 CA
2 AB, 3 BC, 4 CA => 1 FUEL

View File

@@ -0,0 +1,9 @@
157 ORE => 5 NZVS
165 ORE => 6 DCFZ
44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL
12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ
179 ORE => 7 PSHF
177 ORE => 5 HKGWZ
7 DCFZ, 7 PSHF => 2 XJWVT
165 ORE => 2 GPVTF
3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT

View File

@@ -0,0 +1,12 @@
2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG
17 NVRVD, 3 JNWZP => 8 VPVL
53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL
22 VJHF, 37 MNCFX => 5 FWMGM
139 ORE => 4 NVRVD
144 ORE => 7 JNWZP
5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC
5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV
145 ORE => 6 MNCFX
1 NVRVD => 8 CXFTF
1 VJHF, 6 MNCFX => 4 RFSQX
176 ORE => 6 VJHF

View File

@@ -0,0 +1,17 @@
171 ORE => 8 CNZTR
7 ZLQW, 3 BMBT, 9 XCVML, 26 XMN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