index.js (4135B)
1 #!/usr/bin/env node 2 3 const fs = require('node:fs'); 4 5 const connectionlimit = 10; 6 7 const input = fs 8 .readFileSync('input', 'utf-8') 9 .split('\r\n').join('\n') 10 .split('\r').join('\n') 11 .split('\n') 12 .filter(e => e) 13 .map(line => line.split(',').map(n => parseInt(n,10))) 14 15 const part1circuits = []; 16 const part1map = []; 17 const part1boxes = input.map(pos => ({ pos, circuit: null })) 18 19 // Map out all possible connections 20 for(let idx_src = 0 ; idx_src < input.length ; idx_src++) { 21 for(let idx_dst = idx_src + 1 ; idx_dst < input.length ; idx_dst++) { 22 const ref = { 23 key: `${idx_src}:${idx_dst}`, 24 src: part1boxes[idx_src], 25 dst: part1boxes[idx_dst], 26 }; 27 ref.distance = Math.sqrt( 28 ((ref.src.pos[0]-ref.dst.pos[0])**2) + 29 ((ref.src.pos[1]-ref.dst.pos[1])**2) + 30 ((ref.src.pos[2]-ref.dst.pos[2])**2) 31 ); 32 part1map.push(ref); 33 } 34 } 35 36 // Sort all possible connections 37 part1map.sort((a,b) => a.distance - b.distance); 38 39 // Let's lay the ones we need 40 let part1connections = 0; 41 42 let part2_lastsrc = null; 43 let part2_lastdst = null; 44 45 for(let candidate of part1map) { 46 47 // Print out part1 when limit reached 48 if (part1connections == connectionlimit) { 49 // Sort circuits by size 50 part1circuits.sort((a,b) => b.length - a.length); 51 52 const part1 = part1circuits 53 .slice(0,3) 54 .map(circuit => circuit.length) 55 .reduce((r,a) => r*a, 1) 56 ; 57 process.stdout.write(`------[ Part 1: ${part1} ]------\n`); 58 } 59 60 // Neither src/dst in circuit = new circuit 61 if ( 62 (!candidate.src.circuit) && 63 (!candidate.dst.circuit) 64 ) { 65 part2_lastsrc = candidate.src; 66 part2_lastdst = candidate.dst; 67 const circuit = []; 68 part1circuits.push(circuit); 69 candidate.src.circuit = circuit; 70 candidate.dst.circuit = circuit; 71 circuit.push(candidate.src); 72 circuit.push(candidate.dst); 73 part1connections++; 74 continue; 75 } 76 77 // src has circuit, dst has no circuit 78 if ( 79 ( candidate.src.circuit) && 80 (!candidate.dst.circuit) 81 ) { 82 part2_lastsrc = candidate.src; 83 part2_lastdst = candidate.dst; 84 candidate.dst.circuit = candidate.src.circuit; 85 candidate.src.circuit.push(candidate.dst); 86 part1connections++; 87 continue; 88 } 89 90 // dst has circuit, src has no circuit 91 if ( 92 ( candidate.dst.circuit) && 93 (!candidate.src.circuit) 94 ) { 95 part2_lastsrc = candidate.src; 96 part2_lastdst = candidate.dst; 97 candidate.src.circuit = candidate.dst.circuit; 98 candidate.dst.circuit.push(candidate.src); 99 part1connections++; 100 continue; 101 } 102 103 // Both have circuits, NOT the same circuit 104 if ( 105 ( candidate.src.circuit) && 106 ( candidate.dst.circuit) && 107 ( candidate.src.circuit !== candidate.dst.circuit ) 108 ) { 109 part2_lastsrc = candidate.src; 110 part2_lastdst = candidate.dst; 111 const circuit = []; 112 const circuit_src = candidate.src.circuit; 113 const circuit_dst = candidate.dst.circuit; 114 for(const box of candidate.src.circuit) { 115 box.circuit = circuit; 116 circuit.push(box); 117 } 118 for(const box of candidate.dst.circuit) { 119 box.circuit = circuit; 120 circuit.push(box); 121 } 122 part1circuits.push(circuit); 123 part1circuits.splice(part1circuits.indexOf(circuit_src), 1); 124 part1circuits.splice(part1circuits.indexOf(circuit_dst), 1); 125 part1connections++; 126 continue; 127 } 128 129 // Both have circuits, within same circuit 130 if ( 131 ( candidate.src.circuit) && 132 ( candidate.dst.circuit) && 133 ( candidate.src.circuit === candidate.dst.circuit ) 134 ) { 135 // Do nothing 136 part1connections++; 137 continue; 138 } 139 140 // console.log(candidate); 141 throw new Error("Unhandled case"); 142 } 143 144 const part2 = part2_lastsrc.pos[0] * part2_lastdst.pos[0]; 145 146 process.stdout.write(`------[ Part 2: ${part2} ]------\n`); 147 148 // console.log(part1circuits); 149 // console.log({ 150 // part2_lastsrc, 151 // part2_lastdst 152 // }); 153 154 // // Sort circuits by size 155 // part1circuits.sort((a,b) => b.length - a.length); 156 157 // const part1 = part1circuits 158 // .slice(0,3) 159 // .map(circuit => circuit.length) 160 // .reduce((r,a) => r*a, 1) 161 // ; 162 163 164 165 166 167 168 169 // process.stdout.write(`------[ Part 1: ${part1} ]------\n`);