From bbb79a5d61b500d70f55cdd3ab3e440ddc8f0154 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 11 Dec 2022 13:30:44 -0500 Subject: [PATCH] part 2 --- 2022/11/part1.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 2022/11/part2.js | 37 +++++++++++++++++++++++++ 2022/11/sample | 28 +++++++++++++++++++ 2022/11/test.js | 27 ++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 2022/11/part1.js create mode 100644 2022/11/part2.js create mode 100644 2022/11/sample create mode 100644 2022/11/test.js diff --git a/2022/11/part1.js b/2022/11/part1.js new file mode 100644 index 0000000..4779db4 --- /dev/null +++ b/2022/11/part1.js @@ -0,0 +1,71 @@ +import * as R from "remeda"; + +import { readFile } from "../05/part1.js"; + +function parseMonkey(block) { + const re = new RegExp( + `Starting items: (?.+) + Operation: new = (?.+) + Test: divisible by (?\\d+) + If true: throw to monkey (?\\d+) + If false: throw to monkey (?\\d+)`, + "s" + ); + + const groups = { ...block.match(re).groups }; + + ["divisible", "ifTrue", "ifFalse"].forEach( + (k) => (groups[k] = parseInt(groups[k])) + ); + + groups.operation = eval("(old) => " + groups.operation); + + groups.items = groups.items.split(", ").map((x) => parseInt(x)); + + return { ...groups, inspections: 0 }; +} + +const readInput = (...args) => + R.pipe( + readFile(...args), + (lines) => lines.split("\n\n"), + R.compact, // remove last line + R.map(parseMonkey) + ); + +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.items = monkey.items.map( i => monkey.operation(i) ); + + 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 = []; + + return; +}; + +const playRound = (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 ) +) diff --git a/2022/11/part2.js b/2022/11/part2.js new file mode 100644 index 0000000..8035a4d --- /dev/null +++ b/2022/11/part2.js @@ -0,0 +1,37 @@ +import * as R from "remeda"; + +const playTurn = (monkeys, monkey) => { + const max = monkeys.map( R.prop('divisible') ).reduce( + (a,b) => a * b + ); + + 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 = []; + + return; +}; + +const playRound = (monkeys) => { + monkeys.forEach( monkey => playTurn(monkeys,monkey) ); + return monkeys; +} + +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 ) +) diff --git a/2022/11/sample b/2022/11/sample new file mode 100644 index 0000000..477d33b --- /dev/null +++ b/2022/11/sample @@ -0,0 +1,28 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 + diff --git a/2022/11/test.js b/2022/11/test.js new file mode 100644 index 0000000..12cf53e --- /dev/null +++ b/2022/11/test.js @@ -0,0 +1,27 @@ +import { test, expect, describe } from "vitest"; + +import { expectSolution } from "../01/main.js"; +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("solution", () => { + expectSolution(part1(puzzleInput)).toEqual(58794); + }); +}); + +describe.only("part 2", () => { + test.only( "sample", () => { + expect(part2(sample)).toEqual(2713310158); + }); + test("solution", () => { + expectSolution(part2(puzzleInput)).toBeLessThan(32239420356); + expectSolution(part2(puzzleInput)).toEqual(20151213744); + }); +});