Merge branch '2022-11'

This commit is contained in:
Yanick Champoux 2022-12-11 19:38:16 -05:00
commit dc11ceb117
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

@ -37,35 +37,35 @@ export const puzzleInput = readInput(import.meta.url, "input");
export const sample = readInput(import.meta.url, "sample"); 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) => {
monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
i
);
});
monkey.items.forEach( i => { monkey.items = [];
monkeys[(i % monkey.divisible) ? monkey.ifFalse : monkey.ifTrue
].items.push(i)
} )
monkey.items = []; return;
return;
}; };
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( monkey.inspections += monkey.items.length;
(a,b) => a * b
);
monkey.inspections += monkey.items.length; monkey.items
.map((i) => monkey.operation(i) % max)
.forEach((i) => {
monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
i
);
});
monkey.items = monkey.items.map( i => monkey.operation(i) ); monkey.items = [];
monkey.items = monkey.items.map( i => i % max );
monkey.items.forEach( i => { return monkey;
monkeys[(i % monkey.divisible) ? monkey.ifFalse : monkey.ifTrue
].items.push(i)
} )
monkey.items = [];
return;
}; };
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

@ -5,21 +5,21 @@ import part1, { sample, puzzleInput } 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]).toMatchObject({divisible: 23, items: [ 79, 98 ]}); expect(sample[0]).toMatchObject({ divisible: 23, items: [79, 98] });
}); });
test( "sample", () => { test("sample", () => {
expect(part1(sample)).toEqual(10605); expect(part1(sample)).toEqual(10605);
}); });
test("solution", () => { test("solution", () => {
expectSolution(part1(puzzleInput)).toEqual(58794); expectSolution(part1(puzzleInput)).toEqual(58794);
}); });
}); });
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", () => {
expectSolution(part2(puzzleInput)).toBeLessThan(32239420356); expectSolution(part2(puzzleInput)).toBeLessThan(32239420356);
expectSolution(part2(puzzleInput)).toEqual(20151213744); expectSolution(part2(puzzleInput)).toEqual(20151213744);

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: