-
Notifications
You must be signed in to change notification settings - Fork 1
/
02.js
49 lines (47 loc) · 793 Bytes
/
02.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
40
41
42
43
44
45
46
47
48
49
'use strict';
/**
* @param {string} d
*/
export const part1 = async d => {
let h = 0;
let depth = 0;
d.split('\n').map(e=>e.split(' ')).map(e=>[e[0], parseInt(e[1], 10)])
.map(e=> {
switch(e[0]) {
case 'forward':
h += e[1];
break;
case 'down':
depth += e[1];
break;
case 'up':
depth -= e[1];
break;
}
});
return h*depth;
};
/**
* @param {string} d
*/
export const part2 = async d => {
let h = 0;
let depth = 0;
let aim = 0;
d.split('\n').map(e=>e.split(' ')).map(e=>[e[0], parseInt(e[1], 10)])
.map(e=> {
switch(e[0]) {
case 'forward':
h += e[1];
depth += aim * e[1];
break;
case 'down':
aim += e[1];
break;
case 'up':
aim -= e[1];
break;
}
});
return h * depth;
};