diff --git a/readme.md b/readme.md index 732d771..0a5a324 100644 --- a/readme.md +++ b/readme.md @@ -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 🛠️ diff --git a/src/challenges/20.ts b/src/challenges/20.ts new file mode 100644 index 0000000..327d400 --- /dev/null +++ b/src/challenges/20.ts @@ -0,0 +1,21 @@ +export function distributeGifts (weights: Array>): 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 +} diff --git a/src/tests/20.spec.ts b/src/tests/20.spec.ts new file mode 100644 index 0000000..d2c355e --- /dev/null +++ b/src/tests/20.spec.ts @@ -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) + }) +})