-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.ts
43 lines (33 loc) · 999 Bytes
/
Day1.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
import * as fs from 'fs';
class Solution {
part1(numbers: string[]): number {
let result = 0;
for (let i in numbers) {
result += +numbers[i];
}
return result;
}
part2(numbers: string[]) : number {
let runId = 0;
let existingResults = new Set<number>();
let result = 0;
while(true) {
for (let i in numbers) {
result += +numbers[i];
if (existingResults.has(result)) {
console.log("runId", runId);
console.log("collection size", existingResults.size);
return result;
}
existingResults.add(result);
}
runId++;
}
}
}
var numbers = fs.readFileSync('./Day1.txt').toString().split("\n");
var solution = new Solution();
const part1 = solution.part1(numbers);
console.log(part1);
const part2 = solution.part2(numbers);
console.log(part2);