Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 20, 2023
1 parent 1b7461b commit f482447
Showing 1 changed file with 29 additions and 45 deletions.
74 changes: 29 additions & 45 deletions src/2023/day20.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
function parse(input) {
let parts = input.split('\n').map(line => {
let [part, connectors] = line.split(' -> ');
let type =
part[0] === '%' ? 'flip' : part[0] === '&' ? 'conj' : 'broadcaster';
if (type !== 'broadcaster') {
part = part.slice(1);
}
connectors = connectors.split(', ');
return [part, { part, type, connectors }];
let [part, links] = line.split(' -> ');
let type = part[0];
if (type !== 'b') part = part.slice(1);
links = links.split(', ');
return [part, { part, type, links }];
});
parts = Object.fromEntries(parts);
Object.values(parts).forEach(part => {
if (part.type === 'flip') part.state = false;
if (part.type === 'conj') {
part.memory = {};
Object.values(parts).forEach(x => {
if (x.connectors.includes(part.part)) {
part.memory[x.part] = 'low';
}
});
part.memory;
if (part.type === '%') part.state = 'low';
if (part.type === '&') {
const linked = Object.values(parts).filter(x =>
x.links.includes(part.part),
);
part.memory = Object.fromEntries(linked.map(x => [x.part, 'low']));
}
});
return parts;
Expand All @@ -34,42 +28,33 @@ function signal(parts, wait) {

if (`${part},${level},${from}` === wait) throw new Error('wait');
if (!parts[part]) continue;
if (parts[part].type === 'broadcaster') {
parts[part].connectors.forEach(connector => {
queue.push({ part: connector, level, from: part });
if (parts[part].type === 'b') {
parts[part].links.forEach(link => {
queue.push({ part: link, level, from: part });
});
}
if (parts[part].type === 'flip' && level === 'low') {
parts[part].state = !parts[part].state;
parts[part].connectors.forEach(connector => {
queue.push({
part: connector,
level: parts[part].state ? 'high' : 'low',
from: part,
});
if (parts[part].type === '%' && level === 'low') {
parts[part].state = parts[part].state === 'low' ? 'high' : 'low';
parts[part].links.forEach(link => {
queue.push({ part: link, level: parts[part].state, from: part });
});
}
if (parts[part].type === 'conj') {
if (parts[part].type === '&') {
parts[part].memory[from] = level;
const allHigh = Object.values(parts[part].memory).every(
x => x === 'high',
);
parts[part].connectors.forEach(connector => {
queue.push({
part: connector,
level: allHigh ? 'low' : 'high',
from: part,
});
parts[part].links.forEach(link => {
queue.push({ part: link, level: allHigh ? 'low' : 'high', from: part });
});
}
}
return count;
}

export function part1(input) {
let parts = parse(input);
let count = { low: 0, high: 0 };

const parts = parse(input);
const count = { low: 0, high: 0 };
for (let i = 0; i < 1000; i++) {
const result = signal(parts);
count.low += result.low;
Expand All @@ -79,7 +64,7 @@ export function part1(input) {
}

function wait(input, part, from) {
let parts = parse(input);
const parts = parse(input);
for (let i = 1; i < Infinity; i++) {
try {
signal(parts, `${part},high,${from}`);
Expand All @@ -88,11 +73,10 @@ function wait(input, part, from) {
}
}
}

export function part2(input) {
let parts = parse(input);
const conj = Object.values(parts).find(x => x.connectors[0] === 'rx');
return Object.keys(conj.memory).reduce(
(acc, key) => acc * wait(input, conj.part, key),
1,
);
const parts = parse(input);
const conj = Object.values(parts).find(x => x.links[0] === 'rx');
const loops = Object.keys(conj.memory).map(x => wait(input, conj.part, x));
return loops.reduce((acc, x) => acc * x, 1);
}

0 comments on commit f482447

Please sign in to comment.