Skip to content

Commit

Permalink
🏋️‍♂️ added challenge 20: distribute the weight
Browse files Browse the repository at this point in the history
  • Loading branch information
jamerrq committed Dec 20, 2023
1 parent 0de7755 commit 8c01f61
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Para esta versión estaré usando TypeScript, en su versión 5.3.2.
| 17 | [**Optimizando el alquiler**](https://adventjs.dev/es/challenges/2023/17) | 🟢 | [TS](./src/challenges/17.ts) | [SPEC](./src/tests/17.spec.ts) |
| 18 | [**El reloj digital**](https://adventjs.dev/es/challenges/2023/18) | 🔴 | [TS](./src/challenges/18.ts) | [SPEC](./src/tests/18.spec.ts) |
| 19 | [**Enfrenta el sabotage**](https://adventjs.dev/es/challenges/2023/19) | 🟠 | [TS](./src/challenges/19.ts) | [SPEC](./src/tests/19.spec.ts) |
| 20 | [**Distribuye el peso**](https://adventjs.dev/es/challenges/2023/20) | 🔴 | [TS](./src/challenges/20.ts) | [SPEC](./src/tests/20.spec.ts) |

## Herramientas utilizadas 🛠️

Expand Down
21 changes: 21 additions & 0 deletions src/challenges/20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function distributeGifts (weights: Array<Array<number | null>>): number[][] {
const n = weights.length; const m = weights[0].length
const gifts: number[][] = Array(n).fill(null).map(() => Array(m).fill(0))
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
const up = weights[i - 1]?.[j]
const lf = weights[i][j - 1]
const rg = weights[i][j + 1]
const dw = weights[i + 1]?.[j]
const ct = weights[i][j]
let n = 0; let sum = 0
for (const x of [up, lf, rg, dw, ct]) {
const z = +(x != null)
sum += +([0, x][z] as number)
n += z
}
gifts[i][j] = Math.round(sum / n)
}
}
return gifts
}
140 changes: 140 additions & 0 deletions src/tests/20.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { test, expectTypeOf, expect, describe } from 'vitest'
import { distributeGifts } from '../challenges/20'

describe('Challenge #20', () => {
test('Test #01', () => {
expectTypeOf(distributeGifts).returns.toEqualTypeOf([])
})

test('Test #02', () => {
const received = distributeGifts([
[4, 5, 1],
[6, null, 3],
[8, null, 4]
])
const expected = [
[
5,
3,
3
],
[
6,
5,
3
],
[
7,
6,
4
]
]
expect(received).toEqual(expected)
})

test('Test #03', () => {
const received = distributeGifts([
[2, null],
[null, 3]
])
const expected = [
[
2,
3
],
[
3,
3
]
]
expect(received).toEqual(expected)
})

test('Test #04', () => {
const received = distributeGifts([
[2, 1, 1],
[3, 4, null]
])
const expected = [
[
2,
2,
1
],
[
3,
3,
3
]
]
expect(received).toEqual(expected)
})

test('Test #05', () => {
const received = distributeGifts([
[null, 5],
[3, null]
])
const expected = [
[
4,
5
],
[
3,
4
]
]
expect(received).toEqual(expected)
})

test('Test #06', () => {
const received = distributeGifts([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
const expected = [
[
2,
3,
4
],
[
4,
5,
6
],
[
6,
7,
8
]
]
expect(received).toEqual(expected)
})

test('Test #07', () => {
const received = distributeGifts([
[null, 1, null, 1, null],
[1, null, 1, null, 1]
])
const expected = [
[
1,
1,
1,
1,
1
],
[
1,
1,
1,
1,
1
]
]
expect(received).toEqual(expected)
})
})

0 comments on commit 8c01f61

Please sign in to comment.