-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-day5.ts
38 lines (31 loc) · 991 Bytes
/
util-day5.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import _, { Dictionary } from "lodash";
export const delimiter: string = "***";
export interface RangeMapping extends Dictionary<number> {
destinationRangeStart: number;
sourceRangeStart: number;
rangeLength: number;
}
export type MappingCollection = RangeMapping[];
export const applyMappingCollection = (
location: number,
mappingCollection: MappingCollection
): number => {
const mapping = _.find(
mappingCollection,
({ destinationRangeStart, sourceRangeStart, rangeLength }) => {
return (
location >= sourceRangeStart &&
location < sourceRangeStart + rangeLength
);
}
) as RangeMapping;
if (mapping) {
const offset: number = location - mapping.sourceRangeStart;
return mapping.destinationRangeStart + offset;
}
return location;
};
export const getFinalLocation = (
seedLocation: number,
mappingCollections: MappingCollection[]
): number => _.reduce(mappingCollections, applyMappingCollection, seedLocation);