-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (49 loc) · 1.76 KB
/
index.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
const _ = require("lodash");
const fs = require("fs");
const LegoApi = require("./LegoApi");
const LegoFactory = require("./LegoPlate");
const Map = require("./Map");
main();
async function main() {
var threshold = process.argv[process.execArgv.length + 2] || 112;
try {
let greenLegos;
try {
const content = fs.readFileSync(`greenLegos.json`);
greenLegos = JSON.parse(content);
console.log(`Got green plates info from file.`);
} catch (error) {
console.log(`Getting green plates info from Internet...`);
const legoApi = new LegoApi();
greenLegos = await legoApi.getProductWithColor(LegoApi.DARK_GREEN);
fs.writeFileSync(`greenLegos.json`, JSON.stringify(greenLegos, null, "\t"));
}
const greenPlates = _(greenLegos).map(LegoFactory).compact().orderBy(['studs', 'pricePerStud'], ['desc' ,'asc']).value();
const map = new Map(threshold);
const start = Date.now();
_.each(greenPlates, (greenPlate) => {
_(map.map).each((line, y) => (
_(line).each((stud, x) => {
if (_(greenPlate.representations).some((representation) => (
map.tryPlacing(representation, x, y)
))) {
greenPlate.quantity += 1;
}
})
));
console.log();
console.log(map.toString());
});
console.log(`\nTook ${(Date.now() - start) / 1000} s\n`);
const {quantity, cost} = _.transform(greenPlates, (total, {name, price, quantity}) => {
const cost = quantity * price;
total.quantity += quantity;
total.cost += cost;
console.log(`${(quantity + " × " + name).padEnd(23)} @ ${price.toFixed(2).padStart(6)} = ${cost.toFixed(2).padStart(6)}`);
}, {quantity: 0, cost: 0});
console.log("".padStart(43, "_"));
console.log(`${(quantity + " ×").padEnd(23)}${cost.toFixed(2).padStart(18)} $`);
} catch (e) {
console.error(e);
}
}