-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.ts
52 lines (46 loc) · 1014 Bytes
/
day01.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
import { readFileSync } from "fs";
function readInput(input: string): string {
try {
const data = readFileSync(input, "utf8");
return data;
} catch (err) {
console.log(err);
return "";
}
}
function part1(directions: string): number {
let floor = 0;
for (let i = 0; i < directions.length; i++) {
switch (directions[i]) {
case "(":
floor += 1;
break;
case ")":
floor -= 1;
break;
}
}
return floor;
}
function part2(directions: string): number {
let floor = 0;
for (let i = 0; i < directions.length; i++) {
switch (directions[i]) {
case "(":
floor += 1;
break;
case ")":
floor -= 1;
break;
}
if (floor < 0) {
return i + 1;
}
}
return directions.length;
}
export function day01(input: string, verbose: boolean = false) {
const directions = readInput(input);
console.log("Part 1: " + part1(directions));
console.log("Part 2: " + part2(directions));
}