-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.2.js
39 lines (32 loc) · 943 Bytes
/
1.2.js
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
// Advent of Code: 2015, Day 1
/*
--- Part Two ---
Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
For example:
) causes him to enter the basement at character position 1.
()()) causes him to enter the basement at character position 5.
What is the position of the character that causes Santa to first enter the basement?
*/
import { readFile } from 'fs';
import { exit } from 'process';
readFile(__dirname + '/inputs/1.2.txt', {
encoding: 'utf8'
}, (err, data) => {
if (err) throw err;
let floor = 0;
data.split('').forEach((char, idx) => {
switch (char) {
case '(':
floor++;
break;
case ')':
floor--;
if (floor === -1) {
console.log('Basement entered at:', idx + 1);
exit();
}
break;
};
}
);
});