Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static int Bitcount(int n)
- {
- int count = 0;
- while (n != 0)
- {
- count++;
- n &= (n - 1);
- }
- return count;
- }
- static void Main()
- {
- //get the first input line
- int numberCommands = int.Parse(Console.ReadLine());
- int piecesCount = numberCommands / 4;
- //define field variables as bytes so that they hold only bits 0-7
- byte firstRow = 0;
- byte secondRow = 0;
- byte thirdRow = 0;
- byte forthRow = 0;
- //define variable to hold the score
- int shapeScore = 0;
- int totalScore = 0;
- //define some flags
- bool gameEnd = false;
- bool scored = false;
- //get input data for each piece and move it
- for (int i = 0; i < piecesCount; i++)
- {
- int newPiece = int.Parse(Console.ReadLine());
- string dirOne = Console.ReadLine();
- string dirTwo = Console.ReadLine();
- string dirThree = Console.ReadLine();
- shapeScore = Bitcount(newPiece); //count 1 bits in integer
- byte pieceShape = (byte)newPiece; //take only bits 0-7
- scored = false;
- //place shape on first row
- if ((firstRow & pieceShape) == 0) firstRow = (byte)(firstRow | pieceShape);
- else
- {
- gameEnd = true;
- break;
- }
- //piece on first row, move to second row
- if (dirOne == "L") //move shape left
- {
- if ((pieceShape << 1) <= 255)
- {
- pieceShape = (byte)(pieceShape << 1);
- firstRow = (byte)(firstRow ^ (pieceShape / 2)); //divide by 2 to compensate for left shift
- firstRow = (byte)(firstRow | pieceShape);
- }
- }
- if (dirOne == "R") //move shape right
- {
- if ((pieceShape & 1) == 0)
- {
- pieceShape = (byte)(pieceShape >> 1);
- firstRow = (byte)(firstRow ^ (pieceShape * 2)); //multiply by 2 to compensate for the right shift
- firstRow = (byte)(firstRow | pieceShape);
- }
- }
- if ((pieceShape & secondRow) == 0) //move shape down
- {
- secondRow = (byte)(pieceShape | secondRow); //place shape on second row
- firstRow = (byte)(firstRow ^ pieceShape); //remove shape from first row
- }
- else if (firstRow == 255)
- {
- scored = true;
- totalScore = totalScore + shapeScore * 10;
- firstRow = 0;
- continue;
- }
- else
- {
- gameEnd = true;
- totalScore = totalScore + shapeScore;
- break;
- }
- //check if piece is on second row, move to third
- if (! scored)
- {
- if (dirTwo == "L")
- {
- if ((pieceShape << 1) <= 255)
- {
- pieceShape = (byte)(pieceShape << 1);
- secondRow = (byte)(secondRow ^ (pieceShape / 2)); //divide by 2 to compensate for left shift
- secondRow = (byte)(secondRow | pieceShape);
- }
- }
- if (dirTwo == "R")
- {
- if ((pieceShape & 1) == 0)
- {
- pieceShape = (byte)(pieceShape >> 1);
- secondRow = (byte)(secondRow ^ (pieceShape * 2)); //multiply by 2 to compensate for the right shift
- secondRow = (byte)(secondRow | pieceShape);
- }
- }
- if ((pieceShape & thirdRow) == 0)
- {
- thirdRow = (byte)(pieceShape | thirdRow);
- secondRow = (byte)(secondRow ^ pieceShape);
- }
- else if (secondRow == 255)
- {
- scored = true;
- totalScore = totalScore + shapeScore * 10;
- secondRow = (byte)(secondRow & firstRow);
- firstRow = 0;
- continue;
- }
- else
- {
- scored = true;
- totalScore = totalScore + shapeScore;
- }
- }
- //check if piece is on third row, move to forth
- if (! scored)
- {
- if (dirThree == "L")
- {
- if ((pieceShape << 1) <= 255)
- {
- pieceShape = (byte)(pieceShape << 1);
- thirdRow = (byte)(thirdRow ^ (pieceShape / 2));
- thirdRow = (byte)(thirdRow | pieceShape);
- }
- }
- if (dirThree == "R")
- {
- if ((pieceShape & 1) == 0)
- {
- pieceShape = (byte)(pieceShape >> 1);
- thirdRow = (byte)(thirdRow ^ (pieceShape * 2));
- thirdRow = (byte)(thirdRow | pieceShape);
- }
- }
- if ((pieceShape & forthRow) == 0)
- {
- forthRow = (byte)(pieceShape | forthRow);
- thirdRow = (byte)(thirdRow ^ pieceShape);
- }
- else if (thirdRow == 255)
- {
- totalScore = totalScore + shapeScore * 10;
- thirdRow = (byte)(thirdRow & secondRow);
- secondRow = 0;
- secondRow = (byte)(secondRow | firstRow);
- firstRow = 0;
- continue;
- }
- if (forthRow == 255)
- {
- totalScore = totalScore + shapeScore * 10;
- forthRow = (byte)(forthRow & thirdRow);
- thirdRow = 0;
- thirdRow = (byte)(thirdRow | secondRow);
- secondRow = 0;
- secondRow = (byte)(secondRow | firstRow);
- firstRow = 0;
- }
- else
- {
- totalScore = totalScore + shapeScore;
- }
- }
- }
- Console.WriteLine(totalScore);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement