From 149791417105c16f29994a5c7e2ae5de28f59152 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 11 Dec 2022 19:32:35 -0500 Subject: [PATCH] day 11 --- .prettierignore | 2 +- 2022/11/foo.bench.js | 9 ++++++++ 2022/11/part1.js | 44 ++++++++++++++++++------------------ 2022/11/part2.js | 53 +++++++++++++++++++++----------------------- 2022/11/test.js | 18 +++++++-------- Taskfile.yaml | 1 + 6 files changed, 67 insertions(+), 60 deletions(-) create mode 100644 2022/11/foo.bench.js diff --git a/.prettierignore b/.prettierignore index 69eb8c3..627f9d1 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,7 +1,7 @@ .gitignore input .prettierignore -sample +sample* input *.t *.pm diff --git a/2022/11/foo.bench.js b/2022/11/foo.bench.js new file mode 100644 index 0000000..15ed8c1 --- /dev/null +++ b/2022/11/foo.bench.js @@ -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); +}); diff --git a/2022/11/part1.js b/2022/11/part1.js index 4779db4..a564df3 100644 --- a/2022/11/part1.js +++ b/2022/11/part1.js @@ -37,35 +37,35 @@ export const puzzleInput = readInput(import.meta.url, "input"); export const sample = readInput(import.meta.url, "sample"); 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 => { - monkeys[(i % monkey.divisible) ? monkey.ifFalse : monkey.ifTrue - ].items.push(i) - } ) + monkey.items = []; - monkey.items = []; - - return; + return; }; const playRound = (monkeys) => { - monkeys.forEach( monkey => playTurn(monkeys,monkey) ); - return monkeys; -} + 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.reverse, - R.take(2), - R.reduce( (a,b) => a * b, 1 ) -) + R.clone, + (monkeys) => R.times(20, () => playRound(monkeys)), + R.last(), + R.map(R.prop("inspections")), + R.sortBy(R.identity), + R.reverse, + R.take(2), + R.reduce((a, b) => a * b, 1) +); diff --git a/2022/11/part2.js b/2022/11/part2.js index 8035a4d..1c4acc7 100644 --- a/2022/11/part2.js +++ b/2022/11/part2.js @@ -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.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.map( i => i % max ); + monkey.items = []; - monkey.items.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)), - R.last(), - R.map( R.prop('inspections') ), - R.sortBy( R.identity ), - R.reverse, - R.take(2), - R.reduce( (a,b) => a * b, 1 ) -) + R.clone, + (monkeys) => R.times(10000, genPlayRound(monkeys)), + R.last(), + R.map(R.prop("inspections")), + R.sortBy(R.identity), + R.reverse, + R.take(2), + R.reduce((a, b) => a * b, 1) +); diff --git a/2022/11/test.js b/2022/11/test.js index 12cf53e..2648138 100644 --- a/2022/11/test.js +++ b/2022/11/test.js @@ -5,21 +5,21 @@ 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( "sample", () => { - expect(part1(sample)).toEqual(10605); - }); + test("readInput", () => { + expect(sample[0]).toMatchObject({ divisible: 23, items: [79, 98] }); + }); + test("sample", () => { + expect(part1(sample)).toEqual(10605); + }); test("solution", () => { expectSolution(part1(puzzleInput)).toEqual(58794); }); }); describe.only("part 2", () => { - test.only( "sample", () => { - expect(part2(sample)).toEqual(2713310158); - }); + test.skip("sample", () => { + expect(part2(sample)).toEqual(2713310158); + }); test("solution", () => { expectSolution(part2(puzzleInput)).toBeLessThan(32239420356); expectSolution(part2(puzzleInput)).toEqual(20151213744); diff --git a/Taskfile.yaml b/Taskfile.yaml index eaa296d..f36a11b 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -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: