diff --git a/Day-08-B.js b/Day-08-B.js index 9c9ea7d..eed4bae 100644 --- a/Day-08-B.js +++ b/Day-08-B.js @@ -2,23 +2,23 @@ const input = document.body.innerText.trim().split('\n').map(line => line.match(/\w+/g)) let total = 0 for (let row of input) { - let key = row.slice(0, 10) - let one = key.filter(n => n.length === 2)[0] - let seven = key.filter(n => n.length === 3)[0] - let four = key.filter(n => n.length === 4)[0] - let eight = key.filter(n => n.length === 7)[0] - let three = key.filter(n => n.length === 5 && n.includes(one[0]) && n.includes(one[1]))[0] - let a = key.filter(n => n.length === 3)[0].split("").filter(c => !one.includes(c))[0] - let d = three.split("").filter(char => char !== a && !one.includes(char) && four.includes(char))[0] - let zero = key.filter(n => n.length === 6 && !n.includes(d))[0] - let nine = key.filter(n => n.length === 6 && n!== zero && n.includes(one[0]) && n.includes(one[1]))[0] - let six = key.filter(n => n.length === 6 && n !== nine && n !== zero)[0] - let c = one.split("").filter(char => !six.includes(char))[0] - let five = key.filter(n => n.length === 5 && !n.includes(c))[0] - let two = key.filter(n => n.length === 5 && n !== five && n !== three)[0] + let key = row.slice(0, 10) + let one = key.filter(n => n.length === 2)[0] + let seven = key.filter(n => n.length === 3)[0] + let four = key.filter(n => n.length === 4)[0] + let eight = key.filter(n => n.length === 7)[0] + let three = key.filter(n => n.length === 5 && n.includes(one[0]) && n.includes(one[1]))[0] + let a = key.filter(n => n.length === 3)[0].split("").filter(c => !one.includes(c))[0] + let d = three.split("").filter(char => char !== a && !one.includes(char) && four.includes(char))[0] + let zero = key.filter(n => n.length === 6 && !n.includes(d))[0] + let nine = key.filter(n => n.length === 6 && n!== zero && n.includes(one[0]) && n.includes(one[1]))[0] + let six = key.filter(n => n.length === 6 && n !== nine && n !== zero)[0] + let c = one.split("").filter(char => !six.includes(char))[0] + let five = key.filter(n => n.length === 5 && !n.includes(c))[0] + let two = key.filter(n => n.length === 5 && n !== five && n !== three)[0] - key = [zero, one, two, three, four, five, six, seven, eight, nine].map(n => n.split("").sort().join("")) - total += +row.slice(-4).map(n => key.indexOf(n.split("").sort().join(""))).join("") + key = [zero, one, two, three, four, five, six, seven, eight, nine].map(n => n.split("").sort().join("")) + total += +row.slice(-4).map(n => key.indexOf(n.split("").sort().join(""))).join("") } total @@ -29,26 +29,26 @@ It took about 15 minutes to code, but I had already thought of the solution befo The searchPerms function is completely generic and I wrote it previously for just such an occasion. */ function searchPerms(length, callback) { - const used = new Uint8Array(length) - const order = new Uint8Array(length) - let depth = 0 - function search(index) { - order[depth] = index - used[index] = 1 - depth++ + const used = new Uint8Array(length) + const order = new Uint8Array(length) + let depth = 0 + function search(index) { + order[depth] = index + used[index] = 1 + depth++ - if (depth === length) { - callback(order) - } else { - for (let i = 0; i < length; i++) { - if (!used[i]) search(i) - } - } - - depth-- - used[index] = 0 - } - for (let i = 0; i < length; i++) search(i) + if (depth === length) { + callback(order) + } else { + for (let i = 0; i < length; i++) { + if (!used[i]) search(i) + } + } + + depth-- + used[index] = 0 + } + for (let i = 0; i < length; i++) search(i) } const input = document.body.innerText.trim().split('\n').map(line => line.match(/\w+/g)) @@ -56,14 +56,14 @@ const map = ["abcefg", "cf", "acdeg", "acdfg", "bcdf", "abdfg", "abdefg", "acf", const valid = map.slice().sort() let total = 0 for (let row of input) { - let key = row.slice(0, 10) - let end = row.slice(-4) - let eight = "abcdefg" - function verify(permOrder) { - const mapped = key.map(num => num.split("").map(char => eight[permOrder[eight.indexOf(char)]]).sort().join("")).sort() - for (let i = 0; i < 10; i++) if (mapped[i] !== valid[i]) return - total += +end.map(num => map.indexOf(num.split("").map(char => eight[permOrder[eight.indexOf(char)]]).sort().join(""))).join("") - } - searchPerms(7, verify) + let key = row.slice(0, 10) + let end = row.slice(-4) + let eight = "abcdefg" + function verify(permOrder) { + const mapped = key.map(num => num.split("").map(char => eight[permOrder[eight.indexOf(char)]]).sort().join("")).sort() + for (let i = 0; i < 10; i++) if (mapped[i] !== valid[i]) return + total += +end.map(num => map.indexOf(num.split("").map(char => eight[permOrder[eight.indexOf(char)]]).sort().join(""))).join("") + } + searchPerms(7, verify) } total \ No newline at end of file diff --git a/Day-10-A.js b/Day-10-A.js new file mode 100644 index 0000000..b79ce9f --- /dev/null +++ b/Day-10-A.js @@ -0,0 +1,11 @@ +let input = document.body.innerText.trim().split('\n') +const scores = { + ")": 3, + "]": 57, + "}": 1197, + ">": 25137 +} +for (let i = 0; i < 10; i++) { + input = input.map(row => row.replace(/\(\)|<>|{}|\[]/g, "")) +} +input.reduce((acc, row) => acc + scores[row.match(/[)\]}>]/)?.[0]] || acc, 0) \ No newline at end of file diff --git a/Day-10-B.js b/Day-10-B.js new file mode 100644 index 0000000..9a36153 --- /dev/null +++ b/Day-10-B.js @@ -0,0 +1,12 @@ +let input = document.body.innerText.trim().split('\n') +const scores = { + "(": 1, + "[": 2, + "{": 3, + "<": 4 +} +for (let i = 0; i < 10; i++) { + input = input.map(row => row.replace(/\(\)|<>|{}|\[]/g, "")) +} +const scoreArr = input.map(row => row.split("").reverse().reduce((acc, char) => acc * 5 + scores[char], 0)).filter(n => n) +console.log(scoreArr.sort((a, b) => a - b)[scoreArr.length / 2 | 0]) \ No newline at end of file