Published
Edited
Dec 12, 2020
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
passports = input.trim()
.split('\n\n')
.map((passport) => Object.fromEntries(passport.split(/\s+/).map((pair) => pair.split(':'))))
Insert cell
part1 = passports.filter((p) => Object.keys(p).filter((field) => field in checks).length === numChecks).length
Insert cell
part2 = passports
.map((p) => Object.entries(checks).map(([field, test]) => p[field] && test(p[field])))
.filter((results) => results.length === numChecks && results.every(d => d))
.length
Insert cell
eyeColors = new Set(['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'])
Insert cell
checks = ({
byr: (d) => 1920 <= +d && +d <= 2002,
iyr: (d) => 2010 <= +d && +d <= 2020,
eyr: (d) => 2020 <= +d && +d <= 2030,
hgt: (d) => {
const [, v, unit] = d.match(/^(\d+)(cm|in)$/) || []
return v && unit === 'cm' ? 150 <= +v && +v <= 193 : 59 <= +v && +v <= 76
},
hcl: (d) => d.match(/^#[0-9a-f]{6}$/),
ecl: (d) => eyeColors.has(d),
pid: (d) => d.match(/^\d{9}$/),
})
Insert cell
numChecks = Object.keys(checks).length
Insert cell
sample = `pid:0123456789 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
hcl:#623a2f

eyr:2029 ecl:blu cid:129 byr:1989
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm

hcl:#888785
hgt:164cm byr:2001 iyr:2015 cid:88
pid:545766238 ecl:hzl
eyr:2022

iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719`
Insert cell
Insert cell
import { Table } from '@observablehq/table'
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