Created
August 26, 2022 19:17
-
-
Save Xmerr/304961f62bc1800c1f429078919d07cf to your computer and use it in GitHub Desktop.
550f22f4d758534c1100025a
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
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