Skip to content

Instantly share code, notes, and snippets.

@p-a
p-a / aoc_2022_day_22_revisited.js
Last active January 23, 2023 09:51
AoC 2022 Day 22 Revisited
import { map, sum } from "../../lib/arrays.js";
import { gcd } from "../../lib/math.js";
import { pipe } from "../../lib/utils.js";
const parse = input => {
const grp = input.split("\n\n");
const net = grp[0]
.split("\n")
.map(([...line]) => line.map(c => c.trim()));
const instr = [...`R${grp[1]}`.matchAll(/(([LR])(\d*))/g)]
@p-a
p-a / aoc_2022_day_19_bananas.js
Created January 8, 2023 21:37
AoC 2022 Day 19 bananas
import { pipe } from "../../lib/utils.js";
import {
sum,
product,
map,
limit,
} from "../../lib/arrays.js";
import { MaxHeap } from "../../lib/heaps.js";
const RE = /Blueprint (\d+): Each ore robot costs (\d+) ore. Each clay robot costs (\d+) ore. Each obsidian robot costs (\d+) ore and (\d+) clay. Each geode robot costs (\d+) ore and (\d+) obsidian./;
@p-a
p-a / aoc_2022_day25.js
Created December 25, 2022 06:52
AoC 2022 Day 25
const pipe = (...fns) => v => fns.reduce((a, f) => f(a), v);
const MAPPING = "=-012";
const parse = input =>
input
.split("\n")
.map(line =>
line.split("").map(c => MAPPING.indexOf(c) - 2)
);
const toDecimal = list =>
@p-a
p-a / aoc_2022_day24.js
Last active January 5, 2023 16:43
AoC 2022 Day 24
import { pipe } from "../../lib/utils.js";
import { fast_abs as abs, lcm } from "../../lib/math.js";
import { MinHeap } from "../../lib/heaps.js";
const parse = input => input.split("\n").map(([...l]) => l);
const mapBlizzard = map => {
const [w, h] = [map[0].length - 2, map.length - 2];
const s = Array.from({ length: lcm(w, h) }).map(
(_, i) => i
@p-a
p-a / aoc_2022_day23.js
Last active January 5, 2023 21:20
AoC 2022 Day 23
import { pipe } from "../../lib/utils.js";
const X_OFFSET = 1 << 15;
const Y_OFFSET = 1 << 29;
const W = 1 << 16;
// prettier-ignore
const MOVES = [
[[-1 -W, 0 -W, 1 -W], 0 -W],
[[-1 +W, 0 +W, 1 +W], 0 +W],
@p-a
p-a / aoc_2022_day22.js
Created December 22, 2022 15:57
AoC 2022 Day 22
const pipe = (...fns) => v => fns.reduce((a, f) => f(a), v);
const parse = input => {
const g = input.split("\n\n");
const map = g[0]
.split("\n")
.map(line => line.split("").map(c => c.trim()));
const instr = [...("R" + g[1]).matchAll(/(([LR])(\d+))/g)]
.map(([, , a, b]) => [a, b])
.map(([a, b]) => [a === "L" ? -1 : 1, Number(b)]);
@p-a
p-a / aoc_2022_day21.js
Last active December 21, 2022 22:14
AoC 2022 Day 21
const pipe = (...fns) => v => fns.reduce((a, f) => f(a), v);
const parse = input =>
new Map(
input
.split("\n")
.filter(Boolean)
.map(line => line.split(": "))
.map(([k, v]) => [k, v.split(" ")])
);
@p-a
p-a / aoc_2022_day22_testrunner.js
Created December 20, 2022 20:18
AoC Day 20 test runner
import fs from "fs";
import path from "path";
import { part1, part2 } from "./code.js";
const dir = path.dirname(module.filename);
const getLinesFromFile = file => {
const filename = `${dir}/${file}`;
return fs.readFileSync(filename, "utf8");
};
@p-a
p-a / aoc_2022_day20.js
Last active December 20, 2022 20:40
AoC 2022 Day 20
const pipe = (...fns) => v => fns.reduce((a, f) => f(a), v);
const parse = input =>
input.split("\n").filter(Boolean).map(Number);
const mixing = (file, rounds = 1, key = 1) => {
file = file.map(v => v * key).map(d => [d]);
const buffer = [...file];
while (rounds--)
file.forEach(v => {
const i = buffer.indexOf(v);
buffer.splice(i, 1);
@p-a
p-a / aoc_2022_day19.js
Last active December 21, 2022 12:03
AoC 2022 Day 19
const RE = /Blueprint (\d+): Each ore robot costs (\d+) ore. Each clay robot costs (\d+) ore. Each obsidian robot costs (\d+) ore and (\d+) clay. Each geode robot costs (\d+) ore and (\d+) obsidian./;
const parse = input =>
input
.split("\n")
.map(line => RE.exec(line))
.map(([, ...num]) => num.map(Number))
.map(
([
,