Published
Edited
Dec 10, 2019
Insert cell
Insert cell
Insert cell
sameDigitTwiceInARow = x => {
let result = false
const str = x.toString().split("")
for(let i = 0; i < str.length - 1; i++) {
result = str[i]==str[i+1] || result
}
return result
}
Insert cell
digitsNeverDecrease = x => {
let result = true
const str = x.toString().split("")
for(let i = 0; i < str.length - 1; i++) {
result = str[i]<=str[i+1] && result
}
return result
}
Insert cell
possibles = x => {
const [min, max] = x.split("-").map(d => parseInt(d))
return Array.from({length: max - min + 1}, (_,i) => i + min) // range
.filter(sameDigitTwiceInARow)
.filter(digitsNeverDecrease)
}
Insert cell
solve1 = x => possibles(x).length
Insert cell
solve1(data)
Insert cell
Insert cell
tests = ["112233","123444","111122"]
Insert cell
repeats = x => x.match(/(\d)\1+/g)
Insert cell
hasValidRepeat = x => x.reduce((acc, inc) => inc.length == 2 || acc, false)
Insert cell
tests
.map(String)
.map(repeats)
.map(hasValidRepeat)
Insert cell
solve2 = range =>
possibles(range)
.map(String)
.map(repeats)
.filter(hasValidRepeat)
.length
Insert cell
solve2(data)
Insert cell
Insert cell
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