Skip to content

Commit

Permalink
feat(2023): add solution for challenge #10
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 10, 2023
1 parent fb705b8 commit d4ebf97
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 27 deletions.
35 changes: 35 additions & 0 deletions 2023/challenge-10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Challenge #10: Create your owm Christmas tree

What an idea Sam Elfman has had! He wants to offer a service that creates a **customized Christmas tree** 🎄 in a matter of seconds.

To create it, we are given a **string to form the tree** and a **number that indicates its height.**

Each character of the string represents an ornament of the tree, and we use them **cyclically** until we reach the indicated height. **At least, they will always pass us one.**

We must return a multiline **string** with the Christmas tree made with the ornaments, the indicated height **plus a final line with the trunk formed by the character `|`** in the center and, finally, a newline `\n`.

For example, if we receive the string `"123"` and the number `4` as height, we would have to build this tree:

```js
1
2 3
1 2 3
1 2 3 1 2
|
```

If we receive the string `*@o` and the number `3`, the tree we should return is:

```js
1
2 3
1 2 3
1 2 3 1 2
|
```

Note:

- **The tree should always be centered, for that reason add blank spaces to the left of each line.**
- **Create spaces only to the left of each line of the tree. Do not leave blank spaces to the right.**
- **The ornaments have a white space between them for separation.**
19 changes: 19 additions & 0 deletions 2023/challenge-10/challenge-10.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe } from 'vitest'
import { createChristmasTree } from './challenge-10'

const TEST_CASES: TestCases<[string, number], string> = [
{ args: ['x', 3], expected: ` x\n x x\nx x x\n |\n` },
{ args: ['xo', 4], expected: ` x\n o x\n o x o\nx o x o\n |\n` },
{
args: ['123', 5],
expected: ` 1\n 2 3\n 1 2 3\n 1 2 3 1\n2 3 1 2 3\n |\n`,
},
{ args: ['*@o', 3], expected: ` *\n @ o\n* @ o\n |\n` },
]

describe('Challenge #10: Create your owm Christmas tree', () => {
buildChallengeTestCases({
cases: TEST_CASES,
spreadFn: createChristmasTree,
})
})
21 changes: 21 additions & 0 deletions 2023/challenge-10/challenge-10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function createChristmasTree(ornaments: string, height: number) {
let result = ''
const spaces = ' '.repeat(height - 1)

const ornamentsArray = [...ornaments.repeat(height)].join(' ')
let currentPosition = 0

for (const index of new Array(height).keys()) {
const length = index + 1
const total = currentPosition + length

result +=
spaces.slice(index) +
ornamentsArray.slice(currentPosition * 2, total * 2 - 1) +
'\n'

currentPosition = total % ornaments.length
}

return result + spaces + '|\n'
}
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,33 @@ This repository contains the solutions to the challenges proposed by [@midudev](

### 🕹️ Challenges

| # | Challenge | Difficulty[^1] | Description | Solution |
| :-: | :-------------------------: | :------------: | :--------------------------------------------: | :---------------------------------------: |
| 01 | First gift repeated! | 🟢 | [Show](https://adventjs.dev/challenges/2023/1) | [Go](./2023/challenge-01/challenge-01.ts) |
| 02 | We start the factory | 🟢 | [Show](https://adventjs.dev/challenges/2023/2) | [Go](./2023/challenge-02/challenge-02.ts) |
| 03 | The naughty elf | 🟢 | [Show](https://adventjs.dev/challenges/2023/3) | [Go](./2023/challenge-03/challenge-03.ts) |
| 04 | Turn the parentheses around | 🟠 | [Show](https://adventjs.dev/challenges/2023/4) | [Go](./2023/challenge-04/challenge-04.ts) |
| 05 | Santa's CyberTruck | 🟠 | [Show](https://adventjs.dev/challenges/2023/5) | [Go](./2023/challenge-05/challenge-05.ts) |
| 06 | The reindeer on trial | 🟢 | [Show](https://adventjs.dev/challenges/2023/6) | [Go](./2023/challenge-06/challenge-06.ts) |
| 07 | The 3D boxes | 🟢 | [Show](https://adventjs.dev/challenges/2023/7) | [Go](./2023/challenge-07/challenge-07.ts) |
| 08 | Sorting the warehouse | 🟠 | [Show](https://adventjs.dev/challenges/2023/8) | [Go](./2023/challenge-08/challenge-08.ts) |
| 09 | Switch the light | 🟢 | [Show](https://adventjs.dev/challenges/2023/9) | [Go](./2023/challenge-09/challenge-09.ts) |
| 10 | -- | -- | -- | -- |
| 11 | -- | -- | -- | -- |
| 12 | -- | -- | -- | -- |
| 13 | -- | -- | -- | -- |
| 14 | -- | -- | -- | -- |
| 15 | -- | -- | -- | -- |
| 16 | -- | -- | -- | -- |
| 17 | -- | -- | -- | -- |
| 18 | -- | -- | -- | -- |
| 19 | -- | -- | -- | -- |
| 20 | -- | -- | -- | -- |
| 21 | -- | -- | -- | -- |
| 22 | -- | -- | -- | -- |
| 23 | -- | -- | -- | -- |
| 24 | -- | -- | -- | -- |
| 25 | -- | -- | -- | -- |
| # | Challenge | Difficulty[^1] | Description | Solution |
| :-: | :----------------------------: | :------------: | :---------------------------------------------: | :---------------------------------------: |
| 01 | First gift repeated! | 🟢 | [Show](https://adventjs.dev/challenges/2023/1) | [Go](./2023/challenge-01/challenge-01.ts) |
| 02 | We start the factory | 🟢 | [Show](https://adventjs.dev/challenges/2023/2) | [Go](./2023/challenge-02/challenge-02.ts) |
| 03 | The naughty elf | 🟢 | [Show](https://adventjs.dev/challenges/2023/3) | [Go](./2023/challenge-03/challenge-03.ts) |
| 04 | Turn the parentheses around | 🟠 | [Show](https://adventjs.dev/challenges/2023/4) | [Go](./2023/challenge-04/challenge-04.ts) |
| 05 | Santa's CyberTruck | 🟠 | [Show](https://adventjs.dev/challenges/2023/5) | [Go](./2023/challenge-05/challenge-05.ts) |
| 06 | The reindeer on trial | 🟢 | [Show](https://adventjs.dev/challenges/2023/6) | [Go](./2023/challenge-06/challenge-06.ts) |
| 07 | The 3D boxes | 🟢 | [Show](https://adventjs.dev/challenges/2023/7) | [Go](./2023/challenge-07/challenge-07.ts) |
| 08 | Sorting the warehouse | 🟠 | [Show](https://adventjs.dev/challenges/2023/8) | [Go](./2023/challenge-08/challenge-08.ts) |
| 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 | -- | -- | -- | -- |
| 12 | -- | -- | -- | -- |
| 13 | -- | -- | -- | -- |
| 14 | -- | -- | -- | -- |
| 15 | -- | -- | -- | -- |
| 16 | -- | -- | -- | -- |
| 17 | -- | -- | -- | -- |
| 18 | -- | -- | -- | -- |
| 19 | -- | -- | -- | -- |
| 20 | -- | -- | -- | -- |
| 21 | -- | -- | -- | -- |
| 22 | -- | -- | -- | -- |
| 23 | -- | -- | -- | -- |
| 24 | -- | -- | -- | -- |
| 25 | -- | -- | -- | -- |

</details>

Expand Down

0 comments on commit d4ebf97

Please sign in to comment.