diff --git a/solutions/typescript/2023/05/src/p1.ts b/solutions/typescript/2023/05/src/p1.ts index ddbd377cd..ce1fe4a4b 100644 --- a/solutions/typescript/2023/05/src/p1.ts +++ b/solutions/typescript/2023/05/src/p1.ts @@ -1,12 +1,21 @@ import { task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json'; -import { parse } from './parse.js'; +import { findRange, parse } from './parse.js'; export const p1 = (input: string): number => { const data = parse(input); - console.log(data); - return 0; + return data.seeds + .map((seed) => { + const soil = findRange(seed, data.seedToSoilMap); + const fertilizer = findRange(soil, data.soilToFertilizerMap); + const water = findRange(fertilizer, data.fertilizerToWaterMap); + const light = findRange(water, data.waterToLightMap); + const temperature = findRange(light, data.lightToTemperatureMap); + const humidity = findRange(temperature, data.temperatureToHumidityMap); + const location = findRange(humidity, data.humidityToLocationMap); + return location; + }) + .min(); }; -await task(p1, packageJson.aoc, 'example.1.txt'); // 0 ~4.36ms -// await task(p1, packageJson.aoc); // 0 ~4.36ms +await task(p1, packageJson.aoc); // 84470622 ~4.36ms diff --git a/solutions/typescript/2023/05/src/p2.ts b/solutions/typescript/2023/05/src/p2.ts index 320d96804..c98ddee85 100644 --- a/solutions/typescript/2023/05/src/p2.ts +++ b/solutions/typescript/2023/05/src/p2.ts @@ -1,8 +1,21 @@ import { task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json'; +import { findRange, parse } from './parse.js'; -export const p2 = (_input: string): number => { - return 0; +export const p2 = (input: string): number => { + const data = parse(input); + return data.seeds + .map((seed) => { + const soil = findRange(seed, data.seedToSoilMap); + const fertilizer = findRange(soil, data.soilToFertilizerMap); + const water = findRange(fertilizer, data.fertilizerToWaterMap); + const light = findRange(water, data.waterToLightMap); + const temperature = findRange(light, data.lightToTemperatureMap); + const humidity = findRange(temperature, data.temperatureToHumidityMap); + const location = findRange(humidity, data.humidityToLocationMap); + return location; + }) + .min(); }; -await task(p2, packageJson.aoc); // 20107 ~4.36ms +await task(p2, packageJson.aoc); // 84470622 ~4.36ms diff --git a/solutions/typescript/2023/05/src/parse.ts b/solutions/typescript/2023/05/src/parse.ts index 3318a20c0..58ec2f954 100644 --- a/solutions/typescript/2023/05/src/parse.ts +++ b/solutions/typescript/2023/05/src/parse.ts @@ -43,6 +43,16 @@ const toRange = (line: string): Range => { rangeLength, }; }; + +export const findRange = (seed: number, rangeMap: Range[]): number => { + const range = rangeMap.find( + (range) => + range.sourceRangeStart <= seed && seed < range.sourceRangeStart + range.rangeLength, + ); + const delta = range ? range.destinationRange - range.sourceRangeStart : 0; + return seed + delta; +}; + export const parse = (input: string): Card => { let parsing: ParseableData = ParseableData.SEEDS; let seeds: number[] = [];