From d4ebf97f8dd43af7d945af41b07e7bdba98e409d Mon Sep 17 00:00:00 2001 From: iswilljr Date: Sun, 10 Dec 2023 10:42:37 -0500 Subject: [PATCH] feat(2023): add solution for challenge #10 --- 2023/challenge-10/README.md | 35 +++++++++++++++++ 2023/challenge-10/challenge-10.test.ts | 19 +++++++++ 2023/challenge-10/challenge-10.ts | 21 ++++++++++ README.md | 54 +++++++++++++------------- 4 files changed, 102 insertions(+), 27 deletions(-) create mode 100644 2023/challenge-10/README.md create mode 100644 2023/challenge-10/challenge-10.test.ts create mode 100644 2023/challenge-10/challenge-10.ts diff --git a/2023/challenge-10/README.md b/2023/challenge-10/README.md new file mode 100644 index 0000000..7697b9c --- /dev/null +++ b/2023/challenge-10/README.md @@ -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.** diff --git a/2023/challenge-10/challenge-10.test.ts b/2023/challenge-10/challenge-10.test.ts new file mode 100644 index 0000000..b10b890 --- /dev/null +++ b/2023/challenge-10/challenge-10.test.ts @@ -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, + }) +}) diff --git a/2023/challenge-10/challenge-10.ts b/2023/challenge-10/challenge-10.ts new file mode 100644 index 0000000..146bf85 --- /dev/null +++ b/2023/challenge-10/challenge-10.ts @@ -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' +} diff --git a/README.md b/README.md index 0cf8b36..0a4574f 100644 --- a/README.md +++ b/README.md @@ -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 | -- | -- | -- | -- |