Skip to content

Instantly share code, notes, and snippets.

@binkiklou
Created December 8, 2023 20:49
Show Gist options
  • Select an option

  • Save binkiklou/e7f7c0a37381744e18b38d7fdffe7d75 to your computer and use it in GitHub Desktop.

Select an option

Save binkiklou/e7f7c0a37381744e18b38d7fdffe7d75 to your computer and use it in GitHub Desktop.
kinda hacky aoc 2023 day 8 part 2
# lol
import numpy as np
arr = np.array([ ])
x = np.lcm.reduce(arr)
print(x)
const fs = require('fs');
let instrs = [];
// ID:[LEFT, RIGHT, at count]
let nodes = {};
let cursors = [];
// Parsing
{
let lines = fs.readFileSync('input.txt','utf8').split('\n');
for(c of lines[0]){
instrs.push(c);
}
for(let i = 1; i < lines.length; i++){
let tokens = lines[i].match(/\w+/gm);
//if(cursor == undefined){cursor = tokens[0];}
nodes[tokens[0]] = [tokens[1], tokens[2], 0];
}
}
function resetLoopCounters(){
for(n in nodes){
nodes[n][2] = 0;
}
}
cursors = Object.keys(nodes).filter((key) => {if(key[key.length-1] == 'A'){return true;}return false;});
zcount = cursors.length;
console.log(cursors);
let paths = {};
// Compute paths for each cursors
{
// node:[first loop point(step), potential loops]
for(c in cursors){
let firsts = {}; // node:[step]
let potentials = {}; // every potential looping node(only ending with Z): node:[step]
let cursor = cursors[c];
let looped = false;
let i = 0;
let steps = 0;
console.log(`Now at cursor ${c}`);
while(!looped){
if(i == instrs.length){
i = 0;
}
//console.log(`${steps}(${i}):${cursor}`);
if(cursor.endsWith('Z'))
{
if(firsts[cursor] && firsts[cursor].filter((v) => { if(steps%v==0){return true;}return false; }).length != 0){}
else if(potentials[cursor]){
// fitting paths
let indexes = [];
let fitting = potentials[cursor].filter((v,i) => { if(steps%v==0){indexes.push(i); return true;}return false; });
if(fitting.length == 0){
console.log(`Identified potential for ${cursor} at ${steps}`);
potentials[cursor].push(steps);
}
else if(fitting.length == 1)
{
if(!firsts[cursor]){
firsts[cursor] = [];
}
firsts[cursor].push(fitting[0]);
potentials[cursor].splice(indexes[0], 1);
console.log('Confirmed fitting:', cursor);
}
else{
console.log(`wtf too many fitting steps ${cursor}`);
}
}
else{
console.log(`Identified first potential for ${cursor} at ${steps}`);
potentials[cursor] = [steps];
}
}
// very sophisticated loop detection (c^:)
nodes[cursor][2] += 1;
if((nodes[cursor][0] == cursor) && (nodes[cursor][1] == cursor)){
console.log(`Loop(${cursor}): Children are itself`);
// fuck it this is hacky
if(cursor.endsWith('Z') && potentials[cursor].length > 0){
if(!firsts[cursor]){firsts[cursor] = [];}
firsts[cursor].push(potentials[cursor][0]);
console.log('HACKY EXIT1!!!');
}
looped = true;
}
else if(
firsts[cursor] &&
( (firsts[cursor].length == 1 && nodes[cursor][2] > 10) ||
(firsts[cursor].length > 1 && nodes[cursor][2] > 3))){
console.log(firsts.length);
console.log(`Loop(${cursor}) Encountered too many times`);
looped = true;
}
else if(Math.abs(nodes[nodes[cursor][0]] - nodes[nodes[cursor][1]]) > 5){
console.log(`Loop(${cursor}): Node clearly takes one path ${nodes[nodes[cursor][0]][2]}: ${nodes[nodes[cursor][1]][2]}`);
looped = true;
}
if(instrs[i] == 'L'){cursor = nodes[cursor][0];}
else{cursor = nodes[cursor][1];}
i++;
steps++;
}
resetLoopCounters();
console.log(firsts);
paths[c] = firsts; // fuck it theres never more than 1 element anyway
}
}
let yeet = [];
console.log(paths);
for(a in paths){for(b in paths[a]){yeet.push(paths[a][b][0]);}}
console.log(yeet);
console.log('you should edit and run "python hack.py" now');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment