This commit is contained in:
Yanick Champoux 2022-12-11 13:30:44 -05:00
parent 99e244c054
commit bbb79a5d61
4 changed files with 163 additions and 0 deletions

71
2022/11/part1.js Normal file
View File

@ -0,0 +1,71 @@
import * as R from "remeda";
import { readFile } from "../05/part1.js";
function parseMonkey(block) {
const re = new RegExp(
`Starting items: (?<items>.+)
Operation: new = (?<operation>.+)
Test: divisible by (?<divisible>\\d+)
If true: throw to monkey (?<ifTrue>\\d+)
If false: throw to monkey (?<ifFalse>\\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 )
)

37
2022/11/part2.js Normal file
View File

@ -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 )
)

28
2022/11/sample Normal file
View File

@ -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

27
2022/11/test.js Normal file
View File

@ -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);
});
});