Skip to content

Commit

Permalink
chore: cleaned up around 2023 05
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 10, 2023
1 parent fff8edf commit a583106
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/badges/typescript/2023.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"label": "Advent of TypeScript 2023",
"message": "9/25",
"message": "10/25",
"color": "orange"
}
2 changes: 1 addition & 1 deletion solutions/typescript/2023/05/src/p1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 0 additions & 27 deletions solutions/typescript/2023/05/src/p2.naive.ts

This file was deleted.

7 changes: 4 additions & 3 deletions solutions/typescript/2023/05/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { p2, refract } from './p2.js';
import { toRange, type Range } from './parse.js';

type StrippedRange = Omit<Range, 'from' | 'to' | 'slope'>;

const stripRange = (range: Range): StrippedRange => {
return {
destinationRange: range.destinationRange,
Expand All @@ -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);
});
});

Expand Down
90 changes: 19 additions & 71 deletions solutions/typescript/2023/05/src/p2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Range>((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<Range>((rightMapper) =>
middleColumn
.filterMap((middleSection) => Interval.intersection(middleSection, rightMapper.from))
.map((reachableRightMapper) => ({
from: reachableRightMapper,
slope: rightMapper.slope,
sourceRangeStart: reachableRightMapper.low,
Expand All @@ -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<Range>((interval) => ({
from: interval,
to: interval,
destinationRange: interval.low,
rangeLength: interval.length,
slope: 0,
sourceRangeStart: interval.low,
}));

reachableRightMappers.push(
...nonMapping.map<Range>((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 => {
Expand All @@ -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
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 @@ -9,6 +9,7 @@ export interface Card {
lightToTemperatureMap: Range[];
temperatureToHumidityMap: Range[];
humidityToLocationMap: Range[];
maps: Range[][];
}

export interface Range {
Expand Down Expand Up @@ -129,5 +130,14 @@ export const parse = (input: string): Card => {
lightToTemperatureMap,
temperatureToHumidityMap,
humidityToLocationMap,
maps: [
seedToSoilMap,
soilToFertilizerMap,
fertilizerToWaterMap,
waterToLightMap,
lightToTemperatureMap,
temperatureToHumidityMap,
humidityToLocationMap,
],
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,6 @@ describe('Interval', () => {
},
]);

console.log(interval);

expect(interval.length).toEqual(0);
});
});
Expand Down Expand Up @@ -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>(Interval.closed(0, 0));
expect(complement[1]).toEqual<Interval>(Interval.closed(4, 4));
Expand All @@ -498,7 +495,7 @@ describe('Interval', () => {
[Interval.closed(0, 4)],
[Interval.closed(0, 4)],
);
console.log(complement);

expect(complement.length).toEqual(0);
});

Expand Down
30 changes: 6 additions & 24 deletions solutions/typescript/libs/lib/src/math/common/interval.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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({
Expand All @@ -391,30 +379,24 @@ 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);
}
}
}
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,
Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down

0 comments on commit a583106

Please sign in to comment.