adventofcode/2022/14/part1.js

84 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-12-14 15:40:00 +00:00
import * as R from "remeda";
2022-12-14 15:40:27 +00:00
import Victor from "@a-robu/victor";
2022-12-14 15:40:00 +00:00
import { readFile } from "../05/part1.js";
export const V = (...args) => new Victor(...args);
2022-12-14 15:40:27 +00:00
const parseLine = (line) => line.match(/[\d,]+/g).map((x) => eval(`[${x}]`));
2022-12-14 15:40:00 +00:00
const readInput = (...args) =>
R.pipe(
readFile(...args),
(lines) => lines.split("\n"),
R.compact, // remove last line
2022-12-14 15:40:27 +00:00
R.map(parseLine)
2022-12-14 15:40:00 +00:00
);
export const puzzleInput = readInput(import.meta.url, "input");
export const sample = readInput(import.meta.url, "sample");
export function genCave(rocks) {
2022-12-14 15:40:27 +00:00
const cave = {};
for (const rock of rocks) {
const points = rock.map(Victor.fromArray);
let from = points.shift();
while (points.length) {
/** @type Victor */
const to = points.shift();
const direction = to.clone().subtract(from).normalize();
R.times(to.clone().subtract(from).length() + 1, () => {
if (!cave[from.y]) cave[from.y] = {};
cave[from.y][from.x] = "#";
from.add(direction);
});
from = to;
2022-12-14 15:40:00 +00:00
}
2022-12-14 15:40:27 +00:00
}
return cave;
2022-12-14 15:40:00 +00:00
}
2022-12-14 15:40:27 +00:00
function pourSand(cave) {
cave = R.clone(cave);
let sand = 0;
/** @type Victor */
let current;
2022-12-14 15:40:00 +00:00
2022-12-14 15:40:27 +00:00
const maxDepth = R.pipe(
cave,
R.keys,
R.map((x) => parseInt(x)),
R.maxBy(R.identity)
);
console.log({ maxDepth });
while (true) {
if (!current) current = V(500, 0);
if (current.y > maxDepth) return sand;
const next = [
[0, 1],
[-1, 1],
[1, 1],
]
.map((args) => V(...args))
.map((v) => v.add(current))
.find((v) => !(cave[v.y] && cave[v.y][v.x]));
if (next) {
current = next;
} else {
if (!cave[current.y]) cave[current.y] = {};
cave[current.y][current.x] = "o";
sand++;
current = null;
}
}
}
2022-12-14 15:40:00 +00:00
2022-12-14 15:40:27 +00:00
export default R.createPipe(genCave, pourSand);