Skip to content

Commit

Permalink
Day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Willard21 committed Dec 10, 2021
1 parent 01baf13 commit b94ac6c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 44 deletions.
88 changes: 44 additions & 44 deletions Day-08-B.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -29,41 +29,41 @@ 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))
const map = ["abcefg", "cf", "acdeg", "acdfg", "bcdf", "abdfg", "abdefg", "acf", "abcdefg", "abcdfg"]
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
11 changes: 11 additions & 0 deletions Day-10-A.js
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions Day-10-B.js
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit b94ac6c

Please sign in to comment.