-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.js
99 lines (90 loc) · 2.36 KB
/
day02.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { read, readEx } from './util.ts';
import './util/prototype-shenanigans.js';
await readEx();
await read();
/** @type string **/
let data;
// 00:09:29
const partOne = () => {
const lines = data.lines().map(l => l.split(' ').nums());
let count = 0;
for (const l of lines) {
let prev = l[0];
let valid = true;
let dir = 0;
for (const n of l.slice(1)) {
let delta = n - prev;
const new_dir = delta < 0 ? -1 : 1
if (dir === 0) {
dir = new_dir;
} else if (new_dir !== dir) {
valid = false;
break;
}
if (dir === 1) {
delta *= -1;
}
if (delta !== -1 && delta !== -2 && delta !== -3) {
valid = false;
break;
}
prev = n
}
if (valid) {
count += 1;
}
}
console.log(count);
};
// 00:17:24
const partTwo = () => {
const lines = data.lines().map(l => l.split(' ').nums());
let count = 0;
for (const line of lines) {
for (let i = 0; i < line.length + 1; ++i) {
let l;
if (i === 0) {
l = line;
} else {
l = line.slice(0, i - 1).concat(line.slice(i));
}
let prev = l[0];
let valid = true;
let dir = 0;
for (const n of l.slice(1)) {
let delta = n - prev;
const new_dir = delta < 0 ? -1 : 1
if (dir === 0) {
dir = new_dir;
} else if (new_dir !== dir) {
valid = false;
break;
}
if (dir === 1) {
delta *= -1;
}
if (delta !== -1 && delta !== -2 && delta !== -3) {
valid = false;
break;
}
prev = n
}
if (valid) {
count += 1;
break;
}
}
}
console.log(count);
};
if (Deno.args[0]) {
console.log('Sample Data:');
data = await readEx();
console.log(data.lines().map(l => ' ' + l).join`\n`)
} else {
console.log('Real Data');
data = await read();
}
console.log('Output:');
partOne();
partTwo();