aoc-2019-c

Advent of Code 2019 Solutions in C
git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

part1 (4201B)


      1--- Day 14: Space Stoichiometry ---
      2
      3As you approach the rings of Saturn, your ship's low fuel indicator turns on.  There isn't any fuel
      4here, but the rings have plenty of raw material.  Perhaps your ship's Inter-Stellar Refinery Union
      5brand nanofactory can turn these raw materials into fuel.
      6
      7You ask the nanofactory to produce a list of the reactions it can perform that are relevant to this
      8process (your puzzle input). Every reaction turns some quantities of specific input chemicals into
      9some quantity of an output chemical. Almost every chemical is produced by exactly one reaction; the
     10only exception, ORE, is the raw material input to the entire process and is not produced by a
     11reaction.
     12
     13You just need to know how much ORE you'll need to collect before you can produce one unit of
     14FUEL.
     15
     16Each reaction gives specific quantities for its inputs and output; reactions cannot be partially
     17run, so only whole integer multiples of these quantities can be used.  (It's okay to have leftover
     18chemicals when you're done, though.) For example, the reaction 1 A, 2 B, 3 C => 2 D means that
     19exactly 2 units of chemical D can be produced by consuming exactly 1 A, 2 B and 3 C.  You can run
     20the full reaction as many times as necessary; for example, you could produce 10 D by consuming 5 A,
     2110 B, and 15 C.
     22
     23Suppose your nanofactory produces the following list of reactions:
     24
     2510 ORE => 10 A
     261 ORE => 1 B
     277 A, 1 B => 1 C
     287 A, 1 C => 1 D
     297 A, 1 D => 1 E
     307 A, 1 E => 1 FUEL
     31
     32The first two reactions use only ORE as inputs; they indicate that you can produce as much of
     33chemical A as you want (in increments of 10 units, each 10 costing 10 ORE) and as much of chemical B
     34as you want (each costing 1 ORE).  To produce 1 FUEL, a total of 31 ORE is required: 1 ORE to
     35produce 1 B, then 30 more ORE to produce the 7 + 7 + 7 + 7 = 28 A (with 2 extra A wasted) required
     36in the reactions to convert the B into C, C into D, D into E, and finally E into FUEL. (30 A is
     37produced because its reaction requires that it is created in increments of 10.)
     38
     39Or, suppose you have the following list of reactions:
     40
     419 ORE => 2 A
     428 ORE => 3 B
     437 ORE => 5 C
     443 A, 4 B => 1 AB
     455 B, 7 C => 1 BC
     464 C, 1 A => 1 CA
     472 AB, 3 BC, 4 CA => 1 FUEL
     48
     49The above list of reactions requires 165 ORE to produce 1 FUEL:
     50
     51
     52 - Consume 45 ORE to produce 10 A.
     53
     54 - Consume 64 ORE to produce 24 B.
     55
     56 - Consume 56 ORE to produce 40 C.
     57
     58 - Consume 6 A, 8 B to produce 2 AB.
     59
     60 - Consume 15 B, 21 C to produce 3 BC.
     61
     62 - Consume 16 C, 4 A to produce 4 CA.
     63
     64 - Consume 2 AB, 3 BC, 4 CA to produce 1 FUEL.
     65
     66
     67Here are some larger examples:
     68
     69
     70 - 13312 ORE for 1 FUEL:
     71
     72157 ORE => 5 NZVS
     73165 ORE => 6 DCFZ
     7444 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL
     7512 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ
     76179 ORE => 7 PSHF
     77177 ORE => 5 HKGWZ
     787 DCFZ, 7 PSHF => 2 XJWVT
     79165 ORE => 2 GPVTF
     803 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT
     81
     82
     83 - 180697 ORE for 1 FUEL:
     84
     852 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG
     8617 NVRVD, 3 JNWZP => 8 VPVL
     8753 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL
     8822 VJHF, 37 MNCFX => 5 FWMGM
     89139 ORE => 4 NVRVD
     90144 ORE => 7 JNWZP
     915 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC
     925 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV
     93145 ORE => 6 MNCFX
     941 NVRVD => 8 CXFTF
     951 VJHF, 6 MNCFX => 4 RFSQX
     96176 ORE => 6 VJHF
     97
     98
     99 - 2210736 ORE for 1 FUEL:
    100
    101171 ORE => 8 CNZTR
    1027 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL
    103114 ORE => 4 BHXH
    10414 VRPVC => 6 BMBT
    1056 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL
    1066 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT
    10715 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW
    10813 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW
    1095 BMBT => 4 WPTQ
    110189 ORE => 9 KTJDG
    1111 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP
    11212 VRPVC, 27 CNZTR => 2 XDBXC
    11315 KTJDG, 12 BHXH => 5 XCVML
    1143 BHXH, 2 VRPVC => 7 MZWV
    115121 ORE => 7 VRPVC
    1167 XCVML => 6 RJRHP
    1175 BHXH, 4 VRPVC => 5 LTCX
    118
    119
    120
    121Given the list of reactions in your puzzle input, what is the minimum amount of ORE required to
    122produce exactly 1 FUEL?
    123
    124