diff --git a/d14.lua b/d14.lua new file mode 100644 index 0000000..e5cbb11 --- /dev/null +++ b/d14.lua @@ -0,0 +1,52 @@ +recipes = {} + +for line in io.lines(arg[1]) do + input, output = line:match("(.-) *=> *(.+)") + quant, name = output:match("(%d+) +(.+)") + recipe = {quant=tonumber(quant), ingres={}} + for quant, ingre in input:gmatch("(%d+) +(%a+),? *") do + recipe.ingres[ingre] = quant + end + recipes[name] = recipe +end + +recipes.ORE = {quant=0, ingres={}} + +function get_ore(fuel) + local wanted = {FUEL=fuel} + while true do + for name, _ in pairs(wanted) do + if name ~= "ORE" then + recipe = recipes[name] + if wanted[name] > 0 then + amount = wanted[name] // recipe.quant + if wanted[name] % recipe.quant > 0 then + amount = amount + 1 + end + for ingre, ingre_quant in pairs(recipe.ingres) do + if not wanted[ingre] then + wanted[ingre] = 0 + end + wanted[ingre] = wanted[ingre] + ingre_quant * amount + end + wanted[name] = wanted[name] - recipe.quant * amount + end + end + end + + if wanted["ORE"] then + count = 0 + for x, y in pairs(wanted) do if 0 < y then count = count + 1 end end + if count == 1 then + return wanted["ORE"] + end + end + end +end + +-- Manual brute forcing is actually doable, funnily enough +ore = get_ore(tonumber(arg[2])) +print(ore) +if ore > 1000000000000 then + print("Ding!") +end diff --git a/d14_input.txt b/d14_input.txt new file mode 100644 index 0000000..44b8aa8 --- /dev/null +++ b/d14_input.txt @@ -0,0 +1,59 @@ +180 ORE => 9 DQFL +3 HGCR, 9 TKRT => 8 ZBLC +1 MZQLG, 12 RPLCK, 8 PDTP => 8 VCFX +3 ZBLC, 19 VFZX => 1 SJQL +1 CRPGK => 4 TPRT +7 HGCR, 4 TGCW, 1 VFZX => 9 JBPHS +8 GJHX => 4 NSDBV +1 VFTG => 2 QNWD +1 WDKW, 2 DWRH, 6 VNMV, 2 HFHL, 55 GJHX, 4 NSDBV, 15 KLJMS, 17 KZDJ => 1 FUEL +2 JHSJ, 15 JNWJ, 1 ZMFXQ => 4 GVRK +1 PJFBD => 3 MZQLG +1 SJQL, 11 LPVWN => 9 DLZS +3 PRMJ, 2 XNWV => 6 JHSJ +4 SJQL => 8 PJFBD +14 QNWD => 6 STHQ +5 CNLFV, 2 VFTG => 9 XNWV +17 LWNKB, 6 KBWF, 3 PLSCB => 8 KZDJ +6 LHWZQ, 5 LWNKB => 3 ZDWX +5 RPLCK, 2 LPVWN => 8 ZMFXQ +1 QNWD, 2 TKRT => 3 CRPGK +1 JBPHS, 1 XNWV => 6 TLRST +21 ZDWX, 3 FZDP, 4 CRPGK => 6 PDTP +1 JCVP => 1 WXDVT +2 CRPGK => 9 FGVL +4 DQFL, 2 VNMV => 1 HGCR +2 GVRK, 2 VCFX, 3 PJFBD, 1 PLSCB, 23 FZDP, 22 PCSM, 1 JLVQ => 6 HFHL +1 CRPGK, 5 PJFBD, 4 XTCP => 8 PLSCB +1 HTZW, 17 FGVL => 3 LHWZQ +2 KBWF => 4 DQKLC +2 LHWZQ => 2 PRMJ +2 DLZS, 2 VCFX, 15 PDTP, 14 ZDWX, 35 NBZC, 20 JVMF, 1 BGWMS => 3 DWRH +2 TKVCX, 6 RPLCK, 2 HTZW => 4 XTCP +8 CNLFV, 1 NRSD, 1 VFTG => 9 VFZX +1 TLRST => 4 WDKW +9 VFCZG => 7 GJHX +4 FZDP => 8 JLVQ +2 ZMFXQ, 2 STHQ => 6 QDZB +2 SJQL, 8 ZDWX, 6 LPRL, 6 WXDVT, 1 TPRT, 1 JNWJ => 8 KLJMS +6 JBPHS, 2 ZBLC => 6 HTZW +1 PDTP, 2 LHWZQ => 8 JNWJ +8 ZBLC => 7 TKVCX +2 WDKW, 31 QDZB => 4 PCSM +15 GJHX, 5 TKVCX => 7 FZDP +15 SJQL, 3 PRMJ => 4 JCVP +31 CNLFV => 1 TGCW +1 TLRST, 2 WDKW => 9 KBWF +102 ORE => 7 VNMV +103 ORE => 5 CNLFV +163 ORE => 2 VFTG +5 NRSD, 1 STHQ => 3 VFCZG +16 LPVWN, 13 KBWF => 2 BGWMS +5 BGWMS, 11 SJQL, 9 FZDP => 6 NBZC +175 ORE => 7 NRSD +5 HTZW => 4 LPVWN +4 PRMJ => 7 JVMF +6 PCSM, 8 DQKLC => 7 LPRL +2 CNLFV => 7 TKRT +3 FZDP => 3 LWNKB +1 HTZW => 4 RPLCK