Skip to content

Commit

Permalink
↔️ added challenge 15
Browse files Browse the repository at this point in the history
  • Loading branch information
jamerrq committed Dec 15, 2023
1 parent 75c6a85 commit 1e3ca69
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Para esta versión estaré usando TypeScript, en su versión 5.3.2.
| 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) |

## Herramientas utilizadas 🛠️

Expand Down
14 changes: 14 additions & 0 deletions src/challenges/15.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function autonomousDrive (store: string[], movements: string[]): string[] {
let row = store.findIndex(_ => _.includes('!'))
let col = store[row].indexOf('!')
store[row] = store[row].replace('!', '.')
for (const mv of movements) {
col += +(store[row][col + 1] === '.') * +(mv === 'R')
col -= +(store[row][col - 1] === '.') * +(mv === 'L')
row += +(store.at(row + 1)?.at(col) === '.') * +(mv === 'D')
row -= +(store.at(row - 1)?.at(col) === '.') * +(mv === 'U')
}
const str = store[row]
store[row] = str.substring(0, col) + '!' + str.substring(col + 1)
return store
}
83 changes: 83 additions & 0 deletions src/tests/15.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { test, expectTypeOf, expect, describe } from 'vitest'
import { autonomousDrive } from '../challenges/15'

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

test('Test #02', () => {
const received = autonomousDrive(['..!....'], ['R', 'L'])
const expected = [
'..!....'
]
expect(received).toEqual(expected)
})

test('Test #03', () => {
const received = autonomousDrive(['!..', '***'], ['U', 'L'])
const expected = [
'!..',
'***'
]
expect(received).toEqual(expected)
})

test('Test #04', () => {
const received = autonomousDrive([
'..!....',
'......*'
], ['R', 'D', 'L'])
const expected = [
'.......',
'..!...*'
]
expect(received).toEqual(expected)
})

test('Test #05', () => {
const received = autonomousDrive([
'*..!..*',
'*.....*'
], ['R', 'R', 'R', 'D', 'D'])
const expected = [
'*.....*',
'*....!*'
]
expect(received).toEqual(expected)
})

test('Test #06', () => {
const received = autonomousDrive(['***', '.!.', '***'], ['D', 'U', 'R', 'R', 'R'])
const expected = [
'***',
'..!',
'***'
]
expect(received).toEqual(expected)
})

test('Test #07', () => {
const received = autonomousDrive(['***', '*!*', '***'], ['D', 'U', 'R', 'L'])
const expected = [
'***',
'*!*',
'***'
]
expect(received).toEqual(expected)
})

test('Test #08', () => {
const received = autonomousDrive([
'.**.*.*.',
'.***....',
'..!.....'
], ['D', 'U', 'R', 'R', 'R'])
const expected = [
'.**.*.*.',
'.***....',
'.....!..'
]
expect(received).toEqual(expected)
})
})

0 comments on commit 1e3ca69

Please sign in to comment.