From a5831061ee4d5cf9f6a5ddbbb6dfd51642a17a6f Mon Sep 17 00:00:00 2001 From: AlexAegis Date: Sun, 10 Dec 2023 23:09:20 +0100 Subject: [PATCH] chore: cleaned up around 2023 05 --- .github/badges/typescript/2023.json | 2 +- solutions/typescript/2023/05/src/p1.ts | 2 +- solutions/typescript/2023/05/src/p2.naive.ts | 27 ------ solutions/typescript/2023/05/src/p2.spec.ts | 7 +- solutions/typescript/2023/05/src/p2.ts | 90 ++++--------------- solutions/typescript/2023/05/src/parse.ts | 10 +++ .../src/math/common/interval.class.spec.ts | 5 +- .../lib/src/math/common/interval.class.ts | 30 ++----- solutions/typescript/readme.md | 2 +- 9 files changed, 43 insertions(+), 132 deletions(-) delete mode 100644 solutions/typescript/2023/05/src/p2.naive.ts diff --git a/.github/badges/typescript/2023.json b/.github/badges/typescript/2023.json index 0d222e4a2..ff50b1b70 100644 --- a/.github/badges/typescript/2023.json +++ b/.github/badges/typescript/2023.json @@ -1,6 +1,6 @@ { "schemaVersion": 1, "label": "Advent of TypeScript 2023", - "message": "9/25", + "message": "10/25", "color": "orange" } diff --git a/solutions/typescript/2023/05/src/p1.ts b/solutions/typescript/2023/05/src/p1.ts index 01fa17843..e34b63dbf 100644 --- a/solutions/typescript/2023/05/src/p1.ts +++ b/solutions/typescript/2023/05/src/p1.ts @@ -20,4 +20,4 @@ export const p1 = (input: string): number => { return data.seeds.map(mapSeed(data)).min(); }; -await task(p1, packageJson.aoc); // 84470622 ~4.36ms +await task(p1, packageJson.aoc); // 84470622 ~0.11ms diff --git a/solutions/typescript/2023/05/src/p2.naive.ts b/solutions/typescript/2023/05/src/p2.naive.ts deleted file mode 100644 index 537bcc964..000000000 --- a/solutions/typescript/2023/05/src/p2.naive.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { task } from '@alexaegis/advent-of-code-lib'; -import packageJson from '../package.json'; -import { findRange, parse } from './parse.js'; - -// Will never terminate for the actual input -export const p2Naive = (input: string): number => { - const data = parse(input); - return data.seeds - .slideWindow(2, 2) - .flatMap(([seedStart, seedCount]) => { - const locations: number[] = []; - for (let seed = seedStart; seed < seedStart + seedCount; 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); - locations.push(location); - } - return locations; - }) - .min(); -}; - -await task(p2Naive, packageJson.aoc, 'example.1.txt'); // 46 diff --git a/solutions/typescript/2023/05/src/p2.spec.ts b/solutions/typescript/2023/05/src/p2.spec.ts index fbfe1857a..63ee1bfac 100644 --- a/solutions/typescript/2023/05/src/p2.spec.ts +++ b/solutions/typescript/2023/05/src/p2.spec.ts @@ -5,6 +5,7 @@ import { p2, refract } from './p2.js'; import { toRange, type Range } from './parse.js'; type StrippedRange = Omit; + const stripRange = (range: Range): StrippedRange => { return { destinationRange: range.destinationRange, @@ -13,18 +14,18 @@ const stripRange = (range: Range): StrippedRange => { }; }; -describe.skip('2023 05 p2', () => { +describe('2023 05 p2', () => { describe('the input', () => { it('should solve the input', async () => { const { input } = await loadTaskResources(packageJson.aoc); - expect(p2(input)).toEqual(0); + expect(p2(input)).toEqual(26_714_516); }); }); describe('example 1', () => { it('should be solved', async () => { const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt'); - expect(p2(input)).toEqual(0); + expect(p2(input)).toEqual(46); }); }); diff --git a/solutions/typescript/2023/05/src/p2.ts b/solutions/typescript/2023/05/src/p2.ts index 461fa8336..c908cd9d1 100644 --- a/solutions/typescript/2023/05/src/p2.ts +++ b/solutions/typescript/2023/05/src/p2.ts @@ -2,28 +2,16 @@ import { Interval, task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json'; import { parse, type Range } from './parse.js'; -export const getRangeEnd = (range: Range): number => { - return range.destinationRange + range.rangeLength; -}; - /** * Takes a set of mappings, and reduces the right side to only the effective ones - * - * */ export const refract = (left: Range[], right: Range[]): Range[] => { const middleColumn = Interval.merge(left.map((l) => l.to)); - console.log('middle\t\t', middleColumn.toString()); - console.log('rightFrom\t', right.map((r) => r.from).toString()); - console.log('rightTo\t\t', right.map((r) => r.to).toString()); - - // first, this is the set of sections that are mapping to another region - const reachableRightMappers = right.flatMap((rightMapper) => { - const reachableRightMappers = middleColumn.filterMap((middleSection) => - Interval.intersection(middleSection, rightMapper.from), - ); - return reachableRightMappers.map((reachableRightMapper) => { - const res = { + // this is the set of sections that are mapping to another region + const reachableRightMappers = right.flatMap((rightMapper) => + middleColumn + .filterMap((middleSection) => Interval.intersection(middleSection, rightMapper.from)) + .map((reachableRightMapper) => ({ from: reachableRightMapper, slope: rightMapper.slope, sourceRangeStart: reachableRightMapper.low, @@ -39,52 +27,22 @@ export const refract = (left: Range[], right: Range[]): Range[] => { lowQualifier: reachableRightMapper.lowQualifier, }, ), - }; - - //console.log( - // 'INTERSEC', - // rightMapper.from.toString(), - // '->', - // rightMapper.to.toString(), - // 'intersection:', - // reachableRightMapper?.toString() ?? 'NONE', - // '=>', - // res?.to.toString(), - //); - return res; - }); - }); + })), + ); const nonMapping = Interval.complement( reachableRightMappers.map((r) => r.from), - middleColumn.map( - (m) => - new Interval(m.low, m.high, { - lowQualifier: 'open', - highQualifier: 'open', - }), - ), - ).filter((i) => i.isFinite() && i.length > 0); //.map(i => i.as); - - // const nonMapping = Interval.complement(reachableRightMappers.map((r) => r.from)).flatMap((i) => - // middleColumn.filterMap((m) => i.trim(m)), - // ); - console.log( - 'nonMapping', - nonMapping.map((nm) => nm.toString()), - ); + middleColumn, + ).map((interval) => ({ + from: interval, + to: interval, + destinationRange: interval.low, + rangeLength: interval.length, + slope: 0, + sourceRangeStart: interval.low, + })); - reachableRightMappers.push( - ...nonMapping.map((interval) => ({ - from: interval, - to: interval, - destinationRange: interval.low, - rangeLength: interval.length, - slope: 0, - sourceRangeStart: interval.low, - })), - ); - return reachableRightMappers; + return [...reachableRightMappers, ...nonMapping]; }; export const p2 = (input: string): number => { @@ -98,20 +56,10 @@ export const p2 = (input: string): number => { slope: 0, })); - const maps = [ - data.seedToSoilMap, - data.soilToFertilizerMap, - data.fertilizerToWaterMap, - data.waterToLightMap, - data.lightToTemperatureMap, - data.temperatureToHumidityMap, - data.humidityToLocationMap, - ]; - - return maps + return data.maps .reduce((acc, next) => refract(acc, next), seedRanges) .map((range) => range.to.low) .min(); }; -await task(p2, packageJson.aoc); // 26714516 ~4.36ms +await task(p2, packageJson.aoc); // 26714516 ~0.39ms diff --git a/solutions/typescript/2023/05/src/parse.ts b/solutions/typescript/2023/05/src/parse.ts index 0a66ed6d0..ced2abcbd 100644 --- a/solutions/typescript/2023/05/src/parse.ts +++ b/solutions/typescript/2023/05/src/parse.ts @@ -9,6 +9,7 @@ export interface Card { lightToTemperatureMap: Range[]; temperatureToHumidityMap: Range[]; humidityToLocationMap: Range[]; + maps: Range[][]; } export interface Range { @@ -129,5 +130,14 @@ export const parse = (input: string): Card => { lightToTemperatureMap, temperatureToHumidityMap, humidityToLocationMap, + maps: [ + seedToSoilMap, + soilToFertilizerMap, + fertilizerToWaterMap, + waterToLightMap, + lightToTemperatureMap, + temperatureToHumidityMap, + humidityToLocationMap, + ], }; }; diff --git a/solutions/typescript/libs/lib/src/math/common/interval.class.spec.ts b/solutions/typescript/libs/lib/src/math/common/interval.class.spec.ts index 6e6e0bc66..1adbd8d9f 100644 --- a/solutions/typescript/libs/lib/src/math/common/interval.class.spec.ts +++ b/solutions/typescript/libs/lib/src/math/common/interval.class.spec.ts @@ -444,8 +444,6 @@ describe('Interval', () => { }, ]); - console.log(interval); - expect(interval.length).toEqual(0); }); }); @@ -487,7 +485,6 @@ describe('Interval', () => { it('should return the two edges when trying to completement an open interval within its closed counterpart ', () => { const complement = Interval.complement([Interval.open(0, 4)], [Interval.closed(0, 4)]); - console.log(complement); expect(complement.length).toEqual(2); expect(complement[0]).toEqual(Interval.closed(0, 0)); expect(complement[1]).toEqual(Interval.closed(4, 4)); @@ -498,7 +495,7 @@ describe('Interval', () => { [Interval.closed(0, 4)], [Interval.closed(0, 4)], ); - console.log(complement); + expect(complement.length).toEqual(0); }); diff --git a/solutions/typescript/libs/lib/src/math/common/interval.class.ts b/solutions/typescript/libs/lib/src/math/common/interval.class.ts index e4ed8d2ac..a66685447 100644 --- a/solutions/typescript/libs/lib/src/math/common/interval.class.ts +++ b/solutions/typescript/libs/lib/src/math/common/interval.class.ts @@ -96,14 +96,10 @@ export class Interval implements IntervalLike, IntervalQualifier { this.highQualifier = options.highQualifier ?? INTERVAL_ENDPOINT_OPEN_QUALIFIER; } - // static invertQualifier(qualifier: 'closed'): 'open'; - // static invertQualifier(qualifier: 'open'): 'closed'; static invertQualifier(qualifier: IntervalEndpointQualifier): IntervalEndpointQualifier { return qualifier === 'open' ? 'closed' : 'open'; } - // static invertDesignation(designation: 'high'): 'low'; - // static invertDesignation(designation: 'low'): 'high'; static invertDesignation( designation: IntervalEndpointDesignation, ): IntervalEndpointDesignation { @@ -259,22 +255,15 @@ export class Interval implements IntervalLike, IntervalQualifier { intervals: Interval[], within?: Interval[] | undefined, ): Interval[] { - let points: QualifiedNumber[] = []; - points; - if (within) { - points = Interval.collectAllSignificantPoints(within); - } - points.push( - ...Interval.collectAllSignificantPoints(intervals).map(Interval.invertQualifiedNumber), + const points: QualifiedNumber[] = Interval.collectAllSignificantPoints(intervals).map( + Interval.invertQualifiedNumber, ); - points.sort(Interval.compareQualifiedNumber); - console.log('all points to be merged', points); + if (within) { + points.push(...Interval.collectAllSignificantPoints(within)); + } - const me = Interval.mergeQualifiedNumbers(points); - console.log('meee', me); - return me.filter((m) => !m.isEmpty()); - // return within ? within.flatMap((w) => me.filterMap((m) => m.trim(w))) : me; + return Interval.mergeQualifiedNumbers(points, true).filter((m) => !m.isEmpty()); } /** @@ -379,7 +368,6 @@ export class Interval implements IntervalLike, IntervalQualifier { qualifiedNumbers.sort(Interval.compareQualifiedNumber); } - console.log('qualifiedNumbers', qualifiedNumbers); const intervalStartStack: QualifiedNumber[] = []; if (qualifiedNumbers[0]?.originalDesignation === 'high') { intervalStartStack.push({ @@ -391,22 +379,17 @@ export class Interval implements IntervalLike, IntervalQualifier { } for (const qualifiedNumber of qualifiedNumbers) { if (qualifiedNumber.originalDesignation === 'low') { - console.log('000ASHGS222AFA'); - intervalStartStack.push(qualifiedNumber); } else if ( qualifiedNumber.originalDesignation === 'high' && intervalStartStack.length > 0 ) { - console.log('1111ASHGS222AFA'); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const matchingLow = intervalStartStack.shift()!; const next = new Interval(matchingLow.value, qualifiedNumber.value, { lowQualifier: matchingLow.lowQualifier, highQualifier: qualifiedNumber.highQualifier, }); - console.log('ASDASDW@222', next); if (!next.isEmpty()) { result.push(next); } @@ -414,7 +397,6 @@ export class Interval implements IntervalLike, IntervalQualifier { } const last = qualifiedNumbers.at(-1); if (last?.originalDesignation === 'low') { - console.log('LASTINIFIFNIFNIF', last); result.push( new Interval(last.value, Number.POSITIVE_INFINITY, { lowQualifier: last.lowQualifier, diff --git a/solutions/typescript/readme.md b/solutions/typescript/readme.md index 2b6dab219..75396c50d 100644 --- a/solutions/typescript/readme.md +++ b/solutions/typescript/readme.md @@ -12,7 +12,7 @@ | [Day 2](/solutions/typescript/2023/02/) | [0.22ms](/solutions/typescript/2023/02/src/p1.ts) | [0.25ms](/solutions/typescript/2023/02/src/p2.ts) | | [Day 3](/solutions/typescript/2023/03/) | [1.39ms](/solutions/typescript/2023/03/src/p1.ts) | [3.89ms](/solutions/typescript/2023/03/src/p2.ts) | | [Day 4](/solutions/typescript/2023/04/) | [4.36ms](/solutions/typescript/2023/04/src/p1.ts) | [4.57ms](/solutions/typescript/2023/04/src/p2.ts) | -| [Day 5](/solutions/typescript/2023/05/) | [?ms](/solutions/typescript/2023/05/src/p1.ts) | [?ms](/solutions/typescript/2023/05/src/p2.ts) | +| [Day 5](/solutions/typescript/2023/05/) | [0.11ms](/solutions/typescript/2023/05/src/p1.ts) | [0.39ms](/solutions/typescript/2023/05/src/p2.ts) | | [Day 6](/solutions/typescript/2023/06/) | [40μs](/solutions/typescript/2023/06/src/p1.ts) | [7μs](/solutions/typescript/2023/06/src/p2.ts) | | [Day 7](/solutions/typescript/2023/07/) | [8.70ms](/solutions/typescript/2023/07/src/p1.ts) | [20.46ms](/solutions/typescript/2023/07/src/p2.ts) | | [Day 8](/solutions/typescript/2023/08/) | [10.25ms](/solutions/typescript/2023/08/src/p1.ts) | [11.44ms](/solutions/typescript/2023/08/src/p2.ts) |