-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
45 lines (34 loc) · 869 Bytes
/
index.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
type Parenthesis = '(' | ')';
const OPEN_PARENTHESIS = '(' as const;
type OpenParenthesis = typeof OPEN_PARENTHESIS;
function isOpenParenthesis(parenthesis: string): parenthesis is OpenParenthesis {
return parenthesis === OPEN_PARENTHESIS;
}
function part1(input: string): number {
let floor = 0;
for (let i = 0; i < input.length; i += 1) {
const parenthesis = input[i] as Parenthesis;
if (isOpenParenthesis(parenthesis)) {
floor += 1;
} else {
floor -= 1;
}
}
return floor;
}
function part2(input: string): number {
let floor = 0;
for (let i = 0; i < input.length; i += 1) {
const parenthesis = input[i] as Parenthesis;
if (isOpenParenthesis(parenthesis)) {
floor += 1;
} else {
floor -= 1;
}
if (floor === -1) {
return i + 1;
}
}
return -1;
}
export { part1, part2 };