Public
Edited
Dec 2
Paused
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function toSnafu(n) {
const toSym = ["=", "-", "0", "1", "2"];
const toS = (n) =>
n === 0 ? "" : toS(Math.floor((n + 2) / 5)).concat(toSym[(n + 2) % 5]);
return n == 0 ? "0" : toS(n);
}
Insert cell
Insert cell
function fromSnafu(s) {
const fromSym = { "=": -2, "-": -1, "0": 0, "1": 1, "2": 2 };
return s
.split("")
.map((sym) => fromSym[sym])
.reverse()
.reduce((total, v, i) => total + v * Math.pow(5, i));
}
Insert cell
Insert cell
function part1(input) {
return toSnafu(AOC.sum(input.split("\n").map(fromSnafu)));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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