Skip to content

Commit 5f08e33

Browse files
committed
Day 14 part 1
1 parent 591ae92 commit 5f08e33

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
root = true
22
[*.py]
33
indent_style = space
4-
indent_size = 2
4+
indent_size = 4
55
end_of_line = lf
66
charset = utf-8
77
trim_trailing_whitespace = true

day14/d14.in

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
1 JNDQ, 11 PHNC => 7 LBJSB
2+
1 BFKR => 9 VGJG
3+
11 VLXQL => 5 KSLFD
4+
117 ORE => 6 DMSLX
5+
2 VGJG, 23 MHQGW => 6 HLVR
6+
2 QBJLJ => 6 DBJZ
7+
1 CZDM, 21 ZVPJT, 1 HLVR => 5 VHGQP
8+
1 RVKX => 1 FKMQD
9+
38 PHNC, 10 MHQGW => 5 GMVJX
10+
4 CZDM, 26 ZVHX => 7 QBGQB
11+
5 LBJSB, 2 DFZRS => 4 QBJLJ
12+
4 TJXZM, 11 DWXW, 14 VHGQP => 9 ZBHXN
13+
20 VHGQP => 8 SLXQ
14+
1 VQKM => 9 BDZBN
15+
115 ORE => 4 BFKR
16+
1 VGJG, 1 SCSXF => 5 PHNC
17+
10 NXZXH, 7 ZFXP, 7 ZCBM, 7 MHNLM, 1 BDKZM, 3 VQKM => 5 RMZS
18+
147 ORE => 2 WHRD
19+
16 CQMKW, 8 BNJK => 5 MHNLM
20+
1 HLVR => 5 TJQDC
21+
9 GSLTP, 15 PHNC => 5 SFZTF
22+
2 MJCD, 2 RVKX, 4 TJXZM => 1 MTJSD
23+
1 DBJZ, 3 SLXQ, 1 GMSB => 9 MGXS
24+
1 WZFK => 8 XCMX
25+
1 DFZRS => 9 GSLTP
26+
17 PWGXR => 2 DFZRS
27+
4 BFKR => 7 JNDQ
28+
2 VKHN, 1 SFZTF, 2 PWGXR => 4 JDBS
29+
2 ZVPJT, 1 PHNC => 6 VQKM
30+
18 GMSB, 2 MGXS, 5 CQMKW => 3 XGPXN
31+
4 JWCH => 3 BNJK
32+
1 BFKR => 2 PWGXR
33+
12 PHNC => 2 GMSB
34+
5 XGPXN, 3 VQKM, 4 QBJLJ => 9 GXJBW
35+
4 MHQGW => 9 DWXW
36+
1 GMSB, 1 BFKR => 5 DBKC
37+
1 VLXQL, 10 KSLFD, 3 JWCH, 7 DBKC, 1 MTJSD, 2 WZFK => 9 GMZB
38+
4 JDBS => 8 BRNWZ
39+
2 ZBHXN => 7 HMNRT
40+
4 LBJSB => 7 BCXGX
41+
4 MTJSD, 1 SFZTF => 8 ZCBM
42+
12 BRNWZ, 4 TJXZM, 1 ZBHXN => 7 WZFK
43+
10 HLVR, 5 LBJSB, 1 VKHN => 9 TJXZM
44+
10 BRNWZ, 1 MTJSD => 6 CMKW
45+
7 ZWHT => 7 VKHN
46+
5 CQMKW, 2 DBKC => 6 ZFXP
47+
1 CMKW, 5 JNDQ, 12 FKMQD, 72 BXZP, 28 GMVJX, 15 BDZBN, 8 GMZB, 8 RMZS, 9 QRPQB, 7 ZVHX => 1 FUEL
48+
10 MGXS => 9 JWCH
49+
1 BFKR => 8 SCSXF
50+
4 SFZTF, 13 CZDM => 3 RVKX
51+
1 JDBS, 1 SFZTF => 9 TSWV
52+
2 GMVJX, 1 PHNC => 1 CZDM
53+
6 JDBS => 1 BXZP
54+
9 TSWV, 5 TJXZM => 8 NXZXH
55+
1 HMNRT, 5 TSWV => 4 VLXQL
56+
16 WZFK, 11 XCMX, 1 GXJBW, 16 NXZXH, 1 QBGQB, 1 ZCBM, 10 JWCH => 3 QRPQB
57+
12 SCSXF, 6 VGJG => 4 ZVPJT
58+
10 JNDQ => 3 ZWHT
59+
1 DBJZ, 9 BCXGX => 2 CQMKW
60+
1 WHRD, 14 DMSLX => 8 MHQGW
61+
3 VKHN, 8 TJQDC => 4 MJCD
62+
1 QBJLJ => 4 ZVHX
63+
1 MHQGW, 4 ZVHX => 3 BDKZM

