Merge branch '2022-11'

main
Yanick Champoux 2022-12-11 19:38:16 -05:00
commit df843a7cbb
7 changed files with 81 additions and 75 deletions

View File

@ -1,7 +1,7 @@
.gitignore
input
.prettierignore
sample
sample*
input
*.t
*.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");
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)
);

View File

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

View File

@ -1,9 +1,8 @@
\--- Day 11: Monkey in the Middle ---
----------
## \--- Day 11: Monkey in the Middle ---
As you finally start making your way upriver, you realize your pack is much lighter than you remember. Just then, one of the items from your pack goes flying overhead. Monkeys are playing [Keep Away](https://en.wikipedia.org/wiki/Keep_away) with your missing things!
To get your stuff back, you need to be able to predict where the monkeys will throw your items. After some careful observation, you realize the monkeys operate based on *how worried you are about each item*.
To get your stuff back, you need to be able to predict where the monkeys will throw your items. After some careful observation, you realize the monkeys operate based on _how worried you are about each item_.
You take some notes (your puzzle input) on the items each monkey currently has, how worried you are about those items, and how the monkey makes decisions based on your worry level. For example:
@ -40,17 +39,17 @@ Monkey 3:
Each monkey has several attributes:
* `Starting items` lists your *worry level* for each item the monkey is currently holding in the order they will be inspected.
* `Operation` shows how your worry level changes as that monkey inspects an item. (An operation like `new = old * 5` means that your worry level after the monkey inspected the item is five times whatever your worry level was before inspection.)
* `Test` shows how the monkey uses your worry level to decide where to throw an item next.
* `If true` shows what happens with an item if the `Test` was true.
* `If false` shows what happens with an item if the `Test` was false.
- `Starting items` lists your _worry level_ for each item the monkey is currently holding in the order they will be inspected.
- `Operation` shows how your worry level changes as that monkey inspects an item. (An operation like `new = old * 5` means that your worry level after the monkey inspected the item is five times whatever your worry level was before inspection.)
- `Test` shows how the monkey uses your worry level to decide where to throw an item next.
- `If true` shows what happens with an item if the `Test` was true.
- `If false` shows what happens with an item if the `Test` was false.
After each monkey inspects an item but before it tests your worry level, your relief that the monkey's inspection didn't damage the item causes your worry level to be *divided by three* and rounded down to the nearest integer.
After each monkey inspects an item but before it tests your worry level, your relief that the monkey's inspection didn't damage the item causes your worry level to be _divided by three_ and rounded down to the nearest integer.
The monkeys take turns inspecting and throwing items. On a single monkey's *turn*, it inspects and throws all of the items it is holding one at a time and in the order listed. Monkey `0` goes first, then monkey `1`, and so on until each monkey has had one turn. The process of each monkey taking a single turn is called a *round*.
The monkeys take turns inspecting and throwing items. On a single monkey's _turn_, it inspects and throws all of the items it is holding one at a time and in the order listed. Monkey `0` goes first, then monkey `1`, and so on until each monkey has had one turn. The process of each monkey taking a single turn is called a _round_.
When a monkey throws an item to another monkey, the item goes on the *end* of the recipient monkey's list. A monkey that starts a round with no items could end up inspecting and throwing many items by the time its turn comes around. If a monkey is holding no items at the start of its turn, its turn ends.
When a monkey throws an item to another monkey, the item goes on the _end_ of the recipient monkey's list. A monkey that starts a round with no items could end up inspecting and throwing many items by the time its turn comes around. If a monkey is holding no items at the start of its turn, its turn ends.
In the above example, the first round proceeds as follows:
@ -219,7 +218,7 @@ Monkey 3:
```
Chasing all of the monkeys at once is impossible; you're going to have to focus on the *two most active* monkeys if you want any hope of getting your stuff back. Count the *total number of times each monkey inspects items* over 20 rounds:
Chasing all of the monkeys at once is impossible; you're going to have to focus on the _two most active_ monkeys if you want any hope of getting your stuff back. Count the _total number of times each monkey inspects items_ over 20 rounds:
```
Monkey 0 inspected items 101 times.
@ -229,12 +228,12 @@ Monkey 3 inspected items 105 times.
```
In this example, the two most active monkeys inspected items 101 and 105 times. The level of *monkey business* in this situation can be found by multiplying these together: `*10605*`.
In this example, the two most active monkeys inspected items 101 and 105 times. The level of _monkey business_ in this situation can be found by multiplying these together: `*10605*`.
Figure out which monkeys to chase by counting how many items they inspect over 20 rounds. *What is the level of monkey business after 20 rounds of stuff-slinging simian shenanigans?*
Figure out which monkeys to chase by counting how many items they inspect over 20 rounds. _What is the level of monkey business after 20 rounds of stuff-slinging simian shenanigans?_
To begin, [get your puzzle input](11/input).
Answer:
You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Monkey+in+the+Middle%22+%2D+Day+11+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F11&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle.
You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Monkey+in+the+Middle%22+%2D+Day+11+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F11&related=ericwastl&hashtags=AdventOfCode) [Mastodon](<javascript:void(0);>)] this puzzle.

View File

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

View File

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