This commit is contained in:
Yanick Champoux 2022-12-11 19:32:35 -05:00
parent bbb79a5d61
commit 1497914171
6 changed files with 67 additions and 60 deletions

View File

@ -1,7 +1,7 @@
.gitignore
input
.prettierignore
sample
sample*
input
*.t
*.pm

9
2022/11/foo.bench.js Normal file
View File

@ -0,0 +1,9 @@
import { bench, test, expect, describe } from "vitest";
import { expectSolution } from "../01/main.js";
import part1, { sample, puzzleInput } from "./part1.js";
import part2 from "./part2.js";
bench("sample", () => {
part2(puzzleInput);
});

View File

@ -39,15 +39,15 @@ export const sample = readInput(import.meta.url, "sample");
const playTurn = (monkeys, monkey) => {
monkey.inspections += monkey.items.length;
monkey.items = monkey.items.map( i => monkey.operation(i) );
monkey.items = monkey.items.map((i) => monkey.operation(i));
monkey.items = monkey.items.map( i => Math.floor(i/3) );
monkey.items = monkey.items.map((i) => Math.floor(i / 3));
monkey.items.forEach( i => {
monkeys[(i % monkey.divisible) ? monkey.ifFalse : monkey.ifTrue
].items.push(i)
} )
monkey.items.forEach((i) => {
monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
i
);
});
monkey.items = [];
@ -55,17 +55,17 @@ const playTurn = (monkeys, monkey) => {
};
const playRound = (monkeys) => {
monkeys.forEach( monkey => playTurn(monkeys,monkey) );
monkeys.forEach((monkey) => playTurn(monkeys, monkey));
return monkeys;
}
};
export default R.createPipe(
R.clone,
(monkeys) => R.times(20, () => playRound(monkeys)),
R.last(),
R.map( R.prop('inspections') ),
R.sortBy( R.identity ),
R.map(R.prop("inspections")),
R.sortBy(R.identity),
R.reverse,
R.take(2),
R.reduce( (a,b) => a * b, 1 )
)
R.reduce((a, b) => a * b, 1)
);

View File

@ -1,37 +1,34 @@
import * as R from "remeda";
const playTurn = (monkeys, monkey) => {
const max = monkeys.map( R.prop('divisible') ).reduce(
(a,b) => a * b
);
const playTurn = (max, monkeys, monkey) => {
monkey.inspections += monkey.items.length;
monkey.items = monkey.items.map( i => monkey.operation(i) );
monkey.items = monkey.items.map( i => i % max );
monkey.items.forEach( i => {
monkeys[(i % monkey.divisible) ? monkey.ifFalse : monkey.ifTrue
].items.push(i)
} )
monkey.items
.map((i) => monkey.operation(i) % max)
.forEach((i) => {
monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
i
);
});
monkey.items = [];
return;
return monkey;
};
const playRound = (monkeys) => {
monkeys.forEach( monkey => playTurn(monkeys,monkey) );
return monkeys;
}
const genPlayRound = (monkeys) => {
const max = monkeys.map(R.prop("divisible")).reduce((a, b) => a * b);
return () => monkeys.map((monkey) => playTurn(max, monkeys, monkey));
};
export default R.createPipe(
R.clone,
(monkeys) => R.times(10000, () => playRound(monkeys)),
(monkeys) => R.times(10000, genPlayRound(monkeys)),
R.last(),
R.map( R.prop('inspections') ),
R.sortBy( R.identity ),
R.map(R.prop("inspections")),
R.sortBy(R.identity),
R.reverse,
R.take(2),
R.reduce( (a,b) => a * b, 1 )
)
R.reduce((a, b) => a * b, 1)
);

View File

@ -5,10 +5,10 @@ import part1, { sample, puzzleInput } from "./part1.js";
import part2 from "./part2.js";
describe("part 1", () => {
test( "readInput", () => {
expect(sample[0]).toMatchObject({divisible: 23, items: [ 79, 98 ]});
test("readInput", () => {
expect(sample[0]).toMatchObject({ divisible: 23, items: [79, 98] });
});
test( "sample", () => {
test("sample", () => {
expect(part1(sample)).toEqual(10605);
});
test("solution", () => {
@ -17,7 +17,7 @@ describe("part 1", () => {
});
describe.only("part 2", () => {
test.only( "sample", () => {
test.skip("sample", () => {
expect(part2(sample)).toEqual(2713310158);
});
test("solution", () => {

View File

@ -29,6 +29,7 @@ tasks:
download:
cmds:
- hygen day new --day {{.DAY_FORMATTED}}
- aoc download -d {{.DAY}} -o -p 2022/{{.DAY_FORMATTED}}/puzzle.md -i 2022/{{.DAY_FORMATTED}}/input
default: