From f85c1c46c815287c2dc6802ae97ba08e4e798e1c Mon Sep 17 00:00:00 2001 From: iswilljr Date: Tue, 12 Dec 2023 10:29:17 -0500 Subject: [PATCH] feat(2023): add solution for challenge #12 --- 2023/challenge-12/README.md | 37 ++++++++++++++++++++++++++ 2023/challenge-12/challenge-12.test.ts | 30 +++++++++++++++++++++ 2023/challenge-12/challenge-12.ts | 24 +++++++++++++++++ README.md | 2 +- 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 2023/challenge-12/README.md create mode 100644 2023/challenge-12/challenge-12.test.ts create mode 100644 2023/challenge-12/challenge-12.ts diff --git a/2023/challenge-12/README.md b/2023/challenge-12/README.md new file mode 100644 index 0000000..6adcda5 --- /dev/null +++ b/2023/challenge-12/README.md @@ -0,0 +1,37 @@ +# Challenge #12: Is it a valid copy? + +At the North Pole, **they still use paper photocopiers.** The elves use them to copy the letters that children send to Santa so they can send them to all the gift departments. + +However, **they are very old and don’t work very well.** Every time they make a copy, the quality of the copy slightly decreases, a phenomenon known as generational loss. + +**You need to detect if one letter is a copy of another.** The letters are very long and you can’t read them, but you can compare them with an algorithm. + +There’s a big **probability** that a character would degrade in each copy (it doesn't always happen!). And when it does, the rule it follows is: + +- **The characters from `A` to `Z` degrade from uppercase to lowercase (`A-Z` ⇒ `a-z`)** +- **Letters degrade in a series of characters in this order: `a-z` ⇒ `#` ⇒ `+` ⇒ `:` ⇒ `.` ⇒ ` `** +- **Characters that are not letters (like digits) do not degrade.** + +Knowing this and receiving the original letter and the copy, you must determine if the copy is a copy of the original. + +```js +checkIsValidCopy('Santa Claus is coming', 'sa#ta cl#us is comin#') // true +checkIsValidCopy('Santa Claus is coming', 'p#nt: cla#s #s c+min#') // false (for the initial p) +checkIsValidCopy('Santa Claus', 's#+:. c:. s') // true +checkIsValidCopy('Santa Claus', 's#+:.#c:. s') // false (there is a # where it should not be) +``` + +To understand how photocopies work and their degradation, look at this example: + +```js +original: 'Santa Claus' +1st copy: 'santa cla#s' +2nd copy: 'sa#t# cl#+s' +3rd copy: 'sa+## c#+:s' +4th copy: 's#++. c+:.s' +5th copy: 's#+:. c:. s' +``` + +Therefore `s#+:. c+:++` is a valid copy of `Santa Claus`. And, as you can see, the degradation of the letters does not occur in a specific order, it is random. + +Based on the CodeWars challenge Photocopy decay diff --git a/2023/challenge-12/challenge-12.test.ts b/2023/challenge-12/challenge-12.test.ts new file mode 100644 index 0000000..8fa0b05 --- /dev/null +++ b/2023/challenge-12/challenge-12.test.ts @@ -0,0 +1,30 @@ +import { describe } from 'vitest' +import { checkIsValidCopy } from './challenge-12' + +const TEST_CASES: TestCases<[string, string], boolean> = [ + { args: ['Santa Claus is coming', 'sa#ta cl#us is comin#'], expected: true }, + { args: ['Santa Claus is coming', 'p#nt: cla#s #s c+min#'], expected: false }, + { args: ['Santa Claus', ' Santa Claus '], expected: false }, + { args: ['Santa Claus', '###:. c:+##'], expected: true }, + { args: ['Santa Claus', 'sant##claus+'], expected: false }, + { args: ['Santa Claus', 's#+:. c:. s'], expected: true }, + { args: ['Santa Claus', 's#+:.#c:. s'], expected: false }, + { args: ['Santa Claus', 'SantA ClauS'], expected: false }, + { args: ['3 regalos', '3 .+:# #:'], expected: true }, + { args: ['3 regalos', '3 '], expected: true }, + { args: ['3 regalos 3', '3 .+:# #: 3'], expected: true }, + { + args: [ + 'Santa Claus viene a buscarte para darte muchos regalos y eso es espectacular porque da mucha felicidad a todos los niños', + 'Santa Claus viene a buscarte para darte muchos regalos y eso es espectacular porque da mucha felicidad a todos los niño', + ], + expected: false, + }, +] + +describe('Challenge #12: Is it a valid copy?', () => { + buildChallengeTestCases({ + cases: TEST_CASES, + spreadFn: checkIsValidCopy, + }) +}) diff --git a/2023/challenge-12/challenge-12.ts b/2023/challenge-12/challenge-12.ts new file mode 100644 index 0000000..1a16844 --- /dev/null +++ b/2023/challenge-12/challenge-12.ts @@ -0,0 +1,24 @@ +export function checkIsValidCopy(original: string, copy: string) { + let index = 0 + let isValidCopy = true + + for (const letter of original) { + const copyLetter = copy[index++] + + const isValidLetter = [ + letter.toLowerCase(), + '#', + '+', + ':', + '.', + ' ', + ].includes(copyLetter) + + const isBlankSpace = +(letter === ' ') + const isValidCharacter = [isValidLetter, copyLetter === ' '][isBlankSpace] + + isValidCopy = [isValidCopy, isValidCharacter][+isValidCopy] + } + + return isValidCopy +} diff --git a/README.md b/README.md index e048642..24344ed 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ This repository contains the solutions to the challenges proposed by [@midudev]( | 09 | Switch the light | 🟢 | [Show](https://adventjs.dev/challenges/2023/9) | [Go](./2023/challenge-09/challenge-09.ts) | | 10 | Create your owm Christmas tree | 🟢 | [Show](https://adventjs.dev/challenges/2023/10) | [Go](./2023/challenge-10/challenge-10.ts) | | 11 | The studious elves | 🟠 | [Show](https://adventjs.dev/challenges/2023/11) | [Go](./2023/challenge-11/challenge-11.ts) | -| 12 | -- | -- | -- | -- | +| 12 | Is it a valid copy? | 🟠 | [Show](https://adventjs.dev/challenges/2023/12) | [Go](./2023/challenge-12/challenge-12.ts) | | 13 | -- | -- | -- | -- | | 14 | -- | -- | -- | -- | | 15 | -- | -- | -- | -- |