problem: Video Game - Problems | CodeChef
my commented solution (with driver code)
#include <bits/stdc++.h>
using namespace std;
int main(){
//find width and maxheight
long long width, maxh;
cin >> width >> maxh;
// initialise by copying current situation
long long situation[width];
for (long long i=0;i<width;i++)
cin >> situation [i];
long currposn=0;
bool hold=false;
//keep operating on each command until 0 recieved
int command=-1;
while (command!=0){
cin >> command;
// driver code
for (long long i=0;i<width;i++){
cout << situation[i] <<" ";}
cout << "\n" << currposn << hold << "\n";
//define commands under appropriate restrictions
if (command ==0){if (currposn>0) {currposn--;}}
else if (command == 2){if (currposn<width) {++currposn;}}
else if (command == 3){if ((!hold) && situation[currposn]>0){hold = true;situation[currposn]--;}}
else if (command == 4){if (hold && situation[currposn]<maxh){hold = false;situation[currposn]++;}}}
//display final situation
for (long long i=0;i<width;i++){
cout << situation[i] <<" ";
}
return 0;
}