Skip to content

Commit b155f6f

Browse files
committed
Day 14 1
1 parent 5cafbd6 commit b155f6f

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

day14.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import * as fs from "fs"
2+
3+
const input = fs.readFileSync("input-day14.txt", "utf8")
4+
const lines = input.trim().split("\n")
5+
const recipes = lines
6+
.map(line => {
7+
const [i, o] = line.split("=>")
8+
return [i.split(",").map(s => s.trim()).map(parse), parse(o.trim())] as const
9+
})
10+
.reduce((a, v) => {
11+
a.set(v[1][1], v)
12+
return a
13+
}, new Map<Element, Recipe>())
14+
15+
function parse(ingredient: string): Ingredient {
16+
const [c, n] = ingredient.split(" ")
17+
return [Number(c), n]
18+
}
19+
20+
function print(recipe: Recipe) {
21+
console.log(recipe[0].map(i => `${i[0]} ${i[1]}`).join(", "), " => ", recipe[1][0], recipe[1][1])
22+
}
23+
24+
type Quantity = number
25+
type Element = string
26+
type Ingredient = readonly [Quantity, Element]
27+
type Recipe = readonly [Ingredient[], Ingredient]
28+
29+
class Bag {
30+
private readonly bag = new Map<Element, Quantity>()
31+
get(e: Element) {
32+
return this.bag.get(e) ?? 0
33+
}
34+
add(e: Element, q: Quantity) {
35+
this.bag.set(e, Math.max(0, q + this.get(e)))
36+
}
37+
remove(e: Element, q: Quantity) {
38+
this.add(e, -q)
39+
}
40+
delete(e: Element) {
41+
this.bag.delete(e)
42+
}
43+
entries() {
44+
return this.bag.entries()
45+
}
46+
get size() {
47+
return this.bag.size
48+
}
49+
}
50+
const need = new Bag()
51+
need.add("FUEL", 1)
52+
53+
const spares = new Bag()
54+
55+
do {
56+
const [e, q] = Array.from(need.entries()).filter(([e,]) => e !== "ORE")[0]
57+
need.delete(e)
58+
59+
const needs = ingredients(recipes.get(e), q)
60+
needs.forEach(([q, e]) => {
61+
need.add(e, q)
62+
})
63+
} while (need.size > 1)
64+
65+
function ingredients(recipe: Recipe, quantity: Quantity): Ingredient[] {
66+
const e = recipe[1][1]
67+
68+
const spare = spares.get(e)
69+
quantity = Math.max(0, quantity - spare)
70+
spares.remove(e, quantity)
71+
if (quantity === 0) {
72+
return []
73+
}
74+
75+
const factor = Math.ceil(quantity / recipe[1][0])
76+
const producing = recipe[1][0] * factor
77+
spares.add(e, producing - quantity)
78+
return recipe[0].map(r => ([r[0] * factor, r[1]]))
79+
}
80+
81+
console.log(need)

input-day14.txt

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
13 WDSR, 16 FXQB => 6 BSTCB
2+
185 ORE => 9 BWSCM
3+
1 WDSR => 9 RLFSK
4+
5 LCGL, 7 BWSCM => 9 BSVW
5+
6 NLSL => 3 MJSQ
6+
1 JFGM, 7 BSVW, 7 XRLN => 6 WDSR
7+
3 WZLFV => 3 BZDPT
8+
5 DTHZH, 12 QNTH, 20 BSTCB => 4 BMXF
9+
18 JSJWJ, 6 JLMD, 6 TMTF, 3 XSNL, 3 BWSCM, 83 LQTJ, 29 KDGNL => 1 FUEL
10+
1 LWPD, 28 RTML, 16 FDPM, 8 JSJWJ, 2 TNMTC, 20 DTHZH => 9 JLMD
11+
1 SDVXW => 6 BPTV
12+
180 ORE => 7 JFGM
13+
13 RLFSK, 15 HRKD, 1 RFQWL => 5 QNTH
14+
1 RFQWL, 3 NZHFV, 18 XRLN => 9 HRKD
15+
2 NLSL, 2 JXVZ => 5 GTSJ
16+
19 SDVXW, 2 BSVW, 19 XRLN => 6 QMFV
17+
1 CSKP => 8 LQTJ
18+
4 ZSZBN => 5 RBRZT
19+
8 WZLFV, 3 QNWRZ, 1 DTHZH => 4 RTRN
20+
1 CGXBG, 1 PGXFJ => 3 TNMTC
21+
4 CGCSL => 7 RNFW
22+
9 CGCSL, 1 HGTL, 3 BHJXV => 8 RSVR
23+
5 NGJW => 8 HTDM
24+
21 FPBTN, 1 TNMTC, 2 RBRZT, 8 BDHJ, 28 WXQX, 9 RNFW, 6 RSVR => 1 XSNL
25+
2 WZLFV => 5 BHJXV
26+
10 BSTCB, 4 NLSL => 4 HQLHN
27+
1 JFGM => 7 SDVXW
28+
6 CSKP => 8 FXQB
29+
6 TNMTC, 4 BZDPT, 1 BPTV, 18 JSJWJ, 2 DTHZH, 1 LWPD, 8 RTML => 8 KDGNL
30+
6 XFGWZ => 7 CGCSL
31+
3 GTSJ => 4 LWPD
32+
1 WDSR, 1 QNWRZ => 5 XFGWZ
33+
11 CSKP, 10 SDVXW => 4 QNWRZ
34+
7 BSVW, 4 QMFV => 1 RFQWL
35+
12 QNTH, 10 HTDM, 3 WXQX => 3 FDPM
36+
2 HGTL => 7 PGXFJ
37+
14 SDVXW => 6 CSKP
38+
11 HQLHN, 1 GTSJ, 1 QNTH => 5 TMTF
39+
173 ORE => 9 LCGL
40+
4 WXQX => 9 BDHJ
41+
5 BZDPT => 7 NGJW
42+
1 GTSJ, 23 QNWRZ, 6 LQTJ => 7 JSJWJ
43+
23 NZHFV, 3 HQLHN => 6 DTHZH
44+
2 JFGM => 4 XRLN
45+
20 CGCSL => 9 WXQX
46+
2 BSTCB, 3 HRKD => 9 NLSL
47+
1 MJSQ, 1 BPTV => 8 CGXBG
48+
1 RTRN, 1 RSVR => 3 ZSZBN
49+
2 NZHFV, 1 BSTCB, 20 HRKD => 1 JXVZ
50+
2 BZDPT => 5 HGTL
51+
1 ZSZBN, 14 FDPM => 9 RTML
52+
3 BMXF => 8 FPBTN
53+
1 SDVXW, 8 XRLN => 9 NZHFV
54+
18 QNWRZ, 7 RLFSK => 1 WZLFV

0 commit comments

Comments
 (0)