part 2
This commit is contained in:
parent
d59790e7d6
commit
45c706f93f
8
2022/09/bigSample
Normal file
8
2022/09/bigSample
Normal file
@ -0,0 +1,8 @@
|
||||
R 5
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20
|
@ -2,44 +2,43 @@ import * as R from "remeda";
|
||||
import Victor from "@a-robu/victor";
|
||||
|
||||
import { readFile } from "../05/part1.js";
|
||||
import { passthru } from '../08/part1.js'
|
||||
import { passthru } from "../08/part1.js";
|
||||
|
||||
const directions = {
|
||||
R: new Victor(1,0),
|
||||
L: new Victor(-1,0),
|
||||
U: new Victor(0,1),
|
||||
D: new Victor(0,-1),
|
||||
}
|
||||
R: new Victor(1, 0),
|
||||
L: new Victor(-1, 0),
|
||||
U: new Victor(0, 1),
|
||||
D: new Victor(0, -1),
|
||||
};
|
||||
|
||||
const parseLine = R.createPipe(
|
||||
line => line.split(" "),
|
||||
([direction,times]) => Array(parseInt(times)).fill(directions[direction])
|
||||
(line) => line.split(" "),
|
||||
([direction, times]) => Array(parseInt(times)).fill(directions[direction])
|
||||
);
|
||||
|
||||
|
||||
const readInput = (...args) => R.pipe(
|
||||
export const readInput = (...args) =>
|
||||
R.pipe(
|
||||
readFile(...args),
|
||||
text => text.split("\n"),
|
||||
(text) => text.split("\n"),
|
||||
R.compact,
|
||||
R.map( parseLine ),
|
||||
R.flatten,
|
||||
)
|
||||
R.map(parseLine),
|
||||
R.flatten
|
||||
);
|
||||
|
||||
export const puzzleInput = readInput(import.meta.url, "input");
|
||||
export const sample = readInput(import.meta.url, "sample");
|
||||
|
||||
const touching = (v1,v2) => [v1.distanceX(v2), v1.distanceY(v2)].every(
|
||||
x => x*x <= 1
|
||||
);
|
||||
export const touching = (v1, v2) =>
|
||||
[v1.distanceX(v2), v1.distanceY(v2)].every((x) => x * x <= 1);
|
||||
|
||||
export function moveOnce( head, tail, d ) {
|
||||
export const moveOnce = (head, tail) => (d) => {
|
||||
head.add(d);
|
||||
|
||||
if( touching(head,tail) ) return tail.toString();
|
||||
if (touching(head, tail)) return tail.toString();
|
||||
|
||||
const delta = head.clone().subtract(tail).normalize();
|
||||
|
||||
if( delta.x * delta.y ) {
|
||||
if (delta.x * delta.y) {
|
||||
delta.x = delta.x > 0 ? 1 : -1;
|
||||
delta.y = delta.y > 0 ? 1 : -1;
|
||||
}
|
||||
@ -47,21 +46,14 @@ export function moveOnce( head, tail, d ) {
|
||||
tail.add(delta);
|
||||
|
||||
return tail.toString();
|
||||
}
|
||||
|
||||
const moveAround = (directives) => {
|
||||
const head = new Victor(0,0);
|
||||
const tail = new Victor(0,0);
|
||||
|
||||
const move = d => moveOnce(head,tail,d);
|
||||
return [
|
||||
tail.toString(),
|
||||
...directives.map( move )
|
||||
]
|
||||
};
|
||||
|
||||
export default R.createPipe(
|
||||
moveAround,
|
||||
R.uniq,
|
||||
moves => moves.length
|
||||
);
|
||||
const moveAround = (directives) => {
|
||||
const head = new Victor(0, 0);
|
||||
const tail = new Victor(0, 0);
|
||||
|
||||
const move = moveOnce(head, tail);
|
||||
return directives.map(move);
|
||||
};
|
||||
|
||||
export default R.createPipe(moveAround, R.uniq, (moves) => moves.length);
|
||||
|
@ -1,4 +1,36 @@
|
||||
import * as R from "remeda";
|
||||
import Victor from "@a-robu/victor";
|
||||
|
||||
import { touching, readInput } from "./part1.js";
|
||||
|
||||
export default () => {};
|
||||
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);
|
||||
|
@ -5,36 +5,40 @@ const V = (...args) => new Victor(...args);
|
||||
|
||||
import { expectSolution } from "../01/main.js";
|
||||
import part1, { moveOnce, sample, puzzleInput } from "./part1.js";
|
||||
import part2 from "./part2.js";
|
||||
import part2, { bigSample } from "./part2.js";
|
||||
|
||||
describe("part 1", () => {
|
||||
test( "moveOnce", () => {
|
||||
let head = V(1,0);
|
||||
let tail = V(0,0);
|
||||
moveOnce(head,tail,V(1,0));
|
||||
test("moveOnce", () => {
|
||||
let head = V(1, 0);
|
||||
let tail = V(0, 0);
|
||||
moveOnce(head, tail)(V(1, 0));
|
||||
|
||||
expect(tail).toMatchObject({x:1,y:0});
|
||||
expect(tail).toMatchObject({ x: 1, y: 0 });
|
||||
|
||||
moveOnce(head,tail,V(0,1));
|
||||
moveOnce(head, tail)(V(0, 1));
|
||||
|
||||
expect(tail).toMatchObject({x:1,y:0});
|
||||
expect(tail).toMatchObject({ x: 1, y: 0 });
|
||||
|
||||
moveOnce(head,tail,V(0,1));
|
||||
moveOnce(head, tail)(V(0, 1));
|
||||
|
||||
expect(tail).toMatchObject({x:2,y:1});
|
||||
expect(tail).toMatchObject({ x: 2, y: 1 });
|
||||
});
|
||||
|
||||
} );
|
||||
|
||||
test( "sample", () => {
|
||||
test("sample", () => {
|
||||
expect(part1(sample)).toEqual(13);
|
||||
} )
|
||||
});
|
||||
test("solution", () => {
|
||||
expectSolution(part1(puzzleInput)).toEqual(6030);
|
||||
});
|
||||
});
|
||||
|
||||
describe("part 2", () => {
|
||||
test.todo("solution", () => {
|
||||
expectSolution(part2(puzzleInput)).toEqual("TODO");
|
||||
test("sample", () => {
|
||||
expect(part2(sample)).toEqual(1);
|
||||
expect(part2(bigSample)).toEqual(36);
|
||||
});
|
||||
|
||||
test("solution", () => {
|
||||
expectSolution(part2(puzzleInput)).toEqual(2545);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user