Skip to content

Commit d083540

Browse files
committed
day 14
1 parent d6a3ad0 commit d083540

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed

14/input.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
11 TLKRB, 9 DHRN, 1 XTLB => 5 TPXQR
2+
1 GVWZR, 1 RVXMK => 5 BSKC
3+
5 PQXHB, 1 WLMCM => 5 FDGBF
4+
7 XVPB, 10 RMRPX, 2 XVQDV => 4 JFSDT
5+
8 MCQK, 11 RMRPX => 7 STJNH
6+
6 JFRPS => 3 ZRKZ
7+
3 DNDTC, 1 SLPZ => 4 WQCSD
8+
5 BSKC => 1 ZVBX
9+
3 TQXFD, 24 FDGBF, 2 RMRPX => 6 MGHBF
10+
7 JFSDT => 5 VHJM
11+
5 XTLB, 3 DGDNX => 5 ZKDG
12+
12 KHLSP, 1 JFSDT, 23 PBJMZ => 5 RMHKH
13+
1 RMRPX => 9 DNDTC
14+
2 CKRXP, 1 HKWV, 1 RBCPD => 3 ZLVG
15+
11 SLPZ, 27 WQCSD, 2 STJNH => 4 RBCPD
16+
1 TQXFD, 1 RVXMK => 5 LNWD
17+
2 WCJM => 8 NMWV
18+
1 PQXHB, 5 WLMCM => 5 XTLB
19+
1 DHRN => 2 RVXMK
20+
147 ORE => 4 XPFRX
21+
4 KMHG, 11 LNWD => 4 MCQK
22+
4 MGHBF => 3 JCVCG
23+
2 ZRKZ, 1 JFRPS => 6 QTRJ
24+
4 RMHKH => 5 HJNP
25+
13 JPKGW, 20 STJNH, 32 JBPFQ, 9 GXSTP, 3 QRFRQ, 35 ZVBX, 4 HJNP, 54 DVRSL, 45 KZBW, 23 RVXMK => 1 FUEL
26+
12 VFTDK, 29 QDTNQ => 4 XVPB
27+
3 XVQDV => 8 MXWK
28+
11 RVXMK, 12 PZNJ, 1 QXNK, 2 ZKDG, 4 DHWR, 4 WCJM, 7 VHJM, 2 HKWV => 5 QRFRQ
29+
1 LNWD, 4 VHJM => 4 WCJM
30+
6 NMWV, 6 MXWK, 15 DVRSL, 17 ZLVG, 5 JFSDT, 10 SGBQP, 1 GWDVS => 5 JBPFQ
31+
3 TPXQR, 3 RVXMK => 5 XKSXK
32+
6 TPXQR, 2 JCVCG, 1 KMHG => 3 DHWR
33+
3 XTLB => 7 RMRPX
34+
26 HKWV, 2 STJNH, 1 MXWK, 3 CPXW, 2 CKRXP, 16 QTRJ, 1 WQCSD => 8 GXSTP
35+
5 TLKRB => 7 TQXFD
36+
12 VHJM => 9 JPKGW
37+
4 MGHBF, 17 XVPB => 3 KHLSP
38+
1 DVRSL => 3 GWDVS
39+
3 DHRN => 6 KZBW
40+
13 QXNK, 3 SGSBS, 23 BSKC => 6 DVRSL
41+
11 RBCPD => 5 CPXW
42+
177 ORE => 6 VFTDK
43+
1 XVQDV => 4 SLPZ
44+
17 SGSBS, 1 GVWZR => 5 JFRPS
45+
178 ORE => 1 WLMCM
46+
7 DVRSL, 2 MXWK => 4 DGDNX
47+
14 PZNJ, 5 JCVCG, 3 RBCPD => 8 SGBQP
48+
1 QDTNQ, 2 JFRPS, 1 ZRKZ => 8 PBJMZ
49+
4 LNWD => 1 QXNK
50+
30 VFTDK => 8 DHRN
51+
8 TPXQR, 6 XKSXK, 6 TLKRB => 6 KMHG
52+
1 TQXFD, 3 QTRJ => 6 QDTNQ
53+
123 ORE => 4 TLKRB
54+
1 BSKC => 5 XVQDV
55+
132 ORE => 4 PQXHB
56+
5 TLKRB => 7 SGSBS
57+
17 XPFRX => 6 GVWZR
58+
4 HKWV => 5 CKRXP
59+
1 RVXMK, 1 KHLSP => 8 PZNJ
60+
1 JFSDT => 4 HKWV

