Published
Edited
Dec 8, 2021
Insert cell
# Advent of Code - Day 8
Insert cell
Insert cell
test.filter(x => x.length == 2 || x.length == 7 || x.length == 4 || x.length == 3)
Insert cell
Insert cell
input
.map(x => x[1])
.flat()
.filter(x => x.length == 2 || x.length == 7 || x.length == 4 || x.length == 3)
Insert cell
mapping = (test) => {
let m = Array(10)
let lengths = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]

m = Array
.from(m)
.map((x,i) => test.filter(x => x.length == lengths[i]))

m[6] = m[6]
.filter(x => !Array.from(m[1][0]).every(s => Array.from(x).includes(s)))
m[0] = m[0]
.filter(x => x != m[6][0])
.filter(x => !Array.from(m[4][0]).every(s => Array.from(x).includes(s)))

m[9] = m[9]
.filter(x => x != m[6][0])
.filter(x => x != m[0][0])

m[3] = m[3]
.filter(x => Array.from(m[1][0]).every(s => Array.from(x).includes(s)))

m[5] = m[5]
.filter(x => Array.from(m[4][0]).filter(c => !m[1][0].includes(c)).every(s => Array.from(x).includes(s)))

m[2] = m[2]
.filter(x => x != m[3][0])
.filter(x => x != m[5][0])
return m
}
Insert cell
mapping(input[0][0])
Insert cell
order = (str) => Array.from(str).sort((a,b)=> a.localeCompare(b)).join("")
Insert cell
order("badc")
Insert cell
input.map(entry => {
let m = mapping(entry[0]).map(x => order(x[0]))
return parseInt(entry[1].map(x => m.indexOf(order(x))).join(""))
})
.reduce((previous, current) => previous + current)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more