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