adventofcode/2022/09/part2.js

37 lines
859 B
JavaScript
Raw Normal View History

2022-12-09 17:28:05 +00:00
import * as R from "remeda";
2022-12-09 18:05:42 +00:00
import Victor from "@a-robu/victor";
2022-12-09 17:28:05 +00:00
2022-12-09 18:05:42 +00:00
import { touching, readInput } from "./part1.js";
2022-12-09 17:28:05 +00:00
2022-12-09 18:05:42 +00:00
export const bigSample = readInput(import.meta.url, "bigSample");
export const moveOnce = (knots) => (d) => {
R.first(knots).add(d);
for (const i of R.range(1, knots.length)) {
if (touching(knots[i - 1], knots[i])) break;
const delta = knots[i - 1].clone().subtract(knots[i]).normalize();
if (delta.x * delta.y) {
delta.x = delta.x > 0 ? 1 : -1;
delta.y = delta.y > 0 ? 1 : -1;
}
knots[i].add(delta);
}
return R.last(knots).toString();
};
const moveAround = (directives) => {
const knots = Array(10)
.fill(null)
.map(() => new Victor(0, 0));
const move = moveOnce(knots);
return directives.map(move);
};
export default R.createPipe(moveAround, R.uniq, (moves) => moves.length);