Skip to content

Commit

Permalink
fix(core): pick best sequence from duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
marcincichocki authored Mar 25, 2022
1 parent ebc232a commit 1a1b5b7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/common/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BitMask,
chunk,
getClosest,
groupBy,
memoize,
unique,
uniqueBy,
Expand Down Expand Up @@ -88,4 +89,25 @@ describe('utils', () => {
expect(mask.has(2)).toBe(true);
expect(mask.has(4)).toBe(false);
});

it('should group elements by value', () => {
const data = [
{ key: 'bug', value: 99 },
{ key: 'feature', value: 18 },
{ key: 'bug', value: 7 },
{ key: 'feature', value: 42 },
];
const group = groupBy(data, (item) => item.key);

expect(group).toEqual({
bug: [
{ key: 'bug', value: 99 },
{ key: 'bug', value: 7 },
],
feature: [
{ key: 'feature', value: 18 },
{ key: 'feature', value: 42 },
],
});
});
});
16 changes: 16 additions & 0 deletions src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ export function uniqueWith<T, R>(fn: (obj: T) => R) {
};
}

export function groupBy<T>(data: T[], callback: (item: T) => any) {
const group: Record<string, T[]> = {};

for (const item of data) {
const key = callback(item);

if (!group[key]) {
group[key] = [item];
} else {
group[key].push(item);
}
}

return group;
}

// Simple memo, only use with primitives
export function memoize<R, T extends (...args: any[]) => R>(fn: T): T {
const cache = new Map<string, R>();
Expand Down
27 changes: 21 additions & 6 deletions src/core/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { memoize, permute, Serializable, uniqueBy, uniqueWith } from '@/common';
import {
groupBy,
memoize,
permute,
Serializable,
uniqueBy,
uniqueWith,
} from '@/common';
import {
BreachProtocolRawData,
byBufferSize,
Expand Down Expand Up @@ -177,6 +184,13 @@ export function parseDaemons(
return [regularDaemons, childDaemons];
}

function getBestSequenceFromGroup(group: Sequence[]) {
const strengths = group.map((s) => s.strength);
const maxStrength = Math.max(...strengths);

return group.find((s) => s.strength === maxStrength);
}

export function generateSequences(
{ daemons, bufferSize }: Omit<BreachProtocolRawData, 'grid'>,
strategy: SequenceCompareStrategy = new IndexSequenceCompareStrategy()
Expand All @@ -185,15 +199,16 @@ export function generateSequences(
const childSequences = childDaemons
.filter(byUniqueValue())
.map((d) => Sequence.fromPermutation([d]));

const regularSequences = permute(regularDaemons)
.flatMap((p) => p.map((d, i) => p.slice(0, i + 1)))
.filter(uniqueWith(getPermutationId))
.map((p) => Sequence.fromPermutation(p));

return regularSequences
const sequences = regularSequences
.concat(childSequences)
.filter(byUniqueValue())
.filter(byBufferSize(bufferSize))
.filter(byBufferSize(bufferSize));
const group = groupBy(sequences, (s) => s.tValue);

return Object.keys(group)
.map((k) => getBestSequenceFromGroup(group[k]))
.sort((s1, s2) => strategy.apply(s1, s2));
}

0 comments on commit 1a1b5b7

Please sign in to comment.