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) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
}
return rotations;
}
TypeError: Cannot read property '3' of undefined at solution (solution.js:15:32) at solutionWrapper (/tmp/exec_user_zxakj9rq/exec.js:402:28) at /tmp/exec_user_zxakj9rq/exec.js:428:24
TypeError: Cannot read property '0' of undefined at solution (solution.js:15:32) at solutionWrapper (/tmp/exec_user_zxakj9rq/exec.js:402:28) at /tmp/exec_user_zxakj9rq/exec.js:428:24
TypeError: Cannot read property '1' of undefined at solution (solution.js:15:32) at solutionWrapper (/tmp/exec_user_zxakj9rq/exec.js:402:28) at /tmp/exec_user_zxakj9rq/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 rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (tile1[tile1.length - 1] !== tile2[0]) {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (tile1[tile1.length - 1] !== tile2[0]) {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (mapping[tile1]) {
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
} else {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (mapping[tile1]) {
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
} else {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (mapping[tile1]) {
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
} else {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (mapping[tile1]) {
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
} else {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (mapping[tile1]) {
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
} else {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
let mapping = {
RGBW: [1, 0, 1, 1],
GBRW: [1, 1, 0, 1],
WGRB: [1, 1, 1, 0],
RWGB: [0, 1, 1, 1]
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
if (mapping[tile1]) {
rotations += mapping[tile1]['GBRW'.indexOf(tile2[0])];
} else {
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check if the last character of tile1 is different than the first character of tile2
if (tile1[tile1.length - 1] !== tile2[0]) {
// increment the number of rotations
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check if the last character of tile1 is different than the first character of tile2
if (tile1[tile1.length - 1] !== tile2[0]) {
// increment the number of rotations
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
if (tile1[tile1.length - 1] !== tile2[0]) rotations++;
else if (tile1[tile1.length - 1] !== tile2[1]) rotations++;
else if (tile1[tile1.length - 1] !== tile2[2]) rotations++;
else if (tile1[tile1.length - 1] !== tile2[3]) rotations++;
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
if (tile1[tile1.length - 1] !== tile2[0]) rotations++;
else if (tile1[tile1.length - 1] !== tile2[1]) rotations++;
else if (tile1[tile1.length - 1] !== tile2[2]) rotations++;
else if (tile1[tile1.length - 1] !== tile2[3]) rotations++;
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
let count = 0;
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
count++;
}
rotations += count;
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
let count = 0;
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
count++;
}
rotations += count;
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
for ()
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const candidates = {
"WBGR": []
}
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const candidates = {
"WBGR": 0,
"WBGR": 0,
}
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const candidates = {
"W": 0,
"R": 0,
"G": 0,
"B": 0,
};
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const candidates = {
"W": 0,
"R": 0,
"G": 0,
"B": 0,
};
let result = Number.POSITIVE_INFINITY;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ["W", "R", "G", "B"];
let result = Number.POSITIVE_INFINITY;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ["W", "R", "G", "B"];
let result = Number.POSITIVE_INFINITY;
f
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ["W", "R", "G", "B"];
let result = Number.POSITIVE_INFINITY;
for (const first of colors) {
}
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ["W", "R", "G", "B"];
let result = Number.POSITIVE_INFINITY;
const iLeft =
for (const first of colors) {
}
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ["W", "R", "G", "B"];
let result = Number.POSITIVE_INFINITY;
const iLeft = 3;
const iRight =
for (const first of colors) {
}
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ["W", "R", "G", "B"];
let result = Number.POSITIVE_INFINITY;
const iLeft = 3;
const iRight = 1;
for (const first of colors) {
}
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
let rotations = 0;
for (let i = 0; i < A.length - 1; i++) {
let tile1 = A[i];
let tile2 = A[i + 1];
// check the last character of tile1 with all four characters of tile2
// and increment the number of rotations as required
while (tile1[tile1.length - 1] !== tile2[0]) {
tile2 = tile2[3] + tile2.slice(0, 3);
rotations++;
}
}
return rotations;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
const iLeft = 3;
const iRight = 1;
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const lastC = OPPOSITE[iC];
const rotationsNeeded = PRECALCR[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
const iLeft = 3;
const iRight = 1;
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const lastC = OPPOSITE[iC];
const rotationsNeeded = PRECALCR[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const lastC = OPPOSITE[iC];
const rotationsNeeded = PRECALCR[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:31:31) at solutionWrapper (/tmp/exec_user_4a7e4doh/exec.js:402:28) at /tmp/exec_user_4a7e4doh/exec.js:428:24
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:31:31) at solutionWrapper (/tmp/exec_user_4a7e4doh/exec.js:402:28) at /tmp/exec_user_4a7e4doh/exec.js:428:24
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:31:31) at solutionWrapper (/tmp/exec_user_4a7e4doh/exec.js:402:28) at /tmp/exec_user_4a7e4doh/exec.js:428:24
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:31:31) at solutionWrapper (/tmp/exec_user_4a7e4doh/exec.js:402:28) at /tmp/exec_user_4a7e4doh/exec.js:428:24
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const lastC = OPPOSITE[iC];
const rotationsNeeded = PRECALCR[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const lastC = OPPOSITE[iC];
const rotationsNeeded = PRECALCR[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:30:31) at solutionWrapper (/tmp/exec_user_hndstf2l/exec.js:402:28) at /tmp/exec_user_hndstf2l/exec.js:428:24
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:30:31) at solutionWrapper (/tmp/exec_user_hndstf2l/exec.js:402:28) at /tmp/exec_user_hndstf2l/exec.js:428:24
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:30:31) at solutionWrapper (/tmp/exec_user_hndstf2l/exec.js:402:28) at /tmp/exec_user_hndstf2l/exec.js:428:24
ReferenceError: Cannot access 'lastC' before initialization at solution (solution.js:30:31) at solutionWrapper (/tmp/exec_user_hndstf2l/exec.js:402:28) at /tmp/exec_user_hndstf2l/exec.js:428:24
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
const iLeft = 3;
const iRight = 1;
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = OPPOSITE[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
const iLeft = 3;
const iRight = 1;
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = OPPOSITE[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
return result;
}
solution.js:53 }(); ^ SyntaxError: Unexpected token ';' at new Script (vm.js:102:7) at createScript (vm.js:262:10) at Object.runInNewContext (vm.js:303:10) at getSolution (/tmp/exec_user_f39ulakw/exec.js:392:29) at /tmp/exec_user_f39ulakw/exec.js:426:34
solution.js:53 }(); ^ SyntaxError: Unexpected token ';' at new Script (vm.js:102:7) at createScript (vm.js:262:10) at Object.runInNewContext (vm.js:303:10) at getSolution (/tmp/exec_user_f39ulakw/exec.js:392:29) at /tmp/exec_user_f39ulakw/exec.js:426:34
solution.js:53 }(); ^ SyntaxError: Unexpected token ';' at new Script (vm.js:102:7) at createScript (vm.js:262:10) at Object.runInNewContext (vm.js:303:10) at getSolution (/tmp/exec_user_f39ulakw/exec.js:392:29) at /tmp/exec_user_f39ulakw/exec.js:426:34
solution.js:53 }(); ^ SyntaxError: Unexpected token ';' at new Script (vm.js:102:7) at createScript (vm.js:262:10) at Object.runInNewContext (vm.js:303:10) at getSolution (/tmp/exec_user_f39ulakw/exec.js:392:29) at /tmp/exec_user_f39ulakw/exec.js:426:34
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
const iLeft = 3;
const iRight = 1;
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = OPPOSITE[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
Invalid result type, integer expected, non-integer number found
Invalid result type, integer expected, non-integer number found
Invalid result type, integer expected, non-integer number found
Invalid result type, integer expected, non-integer number found
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/**
* Rotations needed to get the color at this index to be the left one (3).
*/
const PRECALCR = {
0: 1,
1: 2,
2: 1,
3: 0,
}
const OPPOSITE = {
0: 2,
1: 3,
2: 0,
3: 1,
};
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = OPPOSITE[iC];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/** Rotations needed to get the color at this index to be the left one (3). */
const PRECALCR = { 0: 1, 1: 2, 2: 1, 3: 0 };
const OPPOSITE = { 0: 2, 1: 3, 2: 0, 3: 1 };
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = tile[OPPOSITE[iC]];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/** Rotations needed to get the color at this index to be the left one (3). */
const PRECALCR = { 0: 1, 1: 2, 2: 1, 3: 0 };
const OPPOSITE = { 0: 2, 1: 3, 2: 0, 3: 1 };
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = tile[OPPOSITE[iC]];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/** Rotations needed to get the color at this index to be the left one (3). */
const PRECALCR = { 0: 1, 1: 2, 2: 1, 3: 0 };
const OPPOSITE = { 0: 2, 1: 3, 2: 0, 3: 1 };
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = tile[OPPOSITE[iC]];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/** Rotations needed to get the color at this index to be the left one (3). */
const PRECALCR = { 0: 1, 1: 2, 2: 1, 3: 0 };
const OPPOSITE = { 0: 2, 1: 3, 2: 0, 3: 1 };
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = tile[OPPOSITE[iC]];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/** Rotations needed to get the color at this index to be the left one (3). */
const PRECALCR = { 0: 1, 1: 2, 2: 1, 3: 0 };
const OPPOSITE = { 0: 2, 1: 3, 2: 0, 3: 1 };
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = tile[OPPOSITE[iC]];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const colors = ['W', 'R', 'G', 'B'];
let result = Number.POSITIVE_INFINITY;
/** Rotations needed to get the color at this index to be the left one (3). */
const PRECALCR = { 0: 1, 1: 2, 2: 1, 3: 0 };
const OPPOSITE = { 0: 2, 1: 3, 2: 0, 3: 1 };
for (const firstC of colors) {
let candidate = 0;
let lastC = firstC;
for (let i = 0; i < A.length; i++) {
const tile = A[i];
const iC = tile.indexOf(lastC);
const rotationsNeeded = PRECALCR[iC];
lastC = tile[OPPOSITE[iC]];
candidate += rotationsNeeded;
}
if (candidate < result) {
result = candidate;
}
}
return result;
}
The solution obtained perfect score.