괜히 했다는 생각이 든다. 아래의 풀이는 속도를 충족하지 못했다. 완전히 새로 풀이하는게 낫겠다.
클래스를 써 보려고 시도했다. 생각보다 코드가 길어져서 당황했다. 이중 배열을 생성해서 행렬을 구현했다. 그 다음 각 규칙을 클래스에 어울리도록 구현했다. 이 규칙들을 적용시키면 객체 값을 고친다. 그러나 빛의 다음 행선지를 어떻게 전달 해야 할지 모르겠다.
해결했다. 스펠링 틀려서 중간에 디버깅 하느라 한시간은 날렸다. class 로 풀이 하려니 코드가 정말 길어졌다. 속도 문제를 어떻게 해결해야 할지 모르겠다.
function solution(grid) {
const result = {};
const arr =[];
const madeGrid = makeGrid(grid);
const matrix = makeGridToMatrix(madeGrid);
const dir = ["north", "east", "south", "west"];
const a = grid.length;
const b = grid[0].length;
const test = a*b*4
for(let x=0; x<a; x++){
for(let y=0; y<b; y++){
for(let i=0; i<4; i++){
let temp = calcPath(dir[i], x,y, matrix, result);
if(temp === test) return [temp];
}
}
}
for(let i in result){
arr.push(result[i])
}
return (arr.sort((a,b)=>a-b));
}
function calcPath(startDirection, startPositionX, startPositionY, matrix, result){
let i =startPositionX, j=startPositionY, direction = startDirection, string="";
let position = matrix[i][j];
while(true){
if(position["rule"] === "S"){
direction = ruleS(direction, position)
}
else if(position["rule"] === "L"){
direction = ruleL(direction, position)
}
else{
direction = ruleR(direction,position)
}
if(position.north > 1 || position.east>1 || position.south>1 || position.west >1){
position.north = Math.min(position.north, 1);
position.east = Math.min(position.east, 1);
position.south = Math.min(position.south, 1);
position.west = Math.min(position.west, 1);
break;
}
if(direction === "north") i++;
else if(direction === "east") j--;
else if(direction === "south") i--;
else j++;
if(i<0) i = matrix.length-1;
if(i>=matrix.length) i = 0;
if(j<0) j = matrix[0].length -1;
if(j>=matrix[0].length) j=0;
position = matrix[i][j];
}
for(let i of matrix){
for(let j=0; j<i.length; j++){
string += i[j].matrixToString;
i[j].restart = 0;
}
}
return result[string] = string.match(/1/g).length;
}
function makeGrid(grid){
const result = [];
for(let i of grid){
result.push(Array.from(i));
}
return result;
}
function makeGridToMatrix(madeGrid){
let result = madeGrid, i=0,j=0;
while(i<result.length){
if(j >= result[i].length) j=0;
if(result[i][j] === "S"){
result[i][j] = new Node("S");
}
else if(result[i][j] === "R"){
result[i][j] = new Node("R");
}
else result[i][j] = new Node("L");
j++;
if(result[i].length === j){
i++;
}
}
return result;
}
function ruleS(direction, position){
if(direction === "north"){
position.lightOutSouth = 1;
return "north"
}
else if(direction === "east"){
position.lightOutWest = 1;
return "east"
}
else if(direction === "south"){
position.lightOutNorth = 1;
return "south"
}
else{
position.lightOutEast = 1;
return "west"
}
}
function ruleR(direction, position){
if(direction === "north"){
position.lightOutWest = 1;
return "east"
}
else if(direction === "east"){
position.lightOutNorth = 1;
return "south"
}
else if(direction === "south"){
position.lightOutEast = 1;
return "west"
}
else{
position.lightOutSouth = 1;
return "north"
}
}
function ruleL(direction, position){
if(direction === "north"){
position.lightOutEast = 1;
return "west"
}
else if(direction === "east"){
position.lightOutSouth = 1;
return "north"
}
else if(direction === "south"){
position.lightOutWest = 1;
return "east"
}
else{
position.lightOutNorth = 1;
return "south"
}
}
class Node {
constructor(SLR){
this.north = 0,
this.east = 0,
this.south = 0,
this.west = 0,
this.rule = SLR
}
set lightOutNorth(value){
this.north += value;
}
set lightOutSouth(value){
this.south += value;
}
set lightOutEast(value){
this.east += value;
}
set lightOutWest(value){
this.west += value;
}
get matrixToString(){
return String(this.north)+String(this.east)+String(this.south)+String(this.west)
}
set restart(value){
this.north = value,
this.east = value,
this.south = value,
this.west = value
}
}