Created
March 18, 2018 17:38
-
-
Save niradler/7ed9de89190a47de2df126ea24a78381 to your computer and use it in GitHub Desktop.
wix online test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #1 | |
const str1 = `((())))((()())(())(())((())(()()(((()((()))()()(((((((((()())(()()(((((()()))))))(())(())(()))()()))((()((()(()((()())(()())))(()(()))(()))())))()))(())()()((((((())()))))()()((()((())())((((((()())())()))((()))))()()))(((()))((((()))))))))()((((((()(((())())))()())(()))()()()())(()()))))(()))))(((()(()))(()(()())((())))(()((())(()()((()())(()))()((()()(())((((()((()))(()()))(())()((())(()())()(()()((()(()()()))()((()(()))())((()))(())(((()))(((())((()())(())(())((())()()))()((((((())(()))(((()(((((()))()(())()())))((()))))(((()(()())(())()()()))((((((())))(()((((())()())())()())(((((((((())))(()()()(((()((()((())())(((()()))(()(()((((()((()(()())))()((())()(((((()(((((()()())(()(()()((()))())((((())()()))(()(()(()(()((((())))()((((()))())()((((())))))((((()((((()(())))(()()())((()())))))(()))(())()))())(((()))())(()()))()()((())())()())((()(()())(()))()()((())((()())(()((()((())))(())()())(())(()())()()())((())()((((((())))))))))(()()((())(()))()())((())())()))((())())(((((((()(()))(((()()))))))((())())()()(()(()())))((())(())((()))))())))))()())(()(((((()))))(()(((()()))((((()))((()(()((()))((((((((((()))))(())))(((()(())()))(()((()())))()()(((((((()))()))((()(((()))((()((((())((())(()(()())())()()))()())((((()))))(()(((())(()((())((())))))()))))((()(((()))(((())))(()((()(()(()()()(((())())())))((()())(()()(((())(()(()()())(()()(((()()()((((()))))(()))(()()()))((()()((())()))())))())()(())(())(((())()()))())(())))())()((())()())))(()))()())((())())(()))())(()()()())())))()(()))(()))())(()())))))(((()(()(()))()))())))))))()))))))()(()()(()()()))())(())(()))(((((()(()((()))())))())))(()))(((())((()()(()()))((()(((())()` | |
let i = 0 ; | |
for (let key of str1) { | |
if(key == '(') | |
i++; | |
else if(key == ')') | |
i--; | |
} | |
console.log('the floor is '+ i) | |
// #2 | |
const str2 = `>^<v>^<^<v<>>^>v><>^><^><v<^<v<<v>>>v>v><^^v<>>^^^>v><<^<<v>><v^v^<^>^v<vv>v^^^>^^>^>^><v^vv><<>>>>v^v<^><<>v<><v<vv^^v>>^>v>>v^<>^v^>>^^<><>^<<>^v<>>^>^<<^^><<<v<<v<<>><><^<vv<>><>v>vvv<<<<<^>>^>^vvv<^v^v<vv^v<>^<><^^^vv<v>v^><^><<^>>^>^vv><><^v^<v>v^v><v<^<^<<vv^^>^<v^>^v^v>^<v<<<^^^<v^<<^<<v<^><>>v^<^vv<vv>>v^^<<v<^<>>^^>>>>v><<v<^^>^>^><<<v><>v<vvv^v^^v^>v^^v<^^v<vv^>^^^<<v^><v>>v><><^v^^v^vv>^vvvv^vv>^>^<<>>vv^<v<><<^^^<>>>v>^<vv>v^>^><vvv><>>v^>^^><>^>>vvvvv<v>^v>v><v>^<>^>>v^^^v<><><v<><^><vv>vv<vv>>^>>>>^<v>v^<<v<v<v>><^<^>>v>vv<>v><<^<<^v<<v<<^<v<^<^<<<<<v<>><^^<<v^<^>><<<^><>^>^^^vv^<^>v>^v>vv^v<>^>v<v^^v><<>^>v><v>^^v^v><^>^><^<<^^<<v>^>><<>v^^^^>v<<<>v<^<><><^>v^^<vvv^<vv<v>><v><^vv>vv^><vv^vv<>^^v<v^<>>>>v^^<>><v>>vv^<^v>v^>>^<>><^^^>v^^>vv><v<<>>^v^vvv^^<^^>^v^<^>v>>>>>v>v<>^<>v<<v^^<>^^^>^^^><<>^v<v<<>^^<>vv><^<<<><>^>v>>v<>v^v>>><v<v^v<^v^>v<v<v^<>v^v^><>^v>^<v<v>^^>>^v>^><^^<<v<<^>^<^<vvvvv<>>v><^v<<><<^^^<v^^^<^<>>^vv^><><>><><vv>>v><^v^^^<v<<vv^v>^<^>v^v><>v^>^^>v>v^^<<<<^v><>v^v>>>^>v<^v^<<><<^^>v^vvv^v<<>^<><^<^^>^^<v^<^^<^v<<>^><v><vv><v<v<v><^<v>^v^v^v<><v<v>>>>>><^<><<>^^v^>>v>^>><^v^><>><<<v>vv<<<^<v<v<^>v>^v>v>v<^v<>v<v>^^<>><>><v>>>^<<^<<><^>>>^^^^^<>^v<vv<v^<<<^><vv^>v><v^<>^v<<v<>^<v<><v^^><vv^<vv>^>vvvv<<^<v<^<v^^^^v<v^<<>^<v>v>>>^>^^>>><vv>v>^<>^<><v><<<^^>^>v<<^<>^^<v<^v^^<v><<^<^^v><^^^^<>>><><<^<^>^^><^vv>v<>v<>^<<vvv^v<v<>^<vv>^<<<<<^^<v<<^v><<>vv^>vv>><<^>><<^vvv><<^<^<>^<v>>vv^>>^v>vv><^^v^^v>^<<<^v^>^>><>^><><v<>v<v><<^vv>^>^><<^v^<vv<vv>><v<<^<^<vv<v><<v^^v^vv<vv>^<>>^>v<<<<<>^v><^<v^vvvv>>vv<>>v<<>v^^^>>^<^>^>v<><<<v<v<<<>v^^^<v>^^v^<^<^<<vv>>>>v<^>v>v<^>><v>^^v>^<<>><<v>><v>v>v^<^^<<^><v^v<^<^<v<^^v>v<>><v<v<>vv<v<>^^^><<v<vv><<<^>^<vv>v^>><^^<^>>><<<^v<vvv<^>>>v><v>>v>v^>^^<>^>><^>>^>^^^<>><^>^<<>>>v^^><vv<<^^^>><^^<>v^>v<v<<^>v>>>^^>>^>>>^v<>v>^v^v><>^>vv^v<><>v^v>vv^^vv<v>v><v^<`; | |
//calc map size | |
const buildMap = ()=>{ | |
let x=0,y=0,max_x=0,max_y=0; | |
const is_max_x = (x)=> Math.abs(x) > max_x ? max_x = Math.abs(x) : false; | |
const is_max_y = (x)=> Math.abs(y) > max_y ? max_y = Math.abs(y) : false; | |
for (let key of str2) { | |
switch (key) { | |
case '<': | |
x-=1; | |
is_max_x(x) | |
break; | |
case '>': | |
x+=1; | |
is_max_x(x) | |
break; | |
case '^': | |
y+=1; | |
is_max_y(y) | |
break; | |
case 'v': | |
y-=1; | |
is_max_y(y) | |
break; | |
default: | |
console.log('ERR!') | |
break; | |
} | |
} | |
//return map size | |
return max_x > max_y ? (max_x *2)+1 : (max_y *2)+1 ; | |
} | |
const markAsVisit = (x,y) =>{ | |
if (map[x][y] == 1) { | |
number_of_visits++; | |
map[x][y] = 2; | |
} | |
else if(map[x][y] == 2){ | |
//ignore | |
} | |
else { | |
map[x][y] = 1; | |
} | |
}; | |
const map = [] | |
const map_size = buildMap(); | |
let x = Math.round(map_size/2)+1,y = Math.round(map_size/2)+1; // set location to center | |
//fill the map | |
for (let i = 0; i < map_size; i++) { | |
map.push([]) | |
for (let j = 0; j < map_size; j++) { | |
map[i].push(0) | |
} | |
} | |
map[x][y] = 1;// visit first | |
let number_of_visits = 0; | |
for (let key of str2) { | |
switch (key) { | |
case '<': | |
x-=1; | |
markAsVisit(x,y) | |
break; | |
case '>': | |
x+=1; | |
markAsVisit(x,y) | |
break; | |
case '^': | |
y+=1; | |
markAsVisit(x,y) | |
break; | |
case 'v': | |
y-=1; | |
markAsVisit(x,y) | |
break; | |
default: | |
console.log('ERR!') | |
break; | |
} | |
} | |
console.log('number_of_visits',number_of_visits) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment