part 2
This commit is contained in:
parent
99e244c054
commit
bbb79a5d61
71
2022/11/part1.js
Normal file
71
2022/11/part1.js
Normal 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
37
2022/11/part2.js
Normal 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
28
2022/11/sample
Normal 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
27
2022/11/test.js
Normal 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);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user