-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path(medium) the Last Crusade - Episode 1.js
75 lines (71 loc) · 2.29 KB
/
(medium) the Last Crusade - Episode 1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/** the Last Crusade - Ep. 1 (medium) https://www.codingame.com/training/medium/the-last-crusade-episode-1
* This puzzle makes you use an associative array that make a link between
* arbitrary types and directions. You also need to store some values in a
* 2D array.
*
* Statement:
* The goal of this puzzle is to predict the path that a character will take
* in a labyrinth according to the topology of the rooms. The resolution
* of this exercise intensively focus on the correct usage of associative
* arrays. If you can manage them correctly and creates the right
* associations, your final code could be quite short.
*
* Story:
* Indiana is trapped in a tunnel, help him escape!
* In this first level, you just have to get familiar with how the tunnel
* works: your goal is simply to predict Indiana movements within this
* tunnel.
**/
// W - Number of columns.
// H - Number of rows.
let [W, H] = readline().split` `.map(Number);
const maze = []; // Maze
while (H--) {
// Represents a line in the grid and contains W integers.
// Each integer represents one room of a given type.
maze.push(readline());
}
// The coordinate along the X axis of the exit
// (not useful for this first mission, but must be read).
const EX = +readline();
// game loop
while (true) {
inputs = readline().split` `;
const [XI, YI, POS] = [+inputs[0], +inputs[1], inputs[2]];
const currentLevel = maze[YI].split` `;
let [XT, YT] = [XI, YI];
switch(currentLevel[XI]) {
case '1':
case '3':
case '7':
case '8':
case '9':
case '12':
case '13':
YT += 1;
break;
case '2':
XT += POS === 'LEFT' ? 1 : -1;
break;
case '4':
(POS === 'TOP') ? XT -= 1 : YT +=1;
break;
case '5':
(POS === 'TOP') ? XT += 1 : YT +=1;
break;
case '6':
XT += (POS === 'TOP' && EX > XI) || POS === 'LEFT' ? 1 : -1;
break;
case '10':
XT -= 1;
break;
case '11':
XT += 1;
break;
default:
break;
}
// One line containing the X Y coordinates of the room
// in which you believe Indy will be on the next turn.
print(`${XT} ${YT}`);
}