-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd10.ts
More file actions
48 lines (38 loc) · 1022 Bytes
/
d10.ts
File metadata and controls
48 lines (38 loc) · 1022 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Day, RegisterDay } from "../Day.ts";
@RegisterDay(2015, 10)
export class Day10 extends Day {
override partOne(): string {
const input = this.readInput().split("");
const result = runGame(input, 40);
return result.length.toString();
}
override partTwo(): string {
const input = this.readInput().split("");
const result = runGame(input, 50);
return result.length.toString();
}
}
function runGame(chars: string[], times: number): string[] {
let latestChars = chars;
for (let n = 0; n < times; n++) {
const newChars = [];
for (let i = 0; i < latestChars.length;) {
const value = latestChars[i];
if (i == latestChars.length - 1) {
newChars.push("1");
newChars.push(value);
i++;
continue;
}
let j = 1;
while (latestChars[i + j] == value) {
j += 1;
}
newChars.push(j.toString());
newChars.push(value);
i += j;
}
latestChars = newChars;
}
return latestChars;
}