2015-12-29 119 views
0

這是在「代碼出現」壞的編碼解決一個問題:link to problem的JavaScript矩陣操作不工作

我不知道,因爲我的代碼不能正常工作的原因,我有相關的錯誤到正則表達式,因爲我沒有重置正則表達式對象的指針,現在我認爲錯誤是固定的,但是有些東西正在逃避,我知道我做了什麼壞事。

問題是我的代碼顯示的解決方案不正確,您可以在我提供的鏈接上提交解決方案,並獲得解決方案的反饋。

正確的解決方案:543903 給出解決方案:418954

// day 6 of advent of code 

var input = "removed, take from problem"; 

function processInput(input, matrix) { 
    var linesOfInput = input.split("\n"); 
    var matches; 
    var turnOnRE = /turn on (\d+),(\d+).*?(\d+),(\d+)/g; 
    var turnOffRE = /turn off (\d+),(\d+).*?(\d+),(\d+)/g; 
    var toggleRE = /toggle (\d+),(\d+).*?(\d+),(\d+)/g; 

    // regular expression objects lastIndex property must be 'reseted' in order to work well 
    for (var i = 0 ; i < linesOfInput.length; i++) { 
    turnOnRE.lastIndex = 0; 
    turnOffRE.lastIndex = 0; 
    toggleRE.lastIndex = 0; 
    matches = turnOnRE.exec(linesOfInput[i]); 
    if (matches != null) { 
     manipulateLights(matrix, matches[1], matches[2], matches[3], matches[4], true); 
     continue; 
    } 
    matches = turnOffRE.exec(linesOfInput[i]); 
    if (matches != null) { 
     manipulateLights(matrix, matches[1], matches[2], matches[3], matches[4], false); 
     continue; 
    } 
    matches = toggleRE.exec(linesOfInput[i]); 
    manipulateLights(matrix, matches[1], matches[2], matches[3], matches[4]); 
    } 

} 

function manipulateLights(matrix, startI, startJ, endI, endJ, newValue) { 
    if (newValue == undefined) { // toogle 
    for (var i = startI ; i <= endI; i++) { 
     for (var j = startJ ; j <= endJ; j++) { 
     matrix[i][j] = !matrix[i][j]; 
     } 
    } 
    console.log(startI, startJ, endI, endJ, newValue); 
    } else { 
    for (var i = startI ; i <= endI; i++) { 
     for (var j = startJ ; j <= endJ; j++) { 
     matrix[i][j] = newValue; 
     } 
    } 
    console.log(startI, startJ, endI, endJ, newValue); 
    } 
    console.log(countTurnedOnLights(matrix)); 
} 

function countTurnedOnLights(matrix) { 
    var turnedOn = 0; 

    for (var i = 0 ; i < matrixWidth; i++) { 
    for (var j = 0 ; j < matrixHeigth; j++) { 
     if (matrix[i][j] == true) { 
     turnedOn++; 
     } 
    } 
    } 

    return turnedOn; 
} 

var matrixHeigth = 1000; 
var matrixWidth = 1000; 

// define a bidimensional array, is almost like in C++ 
var lightMatrix = new Array(matrixWidth); 
for (var i = 0 ; i < matrixWidth; i++) { 
    lightMatrix[i] = new Array(matrixHeigth); 
} 

// turn off all lights 
for (var i = 0 ; i < matrixWidth; i++) { 
    for (var j = 0 ; j < matrixHeigth; j++) { 
    lightMatrix[i][j] = false; 
    } 
} 

processInput(input, lightMatrix); 
console.log(countTurnedOnLights(lightMatrix)); 
+0

什麼是具體錯誤?什麼不按預期工作? – DontVoteMeDown

+0

'代碼無法正常工作'詳細說明。它應該做什麼?它在做什麼?你會得到什麼錯誤?你在調試器中發現了什麼? – csmckelvey

+0

我沒有使用調試器,問題是我得到的解決方案不正確,您可以在頁面上提交解決方案。我編輯抱歉。 – freinn

回答

2

好吧,我想通了錯誤 - 你的正則表達式匹配被視爲字符串,當你第一次創建你的for循環。

for (var i = startI ; i <= endI; i++) { 
    for (var j = startJ ; j <= endJ; j++) { 

當你通過923,339像756,53這樣的組合時,它認爲53> 339,它立即退出循環。在for循環中用Number()包裝每個「start」變量,或者在傳遞參數時這樣做。