Skip to content

Instantly share code, notes, and snippets.

@Yiin
Last active December 4, 2021 10:04
Show Gist options
  • Save Yiin/149bfecd13da2a3e1d9e46f90be90c46 to your computer and use it in GitHub Desktop.
Save Yiin/149bfecd13da2a3e1d9e46f90be90c46 to your computer and use it in GitHub Desktop.
AoC 2021
const input = rawInput.split('\n').filter(Boolean).map(Number);
let inc = 0;
let last;
for (const x of input) {
if (typeof last !== 'undefined' && x > last) {
inc++;
}
last = x;
}
console.log(inc);
const input = rawInput.split('\n').filter(Boolean).map(Number);
let inc = 0;
let last;
for (const i in input) {
const x = input.slice(i, +i + 3).reduce((a, b) => a + b);
if (typeof last !== 'undefined' && x > last) {
inc++;
}
last = x;
}
console.log(inc);
const lines = input.split('\n').filter(Boolean).map(line => {
[dir, amount] = line.split(' ');
return { dir, amount: +amount };
});
const result = lines.reduce(({ x, z }, { dir, amount }) => ({
x: x + ({ forward: amount }[dir] || 0),
z: z + ({ down: amount, up: -amount }[dir] || 0)
}), { x: 0, z: 0 });
console.log(result.x * result.z);
const lines = input.split('\n').filter(Boolean).map(line => {
[dir, amount] = line.split(' ');
return { dir, amount: +amount };
});
const result = lines.reduce(({ x, z, aim }, { dir, amount }) => ({
x: x + ({ forward: amount }[dir] || 0),
z: z + ({ forward: amount * aim }[dir] || 0),
aim: aim + ({ down: amount, up: -amount }[dir] || 0)
}), { x: 0, z: 0, aim: 0 });
console.log(result.x * result.z);
const lines = input.split('\n').filter(Boolean);
function calc(mode) {
const state = {};
for (const line of lines) {
for (const index in [...line]) {
if (!state[index]) {
state[index] = 0;
}
state[index] += { most: 1, least: -1}[mode] * (line[index] * 2 - 1);
}
}
const b = Object.values(state).map(val => val <= 0 ? 0 : 1).join('');
return parseInt(b, 2);
}
const gamma = calc('most');
const epsilon = calc('least');
console.log(gamma * epsilon);
const lines = input.split('\n').filter(Boolean);
function calc(lines, mode) {
const state = {};
const toggle = { most: 1, least: -1}[mode];
for (const index in [...lines[0]]) {
if (!state[index]) {
state[index] = 0;
}
for (const line of lines) {
state[index] += toggle * (line[index] * 2 - 1);
}
lines = lines.filter(line => +line[index] === (state[index] ? +(state[index] > 0) : +!!(toggle + 1)));
if (lines.length === 1) {
return parseInt(lines[0], 2);
}
}
}
const gamma = calc(lines, 'most');
const epsilon = calc(lines, 'least');
console.log(gamma * epsilon);
const lines = input.split('\n');
const numbers = lines.shift().split(',').map(Number);
let boards = lines
.join('\n')
.replace(/\ +/g, ' ')
.split('\n\n')
.map(board => [...board.matchAll(/\d+/g)].map(([n]) => +n));
let last;
const score = (board, rolled) => board.filter(number => !rolled.includes(number)).reduce((a, b) => a + b) * rolled[rolled.length - 1];
for (let i = 1; i <= numbers.length; ++i) {
const rolled = numbers.slice(0, i);
const winningBoards = boards.filter(board => {
const matchesRow = board.reduce((acc, number, j) =>
acc === 5
? acc
: rolled.includes(number) && j % 5 === acc && acc + 1 || 0
, 0) === 5;
const matchesColumn = (() => {
for (let col = 0; col < 5; ++col) {
if (board.filter((number, j) => rolled.includes(number) && j % 5 === col).length === 5) {
return true;
}
}
return false;
})();
return matchesRow || matchesColumn;
});
if (winningBoards.length) {
boards = boards.filter(board => !winningBoards.includes(board));
if (!last) {
console.log('part 1:', score(winningBoards[0], rolled));
}
last = [winningBoards[winningBoards.length - 1], [...rolled]];
console.log(last[0].map(x => last[1].includes(x) ? `(${x})` : x).join(','))
}
}
console.log('part 2:', score(...last))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment