index.js (6529B)
1 #!/usr/bin/env node 2 3 const fs = require('node:fs'); 4 5 const part1Entries = fs 6 .readFileSync('input', 'utf-8') 7 .split('\r\n').join('\n') 8 .split('\r').join('\n') 9 .split('\n') 10 .filter(e => e) 11 .map(line => line.split(' ').filter(e => e)) 12 13 // Rotate the input 14 const part1problems = []; 15 for(let componentIndex = 0 ; componentIndex < part1Entries.length; componentIndex++) { 16 const row = part1Entries[componentIndex]; 17 for(let problemIndex = 0 ; problemIndex < row.length ; problemIndex++) { 18 part1problems[problemIndex]||={ components: [] }; 19 part1problems[problemIndex].components[componentIndex] = row[problemIndex]; 20 } 21 } 22 // Separate the operator & components 23 for(const problem of part1problems) { 24 problem.operator = problem.components.pop(); 25 problem.components = problem.components.map(BigInt); 26 } 27 28 const ops = { 29 '*': (a,b) => a*b, 30 '+': (a,b) => a+b, 31 }; 32 33 34 // Do math 35 for(const problem of part1problems) { 36 const start = problem.operator === '*' ? 1n : 0n; 37 problem.result = problem.components.reduce(ops[problem.operator], start); 38 console.log(`${problem.components.join(` ${problem.operator} `)} = ${problem.result}`); 39 } 40 41 // Sum the results 42 const part1 = part1problems.reduce((r,a) => r+a.result, 0n); 43 44 const part2raw = fs 45 .readFileSync('input', 'utf-8') 46 .split('\r\n').join('\n') 47 .split('\r').join('\n') 48 .split('\n') 49 .filter(e => e) 50 .map(line => line.split('')) 51 52 const part2rotated = []; 53 for(let y = 0 ; y < part2raw.length ; y++) { 54 const line = part2raw[y]; 55 for(let x = 0 ; x < line.length ; x++) { 56 part2rotated[x] ||= new Array(part2raw.length).fill(' ').map(_=>' '); 57 part2rotated[x][y] = line[x]; 58 } 59 } 60 61 const part2problems = []; 62 let buf = []; 63 64 function buf2problem(buf) { 65 return { 66 lines: buf.map(line => line.join('')), 67 operator: buf[0].slice(-1)[0], 68 components: buf.map(line => BigInt(line.slice(0,-1).join(''))) 69 }; 70 } 71 72 for(let line of part2rotated) { 73 if (line.join('').trim() === '') { 74 part2problems.push(buf2problem(buf)); 75 buf = []; 76 } else { 77 buf.push(line); 78 } 79 } 80 if (buf.length) { 81 part2problems.push(buf2problem(buf)); 82 } 83 84 // Do math 85 for(const problem of part2problems) { 86 const start = problem.operator === '*' ? 1n : 0n; 87 problem.result = problem.components.reduce(ops[problem.operator], start); 88 console.log(`${problem.components.join(` ${problem.operator} `)} = ${problem.result}`); 89 } 90 91 // Sum the results 92 const part2 = part2problems.reduce((r,a) => r+a.result, 0n); 93 94 // // Rotate the input 95 // const part1problems = []; 96 // for(let componentIndex = 0 ; componentIndex < part1Entries.length; componentIndex++) { 97 // const row = part1Entries[componentIndex]; 98 // for(let problemIndex = 0 ; problemIndex < row.length ; problemIndex++) { 99 // part1problems[problemIndex]||={ components: [] }; 100 // part1problems[problemIndex].components[componentIndex] = row[problemIndex]; 101 // } 102 // } 103 // // Separate the operator & components 104 // for(const problem of part1problems) { 105 // problem.operator = problem.components.pop(); 106 // problem.components = problem.components.map(BigInt); 107 // } 108 109 110 console.log(part2problems); 111 112 process.stdout.write(`------[ Part 1: ${part1} ]------\n`); 113 process.stdout.write(`------[ Part 2: ${part2} ]------\n`); 114 115 116 // console.log(JSON.stringify({ rawEntries, problems }, null, 2)); 117 118 119 // const ranges = rawRanges 120 // .split('\n') 121 // .filter(line => line) 122 // .map(line => line.split('-').map(n => BigInt(n))) 123 // .map(range => ({ start: range[0], end: range[1] })) 124 // ; 125 // 126 // const queries = rawQueries 127 // .split('\n') 128 // .filter(line => line) 129 // .map(line => BigInt(line)) 130 // ; 131 // 132 // const compositeRanges = ranges.map(range => ({...range})); 133 // 134 // for(let i = 0; i < compositeRanges.length; i++) { 135 // const range = compositeRanges[i]; 136 // 137 // // Find the first range this low one matches 138 // const foundLow = compositeRanges.find(({start,end}) => { 139 // if (start <= range.start && range.start <= end) return true; 140 // if (start <= range.end && range.end <= end) return true; 141 // if (range.start <= start && start <= range.end) return true; 142 // if (range.start <= end && end <= range.end) return true; 143 // return false; 144 // }); 145 // const foundLowIndex = compositeRanges.indexOf(foundLow); 146 // 147 // // Merge with low range 148 // if (i !== foundLowIndex) { 149 // // process.stdout.write(`Merging ${foundLow.start}-${foundLow.end},${range.start}-${range.end} => `); 150 // range.start = foundLow.start < range.start ? foundLow.start : range.start; 151 // range.end = foundLow.end > range.end ? foundLow.end : range.end ; 152 // // process.stdout.write(`${range.start}-${range.end}\n`); 153 // compositeRanges.splice(foundLowIndex, 1); 154 // i = -1; 155 // continue; 156 // } 157 // 158 // // Find the first range this high one matches 159 // const foundHigh = compositeRanges.find(({start,end}) => { 160 // if (start <= range.start && range.start <= end) return true; 161 // if (start <= range.end && range.end <= end) return true; 162 // if (range.start <= start && start <= range.end) return true; 163 // if (range.start <= end && end <= range.end) return true; 164 // return false; 165 // }); 166 // const foundHighIndex = compositeRanges.indexOf(foundHigh); 167 // 168 // // Merge with high range 169 // if (i !== foundHighIndex) { 170 // process.stdout.write(`Merging ${foundHigh.start}-${foundHigh.end},${range.start}-${range.end} => `); 171 // range.start = foundHigh.start < range.start ? foundHigh.start : range.start; 172 // range.end = foundHigh.end > range.end ? foundHigh.end : range.end ; 173 // process.stdout.write(`${range.start}-${range.end}\n`); 174 // compositeRanges.splice(foundHighIndex, 1); 175 // i = -1; 176 // continue; 177 // } 178 // } 179 // 180 // const part1 = queries 181 // .filter(ingredientId => compositeRanges.find(({start,end}) => { 182 // if (ingredientId < start) return false; 183 // if (ingredientId > end ) return false; 184 // return true; 185 // })) 186 // .length; 187 // 188 // const part2 = compositeRanges 189 // // .map(range => `${range.start}-${range.end} => ${range.end - range.start + 1n}`) 190 // .map(range => (range.end - range.start + 1n)) 191 // .reduce((r,a) => r+a, 0n) 192 // 193 // console.log(compositeRanges.map(range => `${range.start}-${range.end} => ${range.end - range.start + 1n}`)); 194 // console.log(compositeRanges.find(range => range.start > range.end)); 195 // 196 // // console.log(part2.join('\n')); 197 // 198 // process.stdout.write(`------[ Part 1: ${part1} ]------\n`); 199 // process.stdout.write(`------[ Part 2: ${part2} ]------\n`); 200 //