Skip to content

Commit

Permalink
🗺️ added challenge 25: calculating distances
Browse files Browse the repository at this point in the history
  • Loading branch information
jamerrq committed Dec 25, 2023
1 parent a9e218d commit 2e33d16
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
34 changes: 18 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Advent JS version 2023 🎄

> [!NOTE]
> Si este repositorio llega a 16+ 🌟 antes de que termine el año, estaré desarrollando una librería en npm para resolver los retos del [AdventJS](https://adventjs.dev/) 2023 de manera local.
> [!NOTE] Si este repositorio llega a 16+ 🌟 antes de que termine el año, estaré
> desarrollando una librería en npm para resolver los retos del
> [AdventJS](https://adventjs.dev/) 2023 de manera local.
Soluciones bizarras a los retos del [AdventJS](https://adventjs.dev/) 2023.

Expand Down Expand Up @@ -29,21 +30,22 @@ versión estaré usando TypeScript, en su versión 5.3.2.
| 07 | [**📦 Las cajas en 3d**](https://adventjs.dev/es/challenges/2023/7) | 🟢 | [TS](./src/challenges/07.ts) | [SPEC](./src/tests/07.spec.ts) |
| 08 | [**🏭 Ordenando el almacen**](https://adventjs.dev/es/challenges/2023/8) | 🟠 | [TS](./src/challenges/08.ts) | [SPEC](./src/tests/08.spec.ts) |
| 09 | [**🚦 Alterna las luces**](https://adventjs.dev/es/challenges/2023/9) | 🟢 | [TS](./src/challenges/09.ts) | [SPEC](./src/tests/09.spec.ts) |
| 10 | [**🎄 Crea tu propio árbol de navidad**](https://adventjs.dev/es/challenges/2023/10) | 🟢 | [TS](./src/challenges/10.ts) | [SPEC](./src/tests/10.spec.ts) |
| 11 | [**📖 Los elfos estudiosos**](https://adventjs.dev/es/challenges/2023/11) | 🟠 | [TS](./src/challenges/11.ts) | [SPEC](./src/tests/11.spec.ts) |
| 12 | [**📸 Es una copia válida?**](https://adventjs.dev/es/challenges/2023/12) | 🟠 | [TS](./src/challenges/12.ts) | [SPEC](./src/tests/12.spec.ts) |
| 13 | [**⌚ Calculando el tiempo**](https://adventjs.dev/es/challenges/2023/13) | 🟢 | [TS](./src/challenges/13.ts) | [SPEC](./src/tests/13.spec.ts) |
| 14 | [**🚨 Evita la alarma**](https://adventjs.dev/es/challenges/2023/14) | 🟠 | [TS](./src/challenges/14.ts) | [SPEC](./src/tests/14.spec.ts) |
| 15 | [**↔️ Robot autónomo**](https://adventjs.dev/es/challenges/2023/15) | 🟠 | [TS](./src/challenges/15.ts) | [SPEC](./src/tests/15.spec.ts) |
| 10 | [**🎄 Crea tu árbol de navidad**](https://adventjs.dev/es/challenges/2023/10) | 🟢 | [TS](./src/challenges/10.ts) | [SPEC](./src/tests/10.spec.ts) |
| 11 | [**📖 Los elfos estudiosos**](https://adventjs.dev/es/challenges/2023/11) | 🟠 | [TS](./src/challenges/11.ts) | [SPEC](./src/tests/11.spec.ts) |
| 12 | [**📸 Es una copia válida?**](https://adventjs.dev/es/challenges/2023/12) | 🟠 | [TS](./src/challenges/12.ts) | [SPEC](./src/tests/12.spec.ts) |
| 13 | [**⌚ Calculando el tiempo**](https://adventjs.dev/es/challenges/2023/13) | 🟢 | [TS](./src/challenges/13.ts) | [SPEC](./src/tests/13.spec.ts) |
| 14 | [**🚨 Evita la alarma**](https://adventjs.dev/es/challenges/2023/14) | 🟠 | [TS](./src/challenges/14.ts) | [SPEC](./src/tests/14.spec.ts) |
| 15 | [**↔️ Robot autónomo**](https://adventjs.dev/es/challenges/2023/15) | 🟠 | [TS](./src/challenges/15.ts) | [SPEC](./src/tests/15.spec.ts) |
| 16 | [**❌ Viernes de deploy**](https://adventjs.dev/es/challenges/2023/16) | 🟢 | [TS](./src/challenges/16.ts) | [SPEC](./src/tests/16.spec.ts) |
| 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) |
| 21 | [**🪐 Mensaje Binario**](https://adventjs.dev/es/challenges/2023/21) | 🟠 | [TS](./src/challenges/21.ts) | [SPEC](./src/tests/21.spec.ts) |
| 22 | [**🚂 Lenguaje de programación**](https://adventjs.dev/es/challenges/2023/22) | 🟢 | [TS](./src/challenges/22.ts) | [SPEC](./src/tests/22.spec.ts) |
| 23 | [**🍽️ Cena de navidad**](https://adventjs.dev/es/challenges/2023/23) | 🟢 | [TS](./src/challenges/23.ts) | [SPEC](./src/tests/23.spec.ts) |
| 24 | [**🪜 Salta en las escaleras**](https://adventjs.dev/es/challenges/2023/24) | 🟠 | [TS](./src/challenges/24.ts) | [SPEC](./src/tests/24.spec.ts) |
| 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) |
| 21 | [**🪐 Mensaje Binario**](https://adventjs.dev/es/challenges/2023/21) | 🟠 | [TS](./src/challenges/21.ts) | [SPEC](./src/tests/21.spec.ts) |
| 22 | [**🚂 Lenguaje de programación**](https://adventjs.dev/es/challenges/2023/22) | 🟢 | [TS](./src/challenges/22.ts) | [SPEC](./src/tests/22.spec.ts) |
| 23 | [**🍽️ Cena de navidad**](https://adventjs.dev/es/challenges/2023/23) | 🟢 | [TS](./src/challenges/23.ts) | [SPEC](./src/tests/23.spec.ts) |
| 24 | [**🪜 Salta en las escaleras**](https://adventjs.dev/es/challenges/2023/24) | 🟠 | [TS](./src/challenges/24.ts) | [SPEC](./src/tests/24.spec.ts) |
| 25 | [**🗺️ Calculando Distancias**](https://adventjs.dev/es/challenges/2023/25) | 🟠 | [TS](./src/challenges/25.ts) | [SPEC](./src/tests/25.spec.ts) |

## Herramientas utilizadas 🛠️

Expand Down
21 changes: 21 additions & 0 deletions src/challenges/25.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function travelDistance (map: string): number {
const positions: Record<string, [number, number]> = {}
for (const [rowIndex, row] of map.split('\n').entries()) {
for (const [colIndex, col] of row.replace('S', '0').split('').entries()) {
if (/[\d|S]/.test(col)) {
positions[col] = [rowIndex, colIndex]
}
}
}
let ans = 0
for (const x of Object.keys(positions)) {
if (x === '0') {
continue
}
const [currentRow, currentCol] = positions[x]
const [prevRow, prevCol] = positions[+x - 1]
ans += Math.abs(currentRow - prevRow)
ans += Math.abs(currentCol - prevCol)
}
return ans
}
67 changes: 67 additions & 0 deletions src/tests/25.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { test, expectTypeOf, expect, describe } from 'vitest'
import { travelDistance } from '../challenges/25'

describe('Challenge #25', () => {
test('Test #01', () => {
expectTypeOf(travelDistance).returns.toEqualTypeOf(0)
})

test('Test #02', () => {
const received = travelDistance(
`.....1....
..S.......
..........
....3.....
......2...`)
const expected = 12
expect(received).toEqual(expected)
})

test('Test #03', () => {
const received = travelDistance('..S.1...')
const expected = 2
expect(received).toEqual(expected)
})

test('Test #04', () => {
const received = travelDistance(
`.....2....
..S.......
..........
....1.....
......3...`
)
const expected = 13
expect(received).toEqual(expected)
})

test('Test #05', () => {
const received = travelDistance(
`3....1....
..S.......
.........2
..........
......4...`
)
const expected = 31
expect(received).toEqual(expected)
})

test('Test #06', () => {
const received = travelDistance('S1')
const expected = 1
expect(received).toEqual(expected)
})

test('Test #07', () => {
const received = travelDistance('1....S')
const expected = 5
expect(received).toEqual(expected)
})

test('Test #08', () => {
const received = travelDistance('S12....3')
const expected = 7
expect(received).toEqual(expected)
})
})

0 comments on commit 2e33d16

Please sign in to comment.