-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd08.ts
More file actions
34 lines (26 loc) · 775 Bytes
/
d08.ts
File metadata and controls
34 lines (26 loc) · 775 Bytes
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
import { Day, RegisterDay } from "../Day.ts";
@RegisterDay(2015, 8)
export class Day08 extends Day {
override partOne(): string {
const data = this.readInput().split("\n");
const result = calculate(data);
return result.toString();
}
override partTwo(): string {
const data = this.readInput().split("\n").map((item) => encode(item));
const result = calculate(data);
return result.toString();
}
}
function encode(str: string): string {
return '"' + str.replace(/\\/g, "\\\\").replace(/\"/g, '\\"') + '"';
}
function calculate(data: string[]): number {
let codeChars = 0;
let memoryChars = 0;
data.forEach((item) => {
codeChars += item.length;
memoryChars += eval(item).length;
});
return (codeChars - memoryChars);
}