-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.js
38 lines (34 loc) · 1.06 KB
/
day4.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
'use strict';
function aoc2017d3p1(input) {
var phrases = input.split(/\n/);
return phrases.reduce(function reducer (sum, phrase, index, array) {
var map = {};
var words = phrase.split(/\s+/);
var isInvalid = words.some(function (word, index) {
if (map.hasOwnProperty(word)) {
// bail out of the loop
return true;
}
map[word] = index;
return false;
});
return isInvalid ? sum : sum + 1;
}, 0);
}
function aoc2017d3p2(input) {
var phrases = input.split(/\n/);
return phrases.reduce(function reducer (sum, phrase, index, array) {
var map = {};
var words = phrase.split(/\s+/);
var isInvalid = words.some(function (word, index) {
var key = [...word].sort();
if (map.hasOwnProperty(key)) {
// bail out of the loop
return true;
}
map[key] = index;
return false;
});
return isInvalid ? sum : sum + 1;
}, 0);
}