prettier
This commit is contained in:
parent
f4553810c1
commit
f80142b6a7
@ -7,40 +7,36 @@ const readInput = (...args) =>
|
|||||||
readFile(...args),
|
readFile(...args),
|
||||||
(lines) => lines.split("\n\n"),
|
(lines) => lines.split("\n\n"),
|
||||||
R.compact, // remove last line
|
R.compact, // remove last line
|
||||||
R.map(
|
R.map((block) => block.split("\n").map((line) => eval(line)))
|
||||||
block => block.split("\n").map( line => eval(line) )
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
export const puzzleInput = readInput(import.meta.url, "input");
|
export const puzzleInput = readInput(import.meta.url, "input");
|
||||||
export const sample = readInput(import.meta.url, "sample");
|
export const sample = readInput(import.meta.url, "sample");
|
||||||
|
|
||||||
export function isLessThan(x,y) {
|
export function isLessThan(x, y) {
|
||||||
if( [x,y].every(R.isNumber) ) return x < y ? -1 : x == y ? 0 : 1;
|
if ([x, y].every(R.isNumber)) return x < y ? -1 : x == y ? 0 : 1;
|
||||||
|
|
||||||
if( [x,y].every(R.isArray) ) {
|
if ([x, y].every(R.isArray)) {
|
||||||
for ( const [a,b] of R.zip(x,y)) {
|
for (const [a, b] of R.zip(x, y)) {
|
||||||
const res = isLessThan(a,b);
|
const res = isLessThan(a, b);
|
||||||
if(res !== 0) return res;
|
if (res !== 0) return res;
|
||||||
}
|
|
||||||
if( x.length === y.length ) return 0
|
|
||||||
return x.length < y.length ? -1 : 1;
|
|
||||||
}
|
}
|
||||||
|
if (x.length === y.length) return 0;
|
||||||
|
return x.length < y.length ? -1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
return isLessThan(...[x,y].map( z => R.isNumber(z)? [z] : z ));
|
return isLessThan(...[x, y].map((z) => (R.isNumber(z) ? [z] : z)));
|
||||||
}
|
}
|
||||||
|
|
||||||
const passthru = (x) => (arg) => {
|
const passthru = (x) => (arg) => {
|
||||||
x(arg);
|
x(arg);
|
||||||
return arg;
|
return arg;
|
||||||
}
|
};
|
||||||
|
|
||||||
export default R.createPipe(
|
export default R.createPipe(
|
||||||
R.map( ([x,y]) => isLessThan(x,y) ),
|
R.map(([x, y]) => isLessThan(x, y)),
|
||||||
(results) => results.map(
|
(results) => results.map((value, index) => ({ value, index: index + 1 })),
|
||||||
(value,index) => ({ value, index: index+1 })
|
R.filter(({ value }) => value !== 1),
|
||||||
),
|
R.map(R.prop("index")),
|
||||||
R.filter( ({ value }) => value !== 1 ),
|
(results) => results.reduce((a, b) => a + b)
|
||||||
R.map( R.prop('index') ),
|
|
||||||
(results) => results.reduce((a,b) => a+b)
|
|
||||||
);
|
);
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import * as R from "remeda";
|
import * as R from "remeda";
|
||||||
import { isLessThan } from "./part1";
|
import { isLessThan } from "./part1";
|
||||||
|
|
||||||
const dividers = [[[2]],[[6]]];
|
const dividers = [[[2]], [[6]]];
|
||||||
export default R.createPipe(
|
export default R.createPipe(
|
||||||
R.flatten,
|
R.flatten,
|
||||||
R.concat(dividers),
|
R.concat(dividers),
|
||||||
(packets) => packets.sort(isLessThan),
|
(packets) => packets.sort(isLessThan),
|
||||||
packets => packets.map( (p,i) => ({ i: i+1, divider: dividers.includes(p) }) ),
|
(packets) =>
|
||||||
R.filter( ({divider}) => divider ),
|
packets.map((p, i) => ({ i: i + 1, divider: dividers.includes(p) })),
|
||||||
R.map(R.prop('i')),
|
R.filter(({ divider }) => divider),
|
||||||
(v) => v.reduce((a,b)=>a*b),
|
R.map(R.prop("i")),
|
||||||
|
(v) => v.reduce((a, b) => a * b)
|
||||||
);
|
);
|
||||||
|
@ -5,17 +5,16 @@ import part1, { sample, puzzleInput, isLessThan } from "./part1.js";
|
|||||||
import part2 from "./part2.js";
|
import part2 from "./part2.js";
|
||||||
|
|
||||||
describe("part 1", () => {
|
describe("part 1", () => {
|
||||||
test( "readInput", () => {
|
test("readInput", () => {
|
||||||
expect(sample[0][0]).toEqual([1,1,3,1,1]);
|
expect(sample[0][0]).toEqual([1, 1, 3, 1, 1]);
|
||||||
|
});
|
||||||
});
|
test("lessThan", () => {
|
||||||
test( "lessThan", () => {
|
expect(isLessThan(1, 2)).toBe(-1);
|
||||||
expect( isLessThan(1,2) ).toBe(-1);
|
expect(isLessThan(2, 1)).toBe(1);
|
||||||
expect( isLessThan(2,1) ).toBe(1);
|
expect(isLessThan(2, 2)).toBe(0);
|
||||||
expect( isLessThan(2,2) ).toBe(0);
|
expect(isLessThan(2, [1])).toBe(1);
|
||||||
expect( isLessThan(2,[1]) ).toBe(1);
|
expect(isLessThan([1], [2])).toBe(-1);
|
||||||
expect( isLessThan([1],[2]) ).toBe(-1);
|
});
|
||||||
});
|
|
||||||
test("sample", () => {
|
test("sample", () => {
|
||||||
expect(part1(sample)).toEqual(13);
|
expect(part1(sample)).toEqual(13);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user