-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.js
63 lines (52 loc) · 1.4 KB
/
day7.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
"use strict";
const readline = require('readline');
const fs = require('fs');
let examples = [
'abba[mnop]qrst',
'abcd[bddb]xyyx',
'aaaa[qwer]tyui',
'ioxxoj[asdfgh]zxcvbn',
'yhrowrreplrrsbupeor[nchtznfzbzwnogh]rynudxihckzattbz[dshxeaqusdlhydtm]rvqzuffgqtysfzxp'
];
function split(ip7) {
return ip7.split(/[\[\]]/);
}
function hasAbba(part) {
for (let i = 0; i < part.length - 3; i++) {
if (part[i] == part[i + 3] && part[i + 1] == part[i + 2] && part[i] != part[i + 1])
return true;
}
return false;
}
function supportsTls(parts) {
let abba = parts.map((v, i) => {
return {
hn: (i % 2 == 1),
abba: hasAbba(v)
};
});
let x = abba.filter(e => !e.hn).reduce((p, c) => p || c.abba, false);
let y = !abba.filter(e => e.hn).reduce((p, c) => p || c.abba, false);
return x && y;
}
console.log(
examples.map(e => supportsTls(split(e)))
);
function ReadFromFile(name, whenDone) {
const rl = readline.createInterface({
input: fs.createReadStream(name)
});
var lines = [];
rl.on('line', (line) => {
lines.push(line);
});
rl.on('close', () => {
whenDone(lines);
});
}
ReadFromFile('day7.txt', (lines) => {
console.log(lines.length);
let r = lines.map(e => supportsTls(split(e)));
console.log(r.length);
console.log(r.filter(_ => _).length);
});