blob: 7654183ff77df3fca4d394d88a01018172924798 [file] [log] [blame]
Samuel Shuert6e7cbc12023-12-05 20:11:54 -05001import { readFileSync } from "fs";
2
3const input = readFileSync('day 5/input.txt').toString().split('\n\n')
4
5interface MappedRange {
6 start: number;
7 dest: number;
8 rangeLength: number;
9 pointers: Record<number, number>;
10}
11
12interface 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
24const mappedInput: Input = {};
25
26mappedInput.seeds = input[0].split(':')[1].trim().split(' ').map(i => parseInt(i));
27const sts = input[1].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
28const stf = input[2].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
29const ftw = input[3].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
30const wtl = input[4].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
31const ltt = input[5].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
32const tth = input[6].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
33const htl = input[7].split(':')[1].trim().split('\n').map(i => i.split(' ').map(n => parseInt(n)));
34
35function 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}
53console.log(`Generating Pointers for seed to soil`)
54mappedInput.sts = pointerMap(sts);
55console.log(`Generating Pointers for soil to fertilizer`)
56mappedInput.stf = pointerMap(stf);
57console.log(`Generating Pointers for fertilizer to water`)
58mappedInput.ftw = pointerMap(ftw);
59console.log(`Generating Pointers for water to light`)
60mappedInput.wtl = pointerMap(wtl);
61console.log(`Generating Pointers for light to temp`)
62mappedInput.ltt = pointerMap(ltt);
63console.log(`Generating Pointers for temp to humidity`)
64mappedInput.tth = pointerMap(tth);
65console.log(`Generating Pointers for humidity to location`)
66mappedInput.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})();