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 .gitignore
input input
.prettierignore .prettierignore
sample sample*
input input
*.t *.t
*.pm *.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) => { const playTurn = (monkeys, monkey) => {
monkey.inspections += monkey.items.length; 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) => {
monkey.items.forEach( i => { monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
monkeys[(i % monkey.divisible) ? monkey.ifFalse : monkey.ifTrue i
].items.push(i) );
} ) });
monkey.items = []; monkey.items = [];
@ -55,17 +55,17 @@ const playTurn = (monkeys, monkey) => {
}; };
const playRound = (monkeys) => { const playRound = (monkeys) => {
monkeys.forEach( monkey => playTurn(monkeys,monkey) ); monkeys.forEach((monkey) => playTurn(monkeys, monkey));
return monkeys; return monkeys;
} };
export default R.createPipe( export default R.createPipe(
R.clone, R.clone,
(monkeys) => R.times(20, () => playRound(monkeys)), (monkeys) => R.times(20, () => playRound(monkeys)),
R.last(), R.last(),
R.map( R.prop('inspections') ), R.map(R.prop("inspections")),
R.sortBy(R.identity), R.sortBy(R.identity),
R.reverse, R.reverse,
R.take(2), 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"; import * as R from "remeda";
const playTurn = (monkeys, monkey) => { const playTurn = (max, monkeys, monkey) => {
const max = monkeys.map( R.prop('divisible') ).reduce(
(a,b) => a * b
);
monkey.inspections += monkey.items.length; monkey.inspections += monkey.items.length;
monkey.items = monkey.items.map( i => monkey.operation(i) ); monkey.items
monkey.items = monkey.items.map( i => i % max ); .map((i) => monkey.operation(i) % max)
.forEach((i) => {
monkey.items.forEach( i => { monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
monkeys[(i % monkey.divisible) ? monkey.ifFalse : monkey.ifTrue i
].items.push(i) );
} ) });
monkey.items = []; monkey.items = [];
return; return monkey;
}; };
const playRound = (monkeys) => { const genPlayRound = (monkeys) => {
monkeys.forEach( monkey => playTurn(monkeys,monkey) ); const max = monkeys.map(R.prop("divisible")).reduce((a, b) => a * b);
return monkeys;
} return () => monkeys.map((monkey) => playTurn(max, monkeys, monkey));
};
export default R.createPipe( export default R.createPipe(
R.clone, R.clone,
(monkeys) => R.times(10000, () => playRound(monkeys)), (monkeys) => R.times(10000, genPlayRound(monkeys)),
R.last(), R.last(),
R.map( R.prop('inspections') ), R.map(R.prop("inspections")),
R.sortBy(R.identity), R.sortBy(R.identity),
R.reverse, R.reverse,
R.take(2), R.take(2),
R.reduce((a, b) => a * b, 1) R.reduce((a, b) => a * b, 1)
) );

View File

@ -17,7 +17,7 @@ describe("part 1", () => {
}); });
describe.only("part 2", () => { describe.only("part 2", () => {
test.only( "sample", () => { test.skip("sample", () => {
expect(part2(sample)).toEqual(2713310158); expect(part2(sample)).toEqual(2713310158);
}); });
test("solution", () => { test("solution", () => {

View File

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