Advent of Code 2022: Day 11
monkey math game 🐒
by Amy L
HTML
<link rel="stylesheet" href="https://adventofcode.com/static/style.css?30">
<h1><a href="https://adventofcode.com/2022/day/11" target="_blank">Day 11</a></h1>
<diV class="puzzle-input">
<label for="INPUT_DATA">Input</label>
<textarea id="INPUT_DATA" autocomplete="off" placeholder="paste your input here" rows="7" cols="50"></textarea>
</diV>
<dl>
<dt>Part 1</dt>
<dd>
<label>Answer:
<input type="text" id="answer1" readonly />
</label>
</dd>
<dt>Part 2</dt>
<dd>
<label>Answer:
<input type="text" id="answer2" readonly />
</label>
</dd>
</dl>
JavaScript
const OPERATIONS = {
DIVISIBLE: (operand1, operand2) => {
return operand1 % operand2 === 0;
},
ADD: (operand1, operand2) => {
return operand1 + operand2;
},
MULTIPLY: (operand1, operand2) => {
return operand1 * operand2;
}
};
const SYMBOL_MAP = {
'divisible': OPERATIONS.DIVISIBLE,
'*': OPERATIONS.MULTIPLY,
'+': OPERATIONS.ADD
};
function Day11(inputData) {
function solvePart1(input) {
const monkeys = parseInputData(input);
const tally = Array(monkeys.length).fill(0);
for (let i = 0; i < 20; i++) {
monkeysPlayRound(monkeys, tally, reliefMethod);
}
return calculateMonkeyBusiness(tally);
}
function solvePart2(input) {
const monkeys = parseInputData(input);
const tally = Array(monkeys.length).fill(0);
const product = getProductOfPrimeFactors(monkeys);
const rounds = 10000;
for (let i = 1; i <= rounds; i++) {
monkeysPlayRound(monkeys, tally, anotherReliefMethod, [product]);
if (i === 1 || i === 20 || i % Math.ceil(Math.sqrt(rounds)) === 0) {
console.log(`After round ${i}`);
tally.forEach((num, index) => {
console.log(`Monkey ${index} inspected items ${num} times.`)
})
}
}
return calculateMonkeyBusiness(tally);
};
const part1 = solvePart1(inputData);
const part2 = solvePart2(inputData);
return {part1, part2};
}
document.addEventListener('DOMContentLoaded', () => {
getInputData('INPUT_DATA', (input) => {
const answers = Day11(input);
const [answer1El, answer2El] = [
document.getElementById('answer1'),
document.getElementById('answer2')
];
answer1El.value = answers.part1;
answer2El.value = answers.part2;
});
});
/*******************************************************************
Utility libs
**/
function LOG() {
const enabled = false;
if (enabled) {
console.log.apply(null, arguments);
}
}
function getInputData(inputId, callback) {
const dataEl = document.getElementById(inputId);
...