// Enter your solution below: var count_chains = function(bejeweledChains) { // test() return count(bejeweledChains) } var count = function(bejeweledChains) { return checkTotalChainsInRow(bejeweledChains) + checkTotalChainsInCol(bejeweledChains) } var checkTotalChainsInRow = function(bejeweledChains) { var totalChains = 0 while (length(bejeweledChains) > 0) { totalChains = totalChains + countChains(get(bejeweledChains, 0)) bejeweledChains = remove(bejeweledChains, 0) } return totalChains } var checkTotalChainsInCol = function(bejeweledChains) { var colsToCheck = transformColumnsToRow(bejeweledChains) return checkTotalChainsInRow(colsToCheck) } var transformColumnsToRow = function(multiArray) { if (length(multiArray) <= 0) { return array() } var firstRow = get(multiArray, 0) var colToIterate = length(firstRow) var noOfCol = length(firstRow) var noOfRow = length(multiArray) var result = array() while(colToIterate > 0) { var currentLoopCountOfRow = 1 var transformedArrayFromColumn = array() while(currentLoopCountOfRow <= noOfRow) { var valueOfSameColInCurrentRow = get(get(multiArray, currentLoopCountOfRow - 1), noOfCol - colToIterate) transformedArrayFromColumn = insert(transformedArrayFromColumn, length(transformedArrayFromColumn), valueOfSameColInCurrentRow) currentLoopCountOfRow = currentLoopCountOfRow + 1 } colToIterate = colToIterate - 1 result = insert(result, length(result), transformedArrayFromColumn) } return result } var countChains = function(toCheckArray) { if (length(toCheckArray) < 3) { return 0 } var totalCountOfChain = 0 var currentPattern = get(toCheckArray, 0) var numberOfConsecutive = 1 toCheckArray = remove(toCheckArray, 0) while (length(toCheckArray) > 0) { var toCheckPattern = get(toCheckArray, 0) if (toCheckPattern == currentPattern) { numberOfConsecutive = numberOfConsecutive + 1 if (numberOfConsecutive == 3) { totalCountOfChain = totalCountOfChain + 1 } } else { numberOfConsecutive = 1 currentPattern = toCheckPattern } toCheckArray = remove(toCheckArray, 0) } return totalCountOfChain } var test = function() { // print (countChains(array( 0,3,3,3 )) == 1) // print (countChains(array( 0,3,3,3,1,1,1 )) == 2) // print (countChains(array( 0,3,3,3,3,4,2,3,1,1,1 )) == 2) // print (countChains(array( 3,3,3 )) == 1) // print (countChains(array( 3,3 )) == 0) // print (countChains(array()) == 0) // print transformColumnsToRow(array(array( 0 , 3 , 3 ),array( 0 , 0 , 0 ),array( 0 , 1 , 4 ), array( 5 , 51 , 5 ))) // [[0,0,0,5],[3,0,1,51],[3,0,4,5]] // print transformColumnsToRow(array(array( 0 , 3 , 3 ),array( 0 , 0 , 0 ),array( 0 , 1 , 4 ))) // [[0,0,0],[3,0,1],[3,0,4]] // print transformColumnsToRow(array()) // [] print (count(array(array( 0 , 3 , 3 ),array( 0 , 0 , 0 ),array( 0 , 1 , 4 ))) == 2) print (count(array(array( 3 , 3 , 3 , 3 , 3 ),array( 0 , 0 , 1 , 2 , 1 ),array( 0 , 1 , 4 , 3 , 2 ))) == 1) print (count(array(array( 3 , 3 , 3 , 4 , 1 , 2 ),array( 0 , 0 , 3 , 2 , 4 , 4 ),array( 4 , 0 , 0 , 2 , 2 , 1 ),array( 1 , 3 , 4 , 2 , 4 , 0 ),array( 0 , 1 , 0 , 2 , 0 , 1 ),array( 2 , 2 , 2 , 4 , 1 , 3 ))) == 3) }