Samuel Shuert | 6e7cbc1 | 2023-12-05 20:11:54 -0500 | [diff] [blame] | 1 | import { readFileSync } from "fs"; |
| 2 | |
| 3 | const input = readFileSync('day 5/input.txt').toString().split('\n\n') |
| 4 | |
| 5 | interface MappedRange { |
| 6 | start: number; |
| 7 | dest: number; |
| 8 | rangeLength: number; |
| 9 | pointers: Record<number, number>; |
| 10 | } |
| 11 | |
| 12 | interface Input { |
| 13 | seeds?: number[]; |
| 14 | sts?: MappedRange[]; |
| 15 | stf?: MappedRange[]; |
| 16 | ftw?: MappedRange[]; |
| 17 | wtl?: MappedRange[]; |
| 18 | ltt?: MappedRange[]; |
| 19 | tth?: MappedRange[]; |
| 20 | htl?: MappedRange[]; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | const mappedInput: Input = {}; |
| 25 | |
| 26 | mappedInput.seeds = input[0].split(':')[1].trim().split(' ').map(i => parseInt(i)); |
| 27 | const sts = input[1].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n))); |
| 28 | const stf = input[2].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n))); |
| 29 | const ftw = input[3].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n))); |
| 30 | const wtl = input[4].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n))); |
| 31 | const ltt = input[5].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n))); |
| 32 | const tth = input[6].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n))); |
| 33 | const htl = input[7].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n))); |
| 34 | |
| 35 | function pointerMap(array: number[][]) { |
| 36 | return array.map(arr => { |
| 37 | const pointers: Record<number, number> = {} |
| 38 | for(Object.keys(pointers).length; Object.keys(pointers).length < arr[2];) { |
| 39 | const start = arr[1] + Object.keys(pointers).length; |
| 40 | const dest = arr[0] + Object.keys(pointers).length |
| 41 | console.log(`Creating Pointer ${start} => ${dest}`) |
| 42 | pointers[start] = dest; |
| 43 | } |
| 44 | |
| 45 | return { |
| 46 | pointers, |
| 47 | rangeLength: arr[2], |
| 48 | start: arr[1], |
| 49 | dest: arr[0] |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | console.log(`Generating Pointers for seed to soil`) |
| 54 | mappedInput.sts = pointerMap(sts); |
| 55 | console.log(`Generating Pointers for soil to fertilizer`) |
| 56 | mappedInput.stf = pointerMap(stf); |
| 57 | console.log(`Generating Pointers for fertilizer to water`) |
| 58 | mappedInput.ftw = pointerMap(ftw); |
| 59 | console.log(`Generating Pointers for water to light`) |
| 60 | mappedInput.wtl = pointerMap(wtl); |
| 61 | console.log(`Generating Pointers for light to temp`) |
| 62 | mappedInput.ltt = pointerMap(ltt); |
| 63 | console.log(`Generating Pointers for temp to humidity`) |
| 64 | mappedInput.tth = pointerMap(tth); |
| 65 | console.log(`Generating Pointers for humidity to location`) |
| 66 | mappedInput.htl = pointerMap(htl); |
| 67 | |
| 68 | |
| 69 | // Part 1 |
| 70 | (() => { |
| 71 | const locations: {seed: number, location: number}[] = [] |
| 72 | for(const seed of mappedInput.seeds) { |
| 73 | console.log(`Checking ${seed}`) |
| 74 | |
| 75 | const soil = mappedInput.stf.find(s => Object.keys(s.pointers).includes(seed.toString()))?.pointers[seed] ?? seed |
| 76 | console.log(soil === seed ? `Continuing with default` : `Found ${soil}`) |
| 77 | const fertilizer = mappedInput.stf.find(s => Object.keys(s.pointers).includes(soil.toString()))?.pointers[soil] ?? soil |
| 78 | console.log(fertilizer === soil ? `Continuing with default` : `Found ${fertilizer}`) |
| 79 | const water = mappedInput.stf.find(s => Object.keys(s.pointers).includes(fertilizer.toString()))?.pointers[fertilizer] ?? fertilizer |
| 80 | console.log(water === fertilizer ? `Continuing with default` : `Found ${water}`) |
| 81 | const light = mappedInput.stf.find(s => Object.keys(s.pointers).includes(water.toString()))?.pointers[water] ?? water |
| 82 | console.log(light === water ? `Continuing with default` : `Found ${light}`) |
| 83 | const temperature = mappedInput.stf.find(s => Object.keys(s.pointers).includes(light.toString()))?.pointers[light] ?? light |
| 84 | console.log(temperature === light ? `Continuing with default` : `Found ${temperature}`) |
| 85 | const humidity = mappedInput.stf.find(s => Object.keys(s.pointers).includes(temperature.toString()))?.pointers[temperature] ?? temperature |
| 86 | console.log(humidity === temperature ? `Continuing with default` : `Found ${humidity}`) |
| 87 | const location = mappedInput.stf.find(s => Object.keys(s.pointers).includes(humidity.toString()))?.pointers[humidity] ?? humidity |
| 88 | console.log(location === humidity ? `Continuing with default` : `Found ${location}`) |
| 89 | |
| 90 | locations.push({seed, location}) |
| 91 | } |
| 92 | |
| 93 | const sorted = locations.sort((a,b) => a.location < b.location ? -1 : 1); |
| 94 | console.log(sorted) |
| 95 | const smallest = sorted[0] |
| 96 | console.log(`(Part 1) Lowest location number: (seed: ${smallest.seed}) ${smallest.location}`) |
| 97 | })(); |