-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.elwaxoro.advent.y2019 | ||
|
||
import org.elwaxoro.advent.PuzzleDayTester | ||
import kotlin.math.ceil | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
/** | ||
* Day 14: Space Stoichiometry | ||
*/ | ||
class Dec14 : PuzzleDayTester(14, 2019) { | ||
|
||
private val stock = mutableMapOf<String, Long>() | ||
private val recipes = loader() | ||
private val trillion = 1000000000000 | ||
|
||
override fun part1(): Any = synthesize("FUEL", 1) == 654909L | ||
|
||
/** | ||
* Simple binary search, keep trying to synthesize until we're as close as we get | ||
*/ | ||
override fun part2(): Any { | ||
var low = 0L | ||
var high = trillion | ||
while (low <= high) { | ||
stock.clear() | ||
val mid = (low + high) / 2 | ||
val created = synthesize("FUEL", mid) | ||
if (created > trillion) { | ||
high = mid - 1 | ||
} else if (created < trillion) { | ||
low = mid + 1 | ||
} else { | ||
return mid == 2876992L | ||
} | ||
} | ||
return low - 1 == 2876992L | ||
} | ||
|
||
private fun synthesize(type: String, amount: Long): Long = | ||
if (type == "ORE") { | ||
// base case: getting ORE costs 1 per | ||
amount | ||
} else { | ||
val available = takeStock(type, amount) | ||
if (available == amount) { | ||
// we have everything on hand already, zero additional cost | ||
0 | ||
} else { | ||
val need = amount - available | ||
val (makes, costs) = recipes.getValue(type) | ||
val repeat = ceil(need / makes.toDouble()).toInt() | ||
val created = makes * repeat | ||
// save the excess from creation | ||
addStock(type, max(0, created - need)) | ||
// ok now actually make all the components | ||
costs.sumOf { (makeType, makeAmount) -> synthesize(makeType, makeAmount * repeat) } | ||
} | ||
} | ||
|
||
private fun addStock(type: String, count: Long) { | ||
stock[type] = stock.getOrDefault(type, 0) + count | ||
} | ||
|
||
private fun takeStock(type: String, count: Long): Long { | ||
val available = stock.getOrDefault(type, 0) | ||
stock[type] = max(0, available - count) | ||
return min(count, available) | ||
} | ||
|
||
private fun loader() = load().associate { line -> | ||
val (inputRaw, outputRaw) = line.split(" => ") | ||
val inputs = inputRaw.split(",").map { it.trim().split(" ").let { it[1] to it[0].toLong() } } | ||
val (outAmt, outType) = outputRaw.split(" ") | ||
outType to (outAmt.toLong() to inputs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
1 HKCVW, 2 DFCT => 5 ZJZRN | ||
8 TCPN, 7 XHTJF, 3 DFCT => 8 ZKCXK | ||
1 ZJZRN, 4 NZVL, 1 NJFXK, 7 RHJCQ, 32 MCQS, 1 XFNPT => 5 ZWQX | ||
10 DRWB, 16 JBHKV => 6 TCPN | ||
3 MBFK => 7 DRWB | ||
9 RHJCQ => 6 MBMKZ | ||
1 BVFPF => 2 KRTGD | ||
1 QNXC, 7 BKNQT, 1 XFNPT => 4 VNFJQ | ||
2 TCPN, 1 WFSV => 2 TGJP | ||
35 DFCT => 2 RHJCQ | ||
1 SKBV, 7 CTRH => 8 QGDSV | ||
8 VSRMJ, 1 BVFPF => 4 CTRH | ||
1 WMCD => 3 FPZLF | ||
13 CVJQG, 8 DXBZJ => 9 QBDQ | ||
1 XSRWM => 5 GDJGV | ||
132 ORE => 3 MBFK | ||
2 BQGP => 9 LZKJZ | ||
5 GZLHP => 7 WFSV | ||
2 RXSZS, 10 MBFK, 1 BPNVK => 2 GZLHP | ||
13 BZFH => 8 XSRWM | ||
3 QLSVN => 3 SKBV | ||
8 QBDQ => 4 VSRMJ | ||
1 RXSZS => 9 CVJQG | ||
3 MBFK => 3 BVFPF | ||
7 GZLHP, 4 MBFK, 5 CVJQG => 8 XHTJF | ||
1 GZLHP => 2 DFCT | ||
4 SZDWB, 4 RHJCQ, 1 WMCD => 3 RGZDK | ||
2 BRXLV => 8 DXBZJ | ||
192 ORE => 7 RXSZS | ||
1 PRMR, 6 DFCT => 5 SZDWB | ||
104 ORE => 9 BPNVK | ||
6 VLJWQ, 8 ZKCXK, 6 BKNQT, 26 JRXQ, 7 FPZLF, 6 HKCVW, 18 KRTGD => 4 RBFX | ||
7 XFNPT, 1 GDJGV => 2 HJDB | ||
15 SKBV, 8 DRWB, 12 RXSZS => 3 GHQPH | ||
1 BZFH => 5 GCBR | ||
1 TGJP, 6 SKBV => 1 BZFH | ||
4 KRTGD, 1 ZJHKP, 1 LZKJZ, 1 VNFJQ, 6 QBDQ, 1 PRMR, 1 NJFXK, 1 HJDB => 8 TFQH | ||
10 BVFPF, 1 RGZDK => 8 QNXC | ||
1 XHTJF => 5 JRXQ | ||
3 XKTMK, 4 QGDSV => 3 ZJHKP | ||
2 BZFH => 7 PRMR | ||
1 BPNVK, 1 RXSZS => 5 JBHKV | ||
10 XHTJF => 9 BKNQT | ||
1 JBHKV, 2 XHTJF => 8 QLSVN | ||
24 VNFJQ, 42 TFQH, 39 RBFX, 1 ZWQX, 7 VBHVQ, 26 DRWB, 21 NJFXK => 1 FUEL | ||
26 WBKQ, 14 XHTJF => 5 BQGP | ||
5 WBKQ, 7 MBMKZ => 3 LQGC | ||
6 LQGC => 5 NZVL | ||
13 KRTGD, 5 GHQPH => 9 VLJWQ | ||
117 ORE => 4 BRXLV | ||
3 XKTMK, 1 PRMR => 2 MCQS | ||
3 DRWB, 7 BVFPF, 4 TCPN => 7 NJFXK | ||
10 VHFCR, 13 JZQJ => 5 XKTMK | ||
17 CVJQG, 4 GCBR => 9 HKCVW | ||
22 DFCT, 17 TGJP => 2 WBKQ | ||
2 JZQJ, 12 XFNPT, 1 BQGP => 2 VBHVQ | ||
12 HKCVW => 1 JZQJ | ||
1 XSRWM => 3 WMCD | ||
12 BZFH, 14 SKBV, 1 CTRH => 4 XFNPT | ||
7 ZKCXK => 6 VHFCR |