14/solve.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
adventofcode.activate(14);
2+
3+
adventofcode.day14_part1 = function(input) {
4+
adventofcode.day14_leftovers = {};
5+
6+
return adventofcode.day14_get_ingredients(input, 'FUEL', 1);
7+
};
8+
9+
adventofcode.day14_part2 = function(input) {
10+
adventofcode.day14_leftovers = {};
11+
12+
const cargo_hold = 1000000000000;
13+
14+
let fuel_produced = 0;
15+
16+
// first estimate how much is needed
17+
// however since we always have leftovers from previous runs
18+
// the actual amount of ore might be lower than that
19+
20+
let increment = Math.floor(cargo_hold / adventofcode.day14_get_ingredients(input, 'FUEL', 1));
21+
22+
// so now home in on the actual amount
23+
24+
while (increment > 0) {
25+
adventofcode.day14_leftovers = {};
26+
27+
let ore_needed = adventofcode.day14_get_ingredients(input, 'FUEL', fuel_produced + increment);
28+
29+
if (ore_needed < cargo_hold)
30+
fuel_produced += increment;
31+
else
32+
increment = Math.floor(increment/2);
33+
}
34+
35+
return fuel_produced;
36+
};
37+
38+
/**
39+
* get amount of ORE needed
40+
*
41+
* @param {string} input whole text input
42+
* @param {string} product_name e.g. FUEL or some other ingredient
43+
* @param {int} product_needed how much of product_name we need
44+
*
45+
* @returns {int} how much ORE is needed
46+
*/
47+
adventofcode.day14_get_ingredients = function(input, product_name, product_needed) {
48+
if (typeof adventofcode.day14_leftovers[product_name] === 'undefined')
49+
adventofcode.day14_leftovers[product_name] = 0;
50+
51+
// do we already have some product left from a previous reaction?
52+
// then we need less now
53+
54+
while (product_needed > 0 && adventofcode.day14_leftovers[product_name] > 0) {
55+
product_needed--;
56+
adventofcode.day14_leftovers[product_name]--;
57+
}
58+
59+
let ore_needed = 0;
60+
61+
if (product_needed > 0) {
62+
// parse ingredient list for current product
63+
let product_regexp = new RegExp('(.*) => (\\d+) ' + product_name, 'ig');
64+
65+
let matches = product_regexp.exec(input);
66+
67+
let product_produced = parseInt(matches[2]);
68+
69+
matches[1].split(', ').forEach(ingredient_raw => {
70+
[ingredient_amount, ingredient_name] = ingredient_raw.split(' ');
71+
72+
let factor = 1;
73+
74+
if (product_produced < product_needed)
75+
factor = Math.ceil(product_needed / product_produced);
76+
77+
// recursively check what's needed for any ingredient
78+
79+
adventofcode.day14_leftovers[product_name] = factor * product_produced - product_needed;
80+
81+
// stop recursion once we reach ORE as most basic ingredient
82+
83+
if (ingredient_name === 'ORE')
84+
ore_needed += factor * ingredient_amount;
85+
else {
86+
let needed = factor * ingredient_amount;
87+
88+
ore_needed += adventofcode.day14_get_ingredients(input, ingredient_name, needed);
89+
}
90+
});
91+
}
92+
93+
return ore_needed;
94+
};

