Skip to content

Commit

Permalink
feat(2023-20): solve part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
leyanlo committed Dec 20, 2023
1 parent 67ea4e2 commit b0c56f2
Showing 1 changed file with 98 additions and 7 deletions.
105 changes: 98 additions & 7 deletions 2023/day-20.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,90 @@ var input = `broadcaster -> a
%b -> con
&con -> output`;
var input = readFileSync('./day-20-input.txt', 'utf8').trimEnd();
// 625185522 too low
// 239918617350720 too low

// function solve(input) {
// // console.log(input);
// const map = {};
// const initialQueue = [];
// for (const line of input.split('\n')) {
// let [l, r] = line.split(' -> ');
// if (l === 'broadcaster') {
// initialQueue.push(
// ...r.split(', ').map((module) => ({ module, pulse: 0, from: 'button' }))
// );
// } else if (l[0] === '%') {
// map[l.substring(1)] = {
// kind: 'ff',
// outputs: r.split(', '),
// state: 0,
// };
// } else {
// map[l.substring(1)] = {
// kind: 'conj',
// outputs: r.split(', '),
// inputs: [],
// };
// }
// }
//
// for (const module in map) {
// const outputs = map[module].outputs;
// for (const output of outputs) {
// if (map[output]?.kind === 'conj') {
// map[output].inputs.push({ module, state: 0 });
// }
// }
// }
// // console.log('map', map);
// // console.log('initialQueue', initialQueue);
//
// const nPulses = [0, 0];
// for (let i = 0; i < 1000; i++) {
// const queue = [...initialQueue];
// nPulses[0]++;
// while (queue.length) {
// const { module, pulse, from } = queue.shift();
// nPulses[pulse]++;
// const input = map[module];
// if (!input) continue;
//
// if (input.kind === 'ff') {
// if (!pulse) {
// input.state = +!input.state;
// queue.push(
// ...input.outputs.map((m) => ({
// module: m,
// pulse: input.state,
// from: module,
// }))
// );
// }
// } else {
// input.inputs = input.inputs.map((i) =>
// i.module === from
// ? {
// module: from,
// state: pulse,
// }
// : i
// );
// queue.push(
// ...input.outputs.map((m) => ({
// module: m,
// pulse: +!input.inputs.every((i) => i.state),
// from: module,
// }))
// );
// }
// }
// // console.log('map', map);
// // console.log('nPulses', nPulses);
// }
// console.log('nPulses', nPulses);
// console.log(nPulses[0] * nPulses[1]);
// }
// solve(input);
function solve(input) {
// console.log(input);
const map = {};
Expand Down Expand Up @@ -49,13 +131,21 @@ function solve(input) {
// console.log('map', map);
// console.log('initialQueue', initialQueue);

const nPulses = [0, 0];
for (let i = 0; i < 1000; i++) {
// these lead to gq
const buttonCounts = {
xj: null,
qs: null,
kz: null,
km: null,
};
let i = 0;
while (!Object.values(buttonCounts).every(Boolean)) {
const queue = [...initialQueue];
nPulses[0]++;
while (queue.length) {
const { module, pulse, from } = queue.shift();
nPulses[pulse]++;
if (module in buttonCounts && !pulse) {
buttonCounts[module] ||= i;
}
const input = map[module];
if (!input) continue;

Expand Down Expand Up @@ -90,8 +180,9 @@ function solve(input) {
}
// console.log('map', map);
// console.log('nPulses', nPulses);
i++;
}
console.log('nPulses', nPulses);
console.log(nPulses[0] * nPulses[1]);
console.log('buttonCounts', buttonCounts);
console.log(Object.values(buttonCounts).reduce((acc, n) => acc * (n + 1), 1));
}
solve(input);

0 comments on commit b0c56f2

Please sign in to comment.