From ebc9f922fe69625d44a6cddbe13878db527dccd2 Mon Sep 17 00:00:00 2001 From: iswilljr Date: Sun, 3 Dec 2023 09:49:06 -0500 Subject: [PATCH] feat(2023): add solution for challenge #03 --- 2023/challenge-03/README.md | 27 ++++++++++++++++++++++++++ 2023/challenge-03/challenge-03.test.ts | 16 +++++++++++++++ 2023/challenge-03/challenge-03.ts | 13 +++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 2023/challenge-03/README.md create mode 100644 2023/challenge-03/challenge-03.test.ts create mode 100644 2023/challenge-03/challenge-03.ts diff --git a/2023/challenge-03/README.md b/2023/challenge-03/README.md new file mode 100644 index 0000000..999a60c --- /dev/null +++ b/2023/challenge-03/README.md @@ -0,0 +1,27 @@ +# Challenge #3: The naughty elf + +In Santa's workshop, **a mischievous elf** has been playing around with the gift production line, adding or removing an unplanned step. + +You have the original sequence of original manufacturing steps and the modified modified sequence that may include an extra step or be missing a step. + +Your task is to **write a function that identifies and returns the first extra step that was added or removed in the manufacturing chain**. If there is no difference between the sequences, return an empty string. + +```js +const original = 'abcd' +const modified = 'abcde' +findNaughtyStep(original, modified) // 'e' + +const original = 'stepfor' +const modified = 'stepor' +findNaughtyStep(original, modified) // 'f' + +const original = 'abcde' +const modified = 'abcde' +findNaughtyStep(original, modified) // '' +``` + +Please, keep in mind: + +- **There will always be one different step or none.** +- **The modification can occur anywhere in the string.** +- **The original steps could be empty** diff --git a/2023/challenge-03/challenge-03.test.ts b/2023/challenge-03/challenge-03.test.ts new file mode 100644 index 0000000..5eb1404 --- /dev/null +++ b/2023/challenge-03/challenge-03.test.ts @@ -0,0 +1,16 @@ +import { describe } from 'vitest' +import { findNaughtyStep } from './challenge-03' + +const TEST_CASES: TestCases<[string, string], string> = [ + { args: ['abcd', 'abcde'], expected: 'e' }, + { args: ['xxxx', 'xxoxx'], expected: 'o' }, + { args: ['stepfor', 'stepor'], expected: 'f' }, + { args: ['iiiii', 'iiiii'], expected: '' }, +] + +describe('Challenge #3: The naughty elf', () => { + buildChallengeTestCases({ + cases: TEST_CASES, + spreadFn: findNaughtyStep, + }) +}) diff --git a/2023/challenge-03/challenge-03.ts b/2023/challenge-03/challenge-03.ts new file mode 100644 index 0000000..e661155 --- /dev/null +++ b/2023/challenge-03/challenge-03.ts @@ -0,0 +1,13 @@ +export function findNaughtyStep(original: string, modified: string) { + const originalLen = original.length + const modifiedLen = modified.length + + const sequences = { + [originalLen]: [original, modified], + [modifiedLen]: [modified, original], + } + + const [steps, reference] = sequences[Math.max(originalLen, modifiedLen)] + + return [...steps].find((step, index) => step !== reference[index]) ?? '' +}