-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.js
50 lines (44 loc) · 1.02 KB
/
Day12.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
fs = require('fs');
fs.readFile('Day12.txt', 'utf8', function(err, data) {
if(err) {
return console.log(err);
}
data = data.trim();
data = data.replace(/,/g, '');
data = data.replace(/<-> /g, '');
let lines = data.split('\n');
let matches = [];
let discards = [];
let groups = [];
while(lines.length > 0) {
let nums = new Set();
let search = true;
while(search) {
matches = [];
discards = [];
lines.forEach(function(line) {
let numbers = line.split(' ').map(Number);
let match = false;
if(nums.size === 0) nums.add(numbers[0]);
for(var i = 0; i < numbers.length; i++) {
if(nums.has(numbers[i])) {
match = true;
break;
}
}
if(match) {
matches.push(line);
numbers.forEach(function(number) {
nums.add(number);
});
}
else discards.push(line);
});
if(matches.length === 0) search = false;
lines = discards;
}
groups.push(nums);
}
console.log("Answer 1: " + groups[0].size);
console.log("Answer 2: " + groups.length);
});