Published
Edited
Dec 12, 2020
1 fork
1 star
Insert cell
Insert cell
Insert cell
positions = input.trim().split('\n')
.flatMap((line) => {
const [, password] = line.match(/: (\w+)/)
return password.split('').map((letter, position) => ({letter, position: position + 1}))
})
Insert cell
Insert cell
Insert cell
Insert cell
part1 = input.trim().split('\n').filter((line) => {
const [, min, max, letter, password] = line.match(/(\d+)-(\d+) (\w): (.*)/)
const count = password.split('').filter(c => c === letter).length
return min <= count && count <= max
}).length
Insert cell
Insert cell
part2 = input.trim().split('\n').filter((line) => {
const [, i, j, letter, password] = line.match(/(\d+)-(\d+) (\w): (.*)/)
const letters = password.split('')
return (letters[i - 1] === letter) ^ (letters[j - 1] === letter)
}).length
Insert cell
import { vl } from '@vega/vega-lite-api'
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