Skip to content

Commit

Permalink
feat: 'Directions Reduction'
Browse files Browse the repository at this point in the history
Add 'Directions Reduction' kata
  • Loading branch information
marcobiedermann committed Aug 9, 2020
1 parent ac78f24 commit 6443a0b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
18 changes: 18 additions & 0 deletions kata/5 kyu/directions-reduction/index.test.ts
@@ -0,0 +1,18 @@
import dirReduc from '.';

describe('dirReduc', () => {
it('should return reduced directions', () => {
expect.assertions(3);

expect(dirReduc(['NORTH', 'SOUTH', 'SOUTH', 'EAST', 'WEST', 'NORTH', 'WEST'])).toStrictEqual([
'WEST',
]);
expect(dirReduc(['NORTH', 'WEST', 'SOUTH', 'EAST'])).toStrictEqual([
'NORTH',
'WEST',
'SOUTH',
'EAST',
]);
expect(dirReduc(['NORTH', 'SOUTH', 'EAST', 'WEST', 'EAST', 'WEST'])).toStrictEqual([]);
});
});
21 changes: 21 additions & 0 deletions kata/5 kyu/directions-reduction/index.ts
@@ -0,0 +1,21 @@
type Direction = 'EAST' | 'NORTH' | 'SOUTH' | 'WEST';

const counterDirections = {
EAST: 'WEST',
NORTH: 'SOUTH',
SOUTH: 'NORTH',
WEST: 'EAST',
};

function dirReduc(arr: Direction[]): Direction[] {
return arr.reduce((accumulator: Direction[], currentValue) => {
const lastDirection = accumulator[accumulator.length - 1];
const counterDirection = counterDirections[currentValue];

return lastDirection === counterDirection
? accumulator.slice(0, accumulator.length - 1)
: [...accumulator, currentValue];
}, []);
}

export default dirReduc;
77 changes: 77 additions & 0 deletions kata/5 kyu/directions-reduction/readme.md
@@ -0,0 +1,77 @@
# [Directions Reduction](https://www.codewars.com/kata/550f22f4d758534c1100025a)

### Once upon a time, on a way through the old wild mountainous west,…

… a man was given directions to go from one point to another. The directions were "NORTH", "SOUTH", "WEST", "EAST". Clearly "NORTH" and "SOUTH" are opposite, "WEST" and "EAST" too.

Going to one direction and coming back the opposite direction _right away_ is a needless effort. Since this is the wild west, with dreadfull weather and not much water, it's important to save yourself some energy, otherwise you might die of thirst!

### How I crossed a _mountain_ desert the smart way.

The directions given to the man are, for example, the following (depending on the language):

```
["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"].
```

or

```
{ "NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST" };
```

or

```
[North, South, South, East, West, North, West]
```

You can immediatly see that going "NORTH" and _immediately_ "SOUTH" is not reasonable, better stay to the same place!
So the task is to give to the man a simplified version of the plan. A better plan in this case is simply:

```
["WEST"]
```

or

```
{ "WEST" }
```

or

```
[West]
```

### Other examples:

In `["NORTH", "SOUTH", "EAST", "WEST"]`, the direction `"NORTH" + "SOUTH"` is going north and coming back _right away_.

The path becomes `["EAST", "WEST"]`, now `"EAST"` and `"WEST"` annihilate each other, therefore, the final result is `[]` (nil in Clojure).

In ["NORTH", "EAST", "WEST", "SOUTH", "WEST", "WEST"], "NORTH" and "SOUTH" are _not_ directly opposite but they become directly opposite after the reduction of "EAST" and "WEST" so the whole path is reducible to ["WEST", "WEST"].

### Task

Write a function `dirReduc` which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N _side by side_).

- The Haskell version takes a list of directions with `data Direction = North | East | West | South`.
-The Clojure version returns nil when the path is reduced to nothing.
-The Rust version takes a slice of `enum Direction {NORTH, SOUTH, EAST, WEST}`.

### See more examples in "Sample Tests:"

## Notes

- Not all paths can be made simpler.
The path ["NORTH", "WEST", "SOUTH", "EAST"] is not reducible. "NORTH" and "WEST", "WEST" and "SOUTH", "SOUTH" and "EAST" are not _directly_ opposite of each other and can't become such. Hence the result path is itself : ["NORTH", "WEST", "SOUTH", "EAST"].

* if you want to translate, please ask before translating.

---

## Tags

- Fundamentals

0 comments on commit 6443a0b

Please sign in to comment.