Skip to content

Commit

Permalink
feat(2023): add solution for challenge #12
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 12, 2023
1 parent 1f8faf3 commit f85c1c4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
37 changes: 37 additions & 0 deletions 2023/challenge-12/README.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions 2023/challenge-12/challenge-12.test.ts
Original file line number Diff line number Diff line change
@@ -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,
})
})
24 changes: 24 additions & 0 deletions 2023/challenge-12/challenge-12.ts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | -- | -- | -- | -- |
Expand Down

0 comments on commit f85c1c4

Please sign in to comment.