-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.js
71 lines (67 loc) · 1.75 KB
/
day14.js
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
function getReactions(input) {
return input
.split("\n")
.map(line => {
const [reagents, [result]] = line.split(" => ").map(side =>
side.split(", ").map(ingredient => {
[output, name] = ingredient.split(" ");
return {
output: parseInt(output, 10),
name
};
})
);
return {
reagents,
result
};
})
.reduce(
(acc, { reagents, result: { name, output } }) => ({
...acc,
[name]: {
reagents,
output
}
}),
{}
);
}
function getOreRequired(recipe, name, amount, storage = {}) {
if (name === 'ORE') return amount;
const reaction = recipe[name];
const { reagents, output } = reaction;
const storedAmount = storage[name] || 0;
const amountToMake = amount - storedAmount;
if (amountToMake <= 0) {
storage[name] = storedAmount - amount;
return 0;
}
const times = Math.ceil(amountToMake / output);
const excess = (times * output) - amountToMake;
storage[name] = excess;
return reagents.reduce((acc, reagent) => {
return acc + getOreRequired(recipe, reagent.name, reagent.output * times, storage)
} , 0);
}
function calculateOreRequired(input) {
const recipe = getReactions(input);
return getOreRequired(recipe, 'FUEL', 1);
}
function calculatePotentialFuel(input, ore) {
const recipe = getReactions(input);
return binarySearch(i => getOreRequired(recipe, "FUEL", i) > ore);
}
function binarySearch(predicate) {
let low = -1;
let high = 1e16;
while (1 + low < high) {
const mid = low + Math.floor((high - low) / 2);
if (predicate(mid)) {
high = mid;
} else {
low = mid;
}
}
return high - 1;
}