Skip to content

Instantly share code, notes, and snippets.

@Xmerr
Created August 26, 2022 19:17
Show Gist options
  • Save Xmerr/304961f62bc1800c1f429078919d07cf to your computer and use it in GitHub Desktop.
Save Xmerr/304961f62bc1800c1f429078919d07cf to your computer and use it in GitHub Desktop.
550f22f4d758534c1100025a
export type tDirection = 'NORTH' | 'SOUTH' | 'EAST' | 'WEST';
export const opposites = {
EAST: 'WEST',
NORTH: 'SOUTH',
SOUTH: 'NORTH',
WEST: 'EAST',
};
export function dirReduc(arr: string[]): tDirection[] {
const output = [...arr] as tDirection[];
const iterate = (): boolean => {
let changed = false;
let index = 0;
while (index < output.length - 1) {
const current = output[index];
const next = output[index + 1];
if (opposites[current] !== next) {
index++;
continue;
}
output.splice(index + 1, 1);
output.splice(index, 1);
changed = true;
}
return changed;
};
let changing = true;
while (changing) {
changing = iterate();
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment