-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (33 loc) · 1.11 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
const readFile = require("../../utils/readFile");
const input = readFile("2015/day02/input.in").trim();
const calculateWrappingPaper = (input) => {
const lines = input.split("\n");
let totalPaper = 0
for (const line of lines) {
const [l, w, h] = line.split("x").map(Number);
const lw = l * w;
const wh = w * h;
const hl = h * l;
const surfaceArea = 2 * (lw + wh + hl);
const smallestSide = Math.min(lw, wh, hl);
totalPaper += surfaceArea + smallestSide;
}
return totalPaper;
}
const calculateRibbon = (input) => {
const lines = input.split("\n");
let totalRibbon = 0;
for (const line of lines) {
const [l, w, h] = line.split("x").map(Number);
const smallestSides = 2 * (l + w + h - Math.max(l, w, h));
const bow = l * w * h
totalRibbon += smallestSides + bow;
}
return totalRibbon;
}
const mode = process.argv[2] || "paper"; // Pass "ribbon" as an argument to switch
if (mode === "ribbon") {
console.log("Total ribbon needed:", calculateRibbon(input));
} else {
console.log("Total wrapping paper needed:", calculateWrappingPaper(input));
}