Skip to content

Commit

Permalink
feat: 2023 day 5 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 5, 2023
1 parent 4a8bd71 commit 237b8b3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
19 changes: 14 additions & 5 deletions solutions/typescript/2023/05/src/p1.ts
Original file line number Diff line number Diff line change
@@ -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
19 changes: 16 additions & 3 deletions solutions/typescript/2023/05/src/p2.ts
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions solutions/typescript/2023/05/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down

0 comments on commit 237b8b3

Please sign in to comment.