Skip to content

Commit

Permalink
feat(2023): add solution for challenge #21
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 21, 2023
1 parent 2caf424 commit b4d87c5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
24 changes: 24 additions & 0 deletions 2023/challenge-21/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Challenge #21: Binary message

The elves are receiving strange binary messages from Mars 🪐. Are the aliens trying to communicate with them? 👽

**The message that arrives is an array of 0s and 1s**. It seems they have found a pattern… To make sure, they want to **find the longest segment of the string where the number of 0s and 1s is equal.**

```js
findBalancedSegment([1, 1, 0, 1, 1, 0, 1, 1])
// |________|
// position of segment: [2, 5]
// longest balanced
// of 0s and 1s

findBalancedSegment([1, 1, 0])
// |__|
// [1, 2]

findBalancedSegment([1, 1, 1])
// no balanced segments: []
```

Keep in mind that if there is more than one balanced pattern, **you should return the longest and the first one you find from left to right.**

They say that if they find the pattern, they will be able to send a message back to Mars 🚀. It seems that they have to send it to `https://mars.codes`.
20 changes: 20 additions & 0 deletions 2023/challenge-21/challenge-21.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe } from 'vitest'
import { findBalancedSegment } from './challenge-21'

const TEST_CASES: TestCases<number[], number[]> = [
{ args: [1, 1, 0, 1, 1, 0, 1, 1], expected: [2, 5] },
{ args: [1, 1, 0], expected: [1, 2] },
{ args: [1, 1, 1], expected: [] },
{ args: [1, 0, 1], expected: [0, 1] },
{ args: [1, 0, 1, 0], expected: [0, 3] },
{ args: [1, 1, 0, 1, 0, 1], expected: [1, 4] },
{ args: [1, 0, 0, 0, 1, 1, 1, 0, 0, 0], expected: [0, 7] },
{ args: [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1], expected: [5, 10] },
]

describe('Challenge #21: Binary message', () => {
buildChallengeTestCases({
cases: TEST_CASES,
fn: findBalancedSegment,
})
})
27 changes: 27 additions & 0 deletions 2023/challenge-21/challenge-21.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function findBalancedSegment(message: number[]) {
const segment = [0, 0]

for (const [i, binary] of message.entries()) {
let ones = binary
let zeros = +(binary === 0)
const currentSegment = [i, i]

for (const [j, number] of message.slice(i + 1).entries()) {
ones += number
zeros += +(number === 0)

if (ones === zeros) {
currentSegment[1] = j + i + 1
}
}

if (currentSegment[1] - currentSegment[0] > segment[1] - segment[0]) {
segment[0] = currentSegment[0]
segment[1] = currentSegment[1]
}
}

if (segment[1] === 0) return []

return segment
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
| 18 | The digital clock | 🔴 | [Show](https://adventjs.dev/challenges/2023/18) | [Go](./2023/challenge-18/challenge-18.ts) |
| 19 | Face the sabotage | 🟠 | [Show](https://adventjs.dev/challenges/2023/19) | [Go](./2023/challenge-19/challenge-19.ts) |
| 20 | Distribute the weight | 🔴 | [Show](https://adventjs.dev/challenges/2023/20) | [Go](./2023/challenge-20/challenge-20.ts) |
| 21 | -- | -- | -- | -- |
| 21 | Binary message | 🟠 | [Show](https://adventjs.dev/challenges/2023/21) | [Go](./2023/challenge-21/challenge-21.ts) |
| 22 | -- | -- | -- | -- |
| 23 | -- | -- | -- | -- |
| 24 | -- | -- | -- | -- |
Expand Down

0 comments on commit b4d87c5

Please sign in to comment.