blob: 7654183ff77df3fca4d394d88a01018172924798 [file] [log] [blame]
import { readFileSync } from "fs";
const input = readFileSync('day 5/input.txt').toString().split('\n\n')
interface MappedRange {
start: number;
dest: number;
rangeLength: number;
pointers: Record<number, number>;
}
interface Input {
seeds?: number[];
sts?: MappedRange[];
stf?: MappedRange[];
ftw?: MappedRange[];
wtl?: MappedRange[];
ltt?: MappedRange[];
tth?: MappedRange[];
htl?: MappedRange[];
}
const mappedInput: Input = {};
mappedInput.seeds = input[0].split(':')[1].trim().split(' ').map(i => parseInt(i));
const sts = input[1].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
const stf = input[2].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
const ftw = input[3].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
const wtl = input[4].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
const ltt = input[5].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
const tth = input[6].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
const htl = input[7].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
function pointerMap(array: number[][]) {
return array.map(arr => {
const pointers: Record<number, number> = {}
for(Object.keys(pointers).length; Object.keys(pointers).length < arr[2];) {
const start = arr[1] + Object.keys(pointers).length;
const dest = arr[0] + Object.keys(pointers).length
console.log(`Creating Pointer ${start} => ${dest}`)
pointers[start] = dest;
}
return {
pointers,
rangeLength: arr[2],
start: arr[1],
dest: arr[0]
}
})
}
console.log(`Generating Pointers for seed to soil`)
mappedInput.sts = pointerMap(sts);
console.log(`Generating Pointers for soil to fertilizer`)
mappedInput.stf = pointerMap(stf);
console.log(`Generating Pointers for fertilizer to water`)
mappedInput.ftw = pointerMap(ftw);
console.log(`Generating Pointers for water to light`)
mappedInput.wtl = pointerMap(wtl);
console.log(`Generating Pointers for light to temp`)
mappedInput.ltt = pointerMap(ltt);
console.log(`Generating Pointers for temp to humidity`)
mappedInput.tth = pointerMap(tth);
console.log(`Generating Pointers for humidity to location`)
mappedInput.htl = pointerMap(htl);
// Part 1
(() => {
const locations: {seed: number, location: number}[] = []
for(const seed of mappedInput.seeds) {
console.log(`Checking ${seed}`)
const soil = mappedInput.stf.find(s => Object.keys(s.pointers).includes(seed.toString()))?.pointers[seed] ?? seed
console.log(soil === seed ? `Continuing with default` : `Found ${soil}`)
const fertilizer = mappedInput.stf.find(s => Object.keys(s.pointers).includes(soil.toString()))?.pointers[soil] ?? soil
console.log(fertilizer === soil ? `Continuing with default` : `Found ${fertilizer}`)
const water = mappedInput.stf.find(s => Object.keys(s.pointers).includes(fertilizer.toString()))?.pointers[fertilizer] ?? fertilizer
console.log(water === fertilizer ? `Continuing with default` : `Found ${water}`)
const light = mappedInput.stf.find(s => Object.keys(s.pointers).includes(water.toString()))?.pointers[water] ?? water
console.log(light === water ? `Continuing with default` : `Found ${light}`)
const temperature = mappedInput.stf.find(s => Object.keys(s.pointers).includes(light.toString()))?.pointers[light] ?? light
console.log(temperature === light ? `Continuing with default` : `Found ${temperature}`)
const humidity = mappedInput.stf.find(s => Object.keys(s.pointers).includes(temperature.toString()))?.pointers[temperature] ?? temperature
console.log(humidity === temperature ? `Continuing with default` : `Found ${humidity}`)
const location = mappedInput.stf.find(s => Object.keys(s.pointers).includes(humidity.toString()))?.pointers[humidity] ?? humidity
console.log(location === humidity ? `Continuing with default` : `Found ${location}`)
locations.push({seed, location})
}
const sorted = locations.sort((a,b) => a.location < b.location ? -1 : 1);
console.log(sorted)
const smallest = sorted[0]
console.log(`(Part 1) Lowest location number: (seed: ${smallest.seed}) ${smallest.location}`)
})();