Skip to content

Commit dbb4d79

Browse files
* docs: kata description * docs: sync progress * feat: kata/treasure-hunt-with-clues --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent 5f63443 commit dbb4d79

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ slug.
2525

2626
| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
2727
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
28-
| 0 | 1 | 2 | 26 | 48 | 439 | 600 | 225 | 56 | 82 |
28+
| 0 | 1 | 2 | 26 | 48 | 439 | 601 | 225 | 56 | 82 |
2929

3030
**Note:** The source code is written in Java 17 and may use language features that are incompatible
3131
with Java 8, 11.

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@
582582
- [Thinking & Testing: A and B?](thinking-and-testing-a-and-b)
583583
- [Tidy Number (Special Numbers Series #9)](tidy-number-special-numbers-series-number-9)
584584
- [ToLeetSpeak](toleetspeak)
585+
- [Treasure Hunt with Clues](treasure-hunt-with-clues)
585586
- [Triangle area](triangle-area)
586587
- [Triangles from Dots](triangles-from-dots)
587588
- [Triangular Treasure](triangular-treasure)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Treasure Hunt with Clues](https://www.codewars.com/kata/treasure-hunt-with-clues "https://www.codewars.com/kata/66c0fec80a2a28b2a451d408")
2+
3+
Inputs:
4+
5+
A `n × n` grid with `1 >= n >= 9`, and 2 integers `row, col` with `1 <= row, col <= n`, indicating the starting position of the treasure
6+
hunt.
7+
8+
Clues:
9+
10+
Every cell of the grid contains a number between `11` and <code>n<sup>2</sup></code>. These values provide the coordinates of the
11+
next cell to search. The treasure is found in a cell whose value matches its location. (The clue says "stay where you are"!) There will
12+
always be treasure reachable from the starting position.
13+
14+
Output:
15+
16+
The value in the treasure cell reached by following the clues from the starting position.
17+
18+
Example:
19+
20+
Consider the grid below, with starting position <code>row=3,col=4.</code>
21+
22+
```
23+
34 21 32 44 25
24+
21 41 43 14 31
25+
31 45 52 42 23
26+
33 15 51 44 35
27+
21 52 33 13 44
28+
```
29+
30+
Indexes start from 1, so the value in cell <code>3,4</code> is <code>42.</code> Thus the next clue is found in cell <code>4,2.</code> The
31+
value there is <code>15.</code> Seeking in cell <code>1,5</code> uncovers the clue <code>25,</code> and cell <code>2,5</code>
32+
contains <code>31.</code> Since the value in cell <code>3,1</code> is <code>31,</code> that is the location of the treasure.
33+
34+
Source: This kata extends a problem found on [GitHub](https://github.com/HoanVanHuynh/Programming-Practice-Problems--treasure-hunts).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
interface TreasureHunt {
2+
static int findTreasure(int[][] grid, int row, int col) {
3+
int cell;
4+
while ((cell = grid[row - 1][col - 1]) != 10 * row + col) {
5+
row = cell / 10;
6+
col = cell % 10;
7+
}
8+
return cell;
9+
}
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class TreasureHuntTest {
6+
@Test
7+
void sample() {
8+
assertEquals(11, TreasureHunt.findTreasure(new int[][]{{11}}, 1, 1));
9+
assertEquals(21, TreasureHunt.findTreasure(new int[][]{{21, 21, 21}, {21, 21, 21}, {21, 21, 21}}, 2, 3));
10+
11+
int[][] allTheWay = {{12, 13, 14, 15, 21}, {22, 23, 24, 25, 31}, {32, 33, 34, 35, 41}, {42, 43, 44, 45, 51}, {52, 53, 54, 55, 55}};
12+
assertEquals(55, TreasureHunt.findTreasure(allTheWay, 1, 1));
13+
14+
int[][] meandering = {{34, 24, 32, 41, 25}, {14, 12, 43, 44, 31}, {54, 45, 52, 34, 23}, {33, 15, 51, 53, 35}, {21, 55, 11, 13, 22}};
15+
assertEquals(34, TreasureHunt.findTreasure(meandering, 1, 5));
16+
17+
int[][] grid5x5 = {{34, 21, 32, 44, 25}, {21, 41, 43, 14, 31}, {31, 45, 52, 42, 23}, {33, 15, 51, 44, 35}, {21, 52, 33, 13, 44}};
18+
assertEquals(31, TreasureHunt.findTreasure(grid5x5, 3, 4));
19+
assertEquals(21, TreasureHunt.findTreasure(grid5x5, 1, 2));
20+
21+
int[][] grid9x9 = {
22+
{34, 21, 98, 14, 25, 29, 32, 18, 25},
23+
{21, 12, 43, 34, 31, 12, 43, 34, 93},
24+
{67, 45, 52, 42, 23, 45, 52, 42, 23},
25+
{38, 76, 51, 53, 35, 15, 51, 53, 35},
26+
{21, 15, 51, 53, 98, 15, 51, 53, 35},
27+
{34, 18, 62, 41, 25, 24, 37, 41, 25},
28+
{71, 12, 43, 34, 31, 29, 43, 34, 31},
29+
{55, 45, 83, 42, 73, 45, 52, 42, 23},
30+
{33, 15, 55, 53, 36, 15, 51, 98, 35}
31+
};
32+
assertEquals(98, TreasureHunt.findTreasure(grid9x9, 1, 1));
33+
}
34+
}

0 commit comments

Comments
 (0)