-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo.js
67 lines (57 loc) · 1.12 KB
/
two.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { readFile } from 'fs/promises';
const F = 'forward';
const D = 'down';
const U = 'up';
// 2a
// async function init() {
// const input = await readFile('two.txt', 'utf8');
// const data = input.split('\n');
// let h = 0,
// d = 0;
// data.forEach((s) => {
// let [dir, x] = s.split(' ');
// x = parseInt(x);
// console.log(dir, x);
// switch (dir) {
// case F:
// h += x;
// break;
// case D:
// d += x;
// break;
// case U:
// d -= x;
// break;
// }
// console.log({ h, d });
// });
// console.log(h * d);
// }
// 2b
async function init() {
const input = await readFile('two.txt', 'utf8');
const data = input.split('\n');
let h = 0,
d = 0,
aim = 0;
data.forEach((s) => {
let [dir, x] = s.split(' ');
x = parseInt(x);
console.log(dir, x);
switch (dir) {
case F:
h += x;
d += aim * x;
break;
case D:
aim += x;
break;
case U:
aim -= x;
break;
}
console.log({ h, d });
});
console.log(h * d);
}
init();