14/task.txt

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
--- Day 14: Space Stoichiometry ---
2+
3+
As you approach the rings of Saturn, your ship's low fuel indicator turns on. There isn't any fuel here, but the rings have plenty of raw material. Perhaps your ship's Inter-Stellar Refinery Union brand nanofactory can turn these raw materials into fuel.
4+
5+
You ask the nanofactory to produce a list of the reactions it can perform that are relevant to this process (your puzzle input). Every reaction turns some quantities of specific input chemicals into some quantity of an output chemical. Almost every chemical is produced by exactly one reaction; the only exception, ORE, is the raw material input to the entire process and is not produced by a reaction.
6+
7+
You just need to know how much ORE you'll need to collect before you can produce one unit of FUEL.
8+
9+
Each reaction gives specific quantities for its inputs and output; reactions cannot be partially run, so only whole integer multiples of these quantities can be used. (It's okay to have leftover chemicals when you're done, though.) For example, the reaction 1 A, 2 B, 3 C => 2 D means that exactly 2 units of chemical D can be produced by consuming exactly 1 A, 2 B and 3 C. You can run the full reaction as many times as necessary; for example, you could produce 10 D by consuming 5 A, 10 B, and 15 C.
10+
11+
Suppose your nanofactory produces the following list of reactions:
12+
13+
10 ORE => 10 A
14+
1 ORE => 1 B
15+
7 A, 1 B => 1 C
16+
7 A, 1 C => 1 D
17+
7 A, 1 D => 1 E
18+
7 A, 1 E => 1 FUEL
19+
20+
The first two reactions use only ORE as inputs; they indicate that you can produce as much of chemical A as you want (in increments of 10 units, each 10 costing 10 ORE) and as much of chemical B as you want (each costing 1 ORE). To produce 1 FUEL, a total of 31 ORE is required: 1 ORE to produce 1 B, then 30 more ORE to produce the 7 + 7 + 7 + 7 = 28 A (with 2 extra A wasted) required in the reactions to convert the B into C, C into D, D into E, and finally E into FUEL. (30 A is produced because its reaction requires that it is created in increments of 10.)
21+
22+
Or, suppose you have the following list of reactions:
23+
24+
9 ORE => 2 A
25+
8 ORE => 3 B
26+
7 ORE => 5 C
27+
3 A, 4 B => 1 AB
28+
5 B, 7 C => 1 BC
29+
4 C, 1 A => 1 CA
30+
2 AB, 3 BC, 4 CA => 1 FUEL
31+
32+
The above list of reactions requires 165 ORE to produce 1 FUEL:
33+
34+
Consume 45 ORE to produce 10 A.
35+
Consume 64 ORE to produce 24 B.
36+
Consume 56 ORE to produce 40 C.
37+
Consume 6 A, 8 B to produce 2 AB.
38+
Consume 15 B, 21 C to produce 3 BC.
39+
Consume 16 C, 4 A to produce 4 CA.
40+
Consume 2 AB, 3 BC, 4 CA to produce 1 FUEL.
41+
42+
Here are some larger examples:
43+
44+
13312 ORE for 1 FUEL:
45+
46+
157 ORE => 5 NZVS
47+
165 ORE => 6 DCFZ
48+
44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL
49+
12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ
50+
179 ORE => 7 PSHF
51+
177 ORE => 5 HKGWZ
52+
7 DCFZ, 7 PSHF => 2 XJWVT
53+
165 ORE => 2 GPVTF
54+
3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT
55+
56+
180697 ORE for 1 FUEL:
57+
58+
2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG
59+
17 NVRVD, 3 JNWZP => 8 VPVL
60+
53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL
61+
22 VJHF, 37 MNCFX => 5 FWMGM
62+
139 ORE => 4 NVRVD
63+
144 ORE => 7 JNWZP
64+
5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC
65+
5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV
66+
145 ORE => 6 MNCFX
67+
1 NVRVD => 8 CXFTF
68+
1 VJHF, 6 MNCFX => 4 RFSQX
69+
176 ORE => 6 VJHF
70+
71+
2210736 ORE for 1 FUEL:
72+
73+
171 ORE => 8 CNZTR
74+
7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL
75+
114 ORE => 4 BHXH
76+
14 VRPVC => 6 BMBT
77+
6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL
78+
6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT
79+
15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW
80+
13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW
81+
5 BMBT => 4 WPTQ
82+
189 ORE => 9 KTJDG
83+
1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP
84+
12 VRPVC, 27 CNZTR => 2 XDBXC
85+
15 KTJDG, 12 BHXH => 5 XCVML
86+
3 BHXH, 2 VRPVC => 7 MZWV
87+
121 ORE => 7 VRPVC
88+
7 XCVML => 6 RJRHP
89+
5 BHXH, 4 VRPVC => 5 LTCX
90+
91+
Given the list of reactions in your puzzle input, what is the minimum amount of ORE required to produce exactly 1 FUEL?
92+
93+
--- Part Two ---
94+
95+
After collecting ORE for a while, you check your cargo hold: 1 trillion (1000000000000) units of ORE.
96+
97+
With that much ore, given the examples above:
98+
99+
The 13312 ORE-per-FUEL example could produce 82892753 FUEL.
100+
The 180697 ORE-per-FUEL example could produce 5586022 FUEL.
101+
The 2210736 ORE-per-FUEL example could produce 460664 FUEL.
102+
103+
Given 1 trillion ORE, what is the maximum amount of FUEL you can produce?

0 commit comments

Comments
 (0)