Skip to content

Instantly share code, notes, and snippets.

@vladyn
Created July 19, 2025 16:41
Show Gist options
  • Save vladyn/d9b511db4fe014fbf7dfbd0c6fcf8afc to your computer and use it in GitHub Desktop.
Save vladyn/d9b511db4fe014fbf7dfbd0c6fcf8afc to your computer and use it in GitHub Desktop.
const pattern = ["red", "green", "yellow"];
function isValidTrafficSequence(matcher: string[]) {
const target = new Array(matcher.length).fill(pattern).flatMap(x => x);
const reducer = (acc: [], item: never, index: number) => {
if (item === target[index]) acc.push(item);
return acc;
}
const result = matcher.reduce(reducer, []);
return matcher.length === result.length;
}
isValidTrafficSequence(["red", "green", "yellow", "red", "green"]);
isValidTrafficSequence(["red", "yellow", "green"]);
isValidTrafficSequence([]);
isValidTrafficSequence(["red", "yellow", "green", "red", "yellow", "green", "red", "yellow", "green", "green"]);
@vladyn
Copy link
Author

vladyn commented Jul 19, 2025

A simple task for validating a traffic light 🚦 sequence, I've got from a challenge of the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment