-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,90 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import { Interval, task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
import { findRange, parse } from './parse.js'; | ||
import { parse, type Range } from './parse.js'; | ||
|
||
export const getRangeEnd = (range: Range): number => { | ||
return range.destinationRange + range.rangeLength; | ||
}; | ||
|
||
export const refract = (a: Range, b: Range): Range[] => { | ||
const aTarget = Interval.closed(a.destinationRange, a.destinationRange + a.rangeLength); | ||
const bSource = Interval.closed(b.sourceRangeStart, b.sourceRangeStart + b.rangeLength); | ||
const bTarget = Interval.closed(b.destinationRange, b.destinationRange + b.rangeLength); | ||
|
||
const result: Range[] = []; | ||
|
||
if (aTarget.high > bSource.high) { | ||
const topRefraction: Range = { | ||
sourceRangeStart: bSource.high, | ||
destinationRange: bSource.high, | ||
rangeLength: Math.min(aTarget.high - bSource.high, a.rangeLength), | ||
}; | ||
result.push(topRefraction); | ||
} | ||
|
||
if (aTarget.low < bSource.low) { | ||
const bottomRefraction: Range = { | ||
sourceRangeStart: aTarget.low, | ||
destinationRange: aTarget.low, | ||
rangeLength: Math.min(bSource.low - aTarget.low, a.rangeLength), | ||
}; | ||
result.push(bottomRefraction); | ||
} | ||
|
||
aTarget.intersects(bSource); | ||
// Only if there's an itersection | ||
if (aTarget.intersects(bSource)) { | ||
const middleRefractionStartLow = Math.max(aTarget.low, bSource.low); | ||
const middleRefractionStartHigh = Math.min(aTarget.high, bSource.high); | ||
const mappingDelta = bTarget.low - bSource.low; | ||
// Only this one is mapping | ||
const middleRefraction: Range = { | ||
sourceRangeStart: middleRefractionStartLow, | ||
rangeLength: middleRefractionStartHigh - middleRefractionStartLow, | ||
destinationRange: middleRefractionStartLow + mappingDelta, | ||
}; | ||
result.push(middleRefraction); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
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; | ||
.slideWindow(2, 2) | ||
.flatMap(([seedStart, seedCount]) => { | ||
const firstRange: Range = { | ||
sourceRangeStart: seedStart, | ||
destinationRange: seedStart, | ||
rangeLength: seedCount, | ||
}; | ||
|
||
const seedToSoilRefraction = data.seedToSoilMap.flatMap((range) => | ||
refract(firstRange, range), | ||
); | ||
const soilToFertilizerRefraction = seedToSoilRefraction.flatMap((a) => | ||
data.soilToFertilizerMap.flatMap((range) => refract(a, range)), | ||
); | ||
const fertilizerToWaterRefraction = soilToFertilizerRefraction.flatMap((a) => | ||
data.fertilizerToWaterMap.flatMap((range) => refract(a, range)), | ||
); | ||
const waterToLightRefraction = fertilizerToWaterRefraction.flatMap((a) => | ||
data.waterToLightMap.flatMap((range) => refract(a, range)), | ||
); | ||
const lightToTemperatureRefraction = waterToLightRefraction.flatMap((a) => | ||
data.lightToTemperatureMap.flatMap((range) => refract(a, range)), | ||
); | ||
const temperatureToHumidityRefraction = lightToTemperatureRefraction.flatMap((a) => | ||
data.temperatureToHumidityMap.flatMap((range) => refract(a, range)), | ||
); | ||
const humidityToLocationRefraction = temperatureToHumidityRefraction.flatMap((a) => | ||
data.humidityToLocationMap.flatMap((range) => refract(a, range)), | ||
); | ||
|
||
return humidityToLocationRefraction.map((range) => range.sourceRangeStart).min(); | ||
}) | ||
.min(); | ||
}; | ||
|
||
await task(p2, packageJson.aoc); // 84470622 ~4.36ms | ||
await task(p2, packageJson.aoc, 'example.1.txt'); // 84470622 ~4.36ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters