Tom is decorating the pavement in his garden with N square tiles. Each tile is divided into four triangles of different colors (white - 'W', red - 'R', green - 'G' and blue - 'B'). A tile is described as a string of four characters denoting respectively, the color of the upper, right, bottom and left triangle. For example, the tile in the figure below is described as "WRGB".
Tom arranged the tiles in a row and decided to rotate some of them to obtain a pretty sequence. He considers a sequence of tiles pretty if each pair of adjacent tiles shares one side of the same color.
Write a function:
function solution(A);
that, given an array A of N strings, representing the sequence of tiles, returns the minimum number of 90-degree rotations (clockwise or counter-clockwise) that Tom has to perform.
Examples:
1. Given A = ["RGBW", "GBRW"], the function should return 1.
Tom can rotate the second tile counter-clockwise once to obtain a pretty sequence.
2. Given A = ["WBGR", "WBGR", "WRGB", "WRGB", "RBGW"], the function should return 4.
Tom can obtain a pretty sequence by rotating the first and third tiles counter-clockwise and the second and fourth tiles clockwise.
3. Given A = ["RBGW", "GBRW", "RWGB", "GBRW"], the function should return 2.
Tom can rotate the first tile clockwise twice to obtain a pretty sequence.
4. Given A = ["GBRW", "RBGW", "BWGR", "BRGW"], the function should return 2.
Tom can rotate the first two tiles clockwise to obtain a pretty sequence.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- string representing a tile has 4 letters (exactly one occurrence of 'R', 'G', 'B' and 'W').
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// Implement your solution here
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// Implement your solution here
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// Implement your solution here
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
return { rotationsNeeded: 1, nextColorToMatch: tile}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// Implement your solution here
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
return { rotationsNeeded: 1, nextColorToMatch: tile[]}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// Implement your solution here
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0]}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// Implement your solution here
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result =
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
for (let i = 1; i < A.length; i++) {
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
for (let i = 1; i < A.length; i++) {
l
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
for (let i = 1; i < A.length; i++) {
let step = calculateStep()
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch =
for (let i = 1; i < A.length; i++) {
let step = calculateStep()
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch)
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
}
function calculateNeededRotations
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
}
function calculateNeededRotations(initialColorToMatch) {
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
}
function calculateNeededRotations(initialColorToMatch) {
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = 0;
let colorToMatch = A[0][1];
}
function calculateNeededRotations(initialColorToMatch) {
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let colorToMatch = A[0][1];
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let colorToMatch = A[0][1];
let result =
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let colorToMatch = A[0][1];
let result = calculateNeededRotations(colorToMatch)
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 1; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let colorToMatch = A[0][1];
let result = calculateNeededRotations(colorToMatch)
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations(colorToMatch)
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
let otherColors = ['B', 'G', 'W']
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
}
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(otherColors[i])
if (current < result)
}
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(otherColors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(otherColors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
ReferenceError: A is not defined at calculateNeededRotations (solution.js:18:25) at solution (solution.js:6:18) at solutionWrapper (/tmp/exec_user_2wg3ya4d/exec.js:402:28) at /tmp/exec_user_2wg3ya4d/exec.js:428:24
ReferenceError: A is not defined at calculateNeededRotations (solution.js:18:25) at solution (solution.js:6:18) at solutionWrapper (/tmp/exec_user_2wg3ya4d/exec.js:402:28) at /tmp/exec_user_2wg3ya4d/exec.js:428:24
ReferenceError: A is not defined at calculateNeededRotations (solution.js:18:25) at solution (solution.js:6:18) at solutionWrapper (/tmp/exec_user_2wg3ya4d/exec.js:402:28) at /tmp/exec_user_2wg3ya4d/exec.js:428:24
ReferenceError: A is not defined at calculateNeededRotations (solution.js:18:25) at solution (solution.js:6:18) at solutionWrapper (/tmp/exec_user_2wg3ya4d/exec.js:402:28) at /tmp/exec_user_2wg3ya4d/exec.js:428:24
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(A, otherColors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations('R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(A, otherColors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations(A, 'R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(A, otherColors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations(A, 'R')
let otherColors = ['B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(A, otherColors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations(A, 'R')
let otherColors = ['R'B', 'G', 'W']
for (let i = 0; i < otherColors.length; i++) {
let current = calculateNeededRotations(A, otherColors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = calculateNeededRotations(A, 'R')
let colors = ['R', 'B', 'G', 'W']
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
colors.map(i => calculateNeededRotations(A, ))
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
colors.map(i => calculateNeededRotations(A, i)).min()
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
colors.map(i => calculateNeededRotations(A, i)).min()
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
TypeError: colors.map(...).min is not a function at solution (solution.js:8:53) at solutionWrapper (/tmp/exec_user_s_tab106/exec.js:402:28) at /tmp/exec_user_s_tab106/exec.js:428:24
TypeError: colors.map(...).min is not a function at solution (solution.js:8:53) at solutionWrapper (/tmp/exec_user_s_tab106/exec.js:402:28) at /tmp/exec_user_s_tab106/exec.js:428:24
TypeError: colors.map(...).min is not a function at solution (solution.js:8:53) at solutionWrapper (/tmp/exec_user_s_tab106/exec.js:402:28) at /tmp/exec_user_s_tab106/exec.js:428:24
TypeError: colors.map(...).min is not a function at solution (solution.js:8:53) at solutionWrapper (/tmp/exec_user_s_tab106/exec.js:402:28) at /tmp/exec_user_s_tab106/exec.js:428:24
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let result = A.length*4
let colors = ['R', 'B', 'G', 'W']
for (let i = 0; i < colors.length; i++) {
let current = calculateNeededRotations(A, colors[i])
if (current < result) result = current
}
return result
}
function calculateNeededRotations(A, colorToMatch) {
let result = 0;
for (let i = 0; i < A.length; i++) {
let step = calculateStep(colorToMatch, A[i])
result += step.rotationsNeeded
colorToMatch = step.nextColorToMatch
}
return result;
}
function calculateStep(colorToMatch, tile) {
if (tile[3] === colorToMatch) return { rotationsNeeded: 0, nextColorToMatch: tile[1] }
if (tile[1] === colorToMatch) return { rotationsNeeded: 2, nextColorToMatch: tile[3] }
if (tile[2] === colorToMatch) return { rotationsNeeded: 1, nextColorToMatch: tile[0] }
else return { rotationsNeeded: 1, nextColorToMatch: tile[2] }
}
The solution obtained perfect score.