Published
Edited
Dec 10, 2020
Insert cell
Insert cell
input = (await FileAttachment("Avent of Code 2020 - Day 02.txt").text())
.trim()
.split('\n')
.map(line => {
// still too lazy to learn how to regex
const t = line.split(" ");
const range = t[0].split("-");

return [+range[0], +range[1], t[1][0], t.slice(2).join("")];
})
Insert cell
validPasswords = {
let validPasswords = 0;
for (const data of input) {
const pw = data[3];
const cc = data[2].charCodeAt(0);
let charCount = 0;
for (let i = 0; i < pw.length; i++) {
if (cc === pw.charCodeAt(i)) charCount++;
}
if (charCount >= data[0] && charCount <= data[1]) validPasswords++;
}
return validPasswords;
}
Insert cell
Insert cell
validPasswords2 = {
let validPasswords = 0;
for (const data of input) {
const pw = data[3];
const cc = data[2].charCodeAt(0);
let charCount = 0;
if (cc === pw.charCodeAt(data[0] - 1)) charCount++;
if (cc === pw.charCodeAt(data[1] - 1)) charCount++;
if (charCount === 1) validPasswords++;
}
return validPasswords;
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more