Published
Edited
Mar 23, 2022
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
_
Insert cell
Insert cell
cy = require("cytoscape@3.20.0/dist/cytoscape.umd.js")
Insert cell
Insert cell
mathjs = require("mathjs@10.0.0/lib/browser/math.js")
Insert cell
Insert cell
R = require("ramda")
Insert cell
Insert cell
Insert cell
Insert cell
function iterateReduce(array, reducer, initialValue) {
return array.reduce(
(previousValue, currentValue) => {
return [...previousValue, reducer(_.last(previousValue), currentValue)];
},
[initialValue]
);
}
Insert cell
Insert cell
day1 = FileAttachment("day_1.txt").text()
Insert cell
Insert cell
Insert cell
measurements = day1.split("\n").map(parseFloat)
Insert cell
Insert cell
Insert cell
changes = R.aperture(2, measurements).map(([fst, snd]) => fst < snd)
Insert cell
Insert cell
changes.filter((x) => x).length
Insert cell
Insert cell
windowSums = R.aperture(3, measurements).map(_.sum)
Insert cell
Insert cell
R.aperture(2, windowSums)
.map(([fst, snd]) => fst < snd)
.filter((x) => x).length
Insert cell
Insert cell
Insert cell
day2 = FileAttachment("day_2.txt").text()
Insert cell
Insert cell
commands = _.trim(day2)
.split("\n")
.map((line) => {
const [command, unitString] = line.split(" ");
return [command, parseFloat(unitString)];
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
updatePosition = (position, commandPair) => {
const { horizontal, depth } = position;
const [command, x] = commandPair;
switch (command) {
case "forward":
return {
depth,
horizontal: horizontal + x
};
case "down":
return {
depth: depth + x,
horizontal
};
case "up":
return {
depth: depth - x,
horizontal
};
}
}
Insert cell
Insert cell
finalPosition = commands.reduce(updatePosition, initialPosition)
Insert cell
Insert cell
Insert cell
finalPosition.depth * finalPosition.horizontal
Insert cell
Insert cell
Insert cell
Insert cell
updatePositionAndAim = (position, commandPair) => {
const { horizontal, depth, aim } = position;
const [command, x] = commandPair;
switch (command) {
case "forward":
return {
depth: depth + aim * x,
horizontal: horizontal + x,
aim
};
case "down":
return {
aim: aim + x,
depth,
horizontal
};
case "up":
return {
aim: aim - x,
depth,
horizontal
};
}
}
Insert cell
Insert cell
finalAimedPosition = commands.reduce(
updatePositionAndAim,
initialPositionWithAim
)
Insert cell
Insert cell
Insert cell
finalAimedPosition.depth * finalAimedPosition.horizontal
Insert cell
Insert cell
day3 = FileAttachment("day_3.txt").text()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
gammaRateString = R.transpose(day3.split("\n"))
.map((column) => {
const count = _.countBy(column);
return count[0] > count[1] ? 0 : 1;
})
.join("")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
powerConsumption = epsilonRate * gammaRate
Insert cell
Insert cell
Insert cell
oxygenGeneratorRatingString = {
let numbers = day3.split("\n");
const bits = numbers[0].length;

for (let bit = 0; bit < bits; ++bit) {
const bitsInPosition = numbers.map((number) => number[bit]);
const count = _.countBy(bitsInPosition);
const mostCommonValue = count[0] > count[1] ? "0" : "1";

numbers = numbers.filter((number) => number[bit] === mostCommonValue);

if (numbers.length === 1) {
return numbers[0];
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
lifeSupportRating = oxygenGeneratorRating * co2ScrubberRating
Insert cell
Insert cell
day4 = FileAttachment("day_4.txt").text()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
bingoSubsystem = {
const [drawnNumbersChunk, ...boardChunks] = day4.trim().split("\n\n");
return {
drawnNumbers: drawnNumbersChunk.split(",").map((s) => parseFloat(s)),
boards: boardChunks.map((boardChunk) =>
boardChunk.split("\n").map((row) =>
row
.trim()
.split(/\s+/)
.map((cell) => parseFloat(cell))
)
)
};
}
Insert cell
Insert cell
scoreBoard = (board, markedNumbers) => {
const rows = board;
const columns = R.transpose(board);

const hasWon = [...rows, ...columns].some((line) =>
line.every((number) => markedNumbers.includes(number))
);

if (!hasWon) {
return undefined;
}

const unmarkedNumbers = board
.flat()
.filter((number) => !markedNumbers.includes(number));

return _.sum(unmarkedNumbers) * _.last(markedNumbers);
}
Insert cell
Insert cell
winningBoardScore = {
for (let index = 0; index < drawnNumbers.length; ++index) {
const markedNumbers = _.take(drawnNumbers, index);

for (const board of boards) {
const score = scoreBoard(board, markedNumbers);
if (score) return score;
}
}
}
Insert cell
Insert cell
lastWinningBoardScore = {
let unwonBoards = boards;
let lastScore;

for (let index = 0; index < drawnNumbers.length; ++index) {
const markedNumbers = _.take(drawnNumbers, index);

for (const board of unwonBoards) {
lastScore = scoreBoard(board, markedNumbers);
}
unwonBoards = unwonBoards.filter(
(board) => !scoreBoard(board, markedNumbers)
);
}

return lastScore;
}
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