-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.ts
39 lines (30 loc) · 1.1 KB
/
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
import { fetchInput } from "./common_ts/inputAccess.ts";
const input = await fetchInput(1);
const numbers = input.split("\n").filter(x => x.length > 0).map((x) => parseInt(x));
const first = () => {
const numberSet = new Set<number>();
for(const num of numbers) {
if (numberSet.has(2020 - num)) {
console.log(`Found pair: {${num}, ${2020 - num}}. Multiplied: ${num * (2020-num)}`);
}
numberSet.add(num);
}
}
const second = () => {
const numberSet = new Set<number>();
for(const [firstIndex, firstNumber] of numbers.entries()) {
for(const [secondIndex, secondNumber] of numbers.entries()) {
if(firstIndex !== secondIndex){
const thirdNumberCandidate = 2020 - firstNumber - secondNumber;
if(thirdNumberCandidate > 0 && numberSet.has(thirdNumberCandidate)){
const multiplied = firstNumber * secondNumber * thirdNumberCandidate
console.log(`Found triple: {${firstNumber}, ${secondNumber}, ${thirdNumberCandidate}}. Multiplied: ${multiplied}`);
return;
}
numberSet.add(secondNumber);
}
}
}
}
first();
second();