advent of code 2025 in ts and nix
at main 1.5 kB view raw
1const file = await Bun.file("../../shared/03/input.txt").text(); 2const banks = file.split("\n"); 3 4(() => { 5 let jolts = 0; 6 for (const bank of banks) { 7 const L = bank.length; 8 9 let highestPair = -1; 10 for (let j = 1; j <= L - 1; j++) { 11 let largestI = -1; 12 const jVal = Number.parseInt(bank[j] as string, 10); 13 for (let i = 0; i < j; i++) { 14 const iVal = Number.parseInt(bank[i] as string, 10); 15 if (iVal > largestI) largestI = iVal; 16 } 17 18 const pair = largestI * 10 + jVal; 19 20 if (pair > highestPair) highestPair = pair; 21 } 22 23 jolts += highestPair; 24 } 25 26 console.log("part 1:", jolts); 27})(); 28 29(() => { 30 let jolts = 0n; 31 for (const bank of banks) { 32 const L = bank.length; 33 if (L === 0) continue; 34 35 const K = 12; 36 const joltNums: number[] = []; 37 38 for (let j = 0; j <= L - 1; j++) { 39 const jVal = Number.parseInt(bank[j] as string, 10); 40 41 // Remaining digits including current position 42 const remaining = L - j; 43 44 // Improve prefix: pop smaller tail digits if we can still reach K after popping 45 while ( 46 joltNums.length > 0 && 47 (joltNums[joltNums.length - 1] as number) < jVal && 48 joltNums.length - 1 + remaining >= K 49 ) { 50 joltNums.pop(); 51 } 52 53 // Take current if we still need digits 54 if (joltNums.length < K) { 55 joltNums.push(jVal); 56 } 57 58 // else skip 59 } 60 61 // Accumulate as BigInt 62 let acc = 0n; 63 for (const d of joltNums) { 64 acc = acc * 10n + BigInt(d); 65 } 66 jolts += acc; 67 } 68 69 console.log("part 2", jolts); 70})();