day14/d14.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import math
2+
3+
recipes = {}
4+
materials = {'ORE'}
5+
for line in open('d14.in').readlines():
6+
left, right = line.strip().split(' => ')
7+
output = right.split()
8+
recipes[output[1]] = {
9+
'quantity': int(output[0]),
10+
'ingredients': {item[1]: int(item[0]) for item in [item.split() for item in left.split(', ')]}
11+
}
12+
materials.add(output[1])
13+
14+
distance = {'ORE': 0}
15+
while len(distance) < len(materials):
16+
for material in materials:
17+
if material in distance:
18+
continue
19+
if not all([i in distance for i in recipes[material]['ingredients'].keys()]):
20+
continue
21+
distance[material] = max([distance[i] for i in recipes[material]['ingredients'].keys()]) + 1
22+
23+
needed = {'FUEL': 1}
24+
while len(needed) > 1 or 'ORE' not in needed:
25+
material = max(needed, key=lambda x: distance[x])
26+
quantity = needed[material]
27+
del needed[material]
28+
if material == 'ORE':
29+
needed[material] = quantity
30+
continue
31+
base_quantity, ingredients = recipes[material].values()
32+
for a, b in ingredients.items():
33+
if a not in needed:
34+
needed[a] = 0
35+
needed[a] += math.ceil(quantity/base_quantity)*b
36+
37+
print('Part 1:', needed['ORE'])

day14/problem.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
--- Day 14: Space Stoichiometry ---
2+
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.
3+
4+
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.
5+
6+
You just need to know how much ORE you'll need to collect before you can produce one unit of FUEL.
7+
8+
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.
9+
10+
Suppose your nanofactory produces the following list of reactions:
11+
12+
10 ORE => 10 A
13+
1 ORE => 1 B
14+
7 A, 1 B => 1 C
15+
7 A, 1 C => 1 D
16+
7 A, 1 D => 1 E
17+
7 A, 1 E => 1 FUEL
18+
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.)
19+
20+
Or, suppose you have the following list of reactions:
21+
22+
9 ORE => 2 A
23+
8 ORE => 3 B
24+
7 ORE => 5 C
25+
3 A, 4 B => 1 AB
26+
5 B, 7 C => 1 BC
27+
4 C, 1 A => 1 CA
28+
2 AB, 3 BC, 4 CA => 1 FUEL
29+
The above list of reactions requires 165 ORE to produce 1 FUEL:
30+
31+
Consume 45 ORE to produce 10 A.
32+
Consume 64 ORE to produce 24 B.
33+
Consume 56 ORE to produce 40 C.
34+
Consume 6 A, 8 B to produce 2 AB.
35+
Consume 15 B, 21 C to produce 3 BC.
36+
Consume 16 C, 4 A to produce 4 CA.
37+
Consume 2 AB, 3 BC, 4 CA to produce 1 FUEL.
38+
Here are some larger examples:
39+
40+
13312 ORE for 1 FUEL:
41+
42+
157 ORE => 5 NZVS
43+
165 ORE => 6 DCFZ
44+
44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL
45+
12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ
46+
179 ORE => 7 PSHF
47+
177 ORE => 5 HKGWZ
48+
7 DCFZ, 7 PSHF => 2 XJWVT
49+
165 ORE => 2 GPVTF
50+
3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT
51+
180697 ORE for 1 FUEL:
52+
53+
2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG
54+
17 NVRVD, 3 JNWZP => 8 VPVL
55+
53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL
56+
22 VJHF, 37 MNCFX => 5 FWMGM
57+
139 ORE => 4 NVRVD
58+
144 ORE => 7 JNWZP
59+
5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC
60+
5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV
61+
145 ORE => 6 MNCFX
62+
1 NVRVD => 8 CXFTF
63+
1 VJHF, 6 MNCFX => 4 RFSQX
64+
176 ORE => 6 VJHF
65+
2210736 ORE for 1 FUEL:
66+
67+
171 ORE => 8 CNZTR
68+
7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL
69+
114 ORE => 4 BHXH
70+
14 VRPVC => 6 BMBT
71+
6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL
72+
6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT
73+
15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW
74+
13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW
75+
5 BMBT => 4 WPTQ
76+
189 ORE => 9 KTJDG
77+
1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP
78+
12 VRPVC, 27 CNZTR => 2 XDBXC
79+
15 KTJDG, 12 BHXH => 5 XCVML
80+
3 BHXH, 2 VRPVC => 7 MZWV
81+
121 ORE => 7 VRPVC
82+
7 XCVML => 6 RJRHP
83+
5 BHXH, 4 VRPVC => 5 LTCX
84+
Given the list of reactions in your puzzle input, what is the minimum amount of ORE required to produce exactly 1 FUEL?

0 commit comments

Comments
 (0)