-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.ts
53 lines (45 loc) · 1.25 KB
/
day02.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
import { readFileSync } from "fs";
class Box {
h: number;
w: number;
l: number;
constructor(input: string) {
const values = input.split("x").map(Number);
this.h = values[0];
this.w = values[1];
this.l = values[2];
}
}
function requiredPaper(b: Box): number {
const side1 = b.l * b.w;
const side2 = b.w * b.h;
const side3 = b.h * b.l;
const extra = Math.min(side1, side2, side3);
return 2 * side1 + 2 * side2 + 2 * side3 + extra;
}
function volume(b: Box): number {
return b.h * b.w * b.l;
}
function requiredRibbon(b: Box): number {
/* Takes 2 shortest sides to calculate wrap length, then adds volume */
const sides = Object.values(b).sort((a, b) => a - b);
return 2 * sides[0] + 2 * sides[1] + volume(b);
}
function readInput(input: string): Box[] {
try {
const data = readFileSync(input, "utf8");
return data.split("\n").map((value) => {
return new Box(value);
});
} catch (err) {
console.log(err);
return [];
}
}
export function day02(input: string, verbose: boolean = false): void {
const boxes = readInput(input);
console.log("Part 1: " + boxes.map(requiredPaper).reduce((a, b) => a + b, 0));
console.log(
"Part 2: " + boxes.map(requiredRibbon).reduce((a, b) => a + b, 0)
);
}