Last active
October 31, 2024 01:31
-
-
Save saeed9321/7aa2ab20715c126a234ffb042096ff62 to your computer and use it in GitHub Desktop.
Robotic leveling Coding Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coding game challenge | |
// https://ide.codingame.com/21913565?id=6961426c38c52b3832ea4526f1e3554103a33c0 | |
function solve(clawPos, boxes, boxInClaw) { | |
// Write your code here | |
// To debug: console.error('Debug messages...'); | |
// #1 Get all available boxes | |
let sumOfBoxes = 0; | |
let numOfStacks = boxes.length; | |
boxes.map(item => sumOfBoxes += item); | |
if(boxInClaw) sumOfBoxes += 1; | |
// #2 Create a target - How the boxes should eventually be/look like | |
let target = new Array(numOfStacks).fill(0); | |
while(sumOfBoxes != 0){ | |
for(var i=0; i<numOfStacks; i++){ | |
if(sumOfBoxes != 0){ | |
target[i] += 1; | |
sumOfBoxes -= 1; | |
} | |
} | |
} | |
// #3 At any position check if a boxed to be placed | |
if(boxInClaw && boxes[clawPos] < target[clawPos]){ | |
return 'PLACE'; | |
} | |
// #4 At any position check if a boxed to be picked | |
if(!boxInClaw && boxes[clawPos] > target[clawPos]){ | |
return 'PICK'; | |
} | |
// #5 Check if should go right | |
for(var i=clawPos; i<numOfStacks; i++){ | |
if(boxInClaw){ | |
if(boxes[i] < target[i]){ | |
return 'RIGHT' | |
} | |
}else{ | |
if(boxes[i] > target[i]){ | |
return 'RIGHT' | |
} | |
} | |
} | |
// #6 Check if should go left | |
for(var i=clawPos; i<numOfStacks; i--){ | |
if(boxInClaw){ | |
if(boxes[i] < target[i]){ | |
return 'LEFT' | |
} | |
}else{ | |
if(boxes[i] > target[i]){ | |
return 'LEFT' | |
} | |
} | |
} | |
return ''; | |
} | |
/* Ignore and do not change the code below */ | |
// game loop | |
while (true) { | |
const clawPos = parseInt(readline()); | |
const boxInClaw = readline() !== '0'; | |
const stacks = parseInt(readline()); | |
const boxes = readline().split(' ').map(j => parseInt(j)).slice(0, stacks); | |
const oldWrite = process.stdout.write; | |
process.stdout.write = chunk => { console.error(chunk); return true } | |
const action = solve(clawPos, boxes, boxInClaw); | |
process.stdout.write = oldWrite; | |
console.log(action); | |
} | |
/* Ignore and do not change the code above */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment