-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc8
More file actions
35 lines (28 loc) · 1.09 KB
/
aoc8
File metadata and controls
35 lines (28 loc) · 1.09 KB
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
allowed = ["abcefg", "cf", "acdeg", "acdfg", "bcdf", "abdfg", "abdefg", "acf", "abcdefg", "abcdfg"];
letters = "abcdefg";
arr = document.getElementsByTagName("pre")[0].innerText.split("\n").filter(function(n){return n;});
permute = function(str, fun) {
if (!str) {
return fun(str);
}
str.split("").forEach(function(c) {
permute(str.replace(c, ""), function(sub) {fun(c + sub)});
});
}
arr.map(function(row) {
options = row.split(" | ")[0].split(" ");
digits = row.split(" | ")[1].split(" ");
foundVersion = "";
permute(letters, function(version) {
if (!options.find(function(option) {
return allowed.indexOf(
option.split("").map(function(c) {return version[letters.indexOf(c)]}).sort(function(a,b){return letters.indexOf(a)-letters.indexOf(b)}).join(""))<0;
})) {
foundVersion = version;
}
});
return parseInt(digits.map(function(digit) {
return allowed.indexOf(
digit.split("").map(function(c) {return foundVersion[letters.indexOf(c)]}).sort(function(a,b){return letters.indexOf(a)-letters.indexOf(b)}).join(""));
}).join(""));
}).reduce(function(a,b) {return a+b})