37 lines
859 B
JavaScript
37 lines
859 B
JavaScript
import * as R from "remeda";
|
|
import Victor from "@a-robu/victor";
|
|
|
|
import { touching, readInput } from "./part1.js";
|
|
|
|
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);
|