RGYB(color) Slots Game to guess the correct color for the correct slot
Given that you have four slots, and each slot will contain a color red (R), yellow (Y), green (G), blue (B) respectively. For example, if you select YGGR (Slot-1 is yellow, Slots-2 and -3 are green, Slot -4 is red). The colors of slots are not known to you beforehand. You will make a guess about the colors. You might, for example, guess YRGB.
When you guess the correct color for the correct slot, you get a “hit” If you guess a color that exists but is in the wrong slot, you get a “pseudo-hit:’ Note that a slot that is a hit can never count as a pseudo-hit.
Given a guess and a solution, your task is to write a program to calculate the number of hits and pseudo-hits.
Examples:
Input: solution -> RGBY, Guess -> GGRR
Output: hit -> 1, pseudohit -> 1
Input: solution -> RGYB, Guess -> YGRR
Output: hit -> 1, pseudohit -> 2
Input: solution -> RGYB, Guess -> GGYR
Output: hit -> 2, pseudohit -> 1
A simple solution will be to traverse both strings simultaneously and check the characters of both strings. If both strings have same character then it is a hit and thus increment the count of hits. If the characters of strings do not match at a position, then traverse the solution string again to see if the character in guess is occurred anywhere in solution string or not, if yes increment count of pseudo hits.
Time Complexity: O(N*N) , where N is the length of string.
An efficient solution is to create a frequency array which stores how many times each character occurs in solution, excluding times when the slot is a “hit”. Then, we iterate through guess to count the number of pseudo-hits.
Below is the implementation of above approach:
C++
#include<iostream>
#include<string>
using namespace std;
int hits = 0;
int pseudoHits = 0;
int MAX_COLORS = 4;
int codeOfColor( char c)
{
switch (c)
{
case 'B' : return 0;
case 'G' : return 1;
case 'R' : return 2;
case 'Y' : return 3;
default :
return -1;
}
}
void calcResult(string guess, string solution)
{
hits = 0;
pseudoHits = 0;
if (guess.length() != solution.length())
cout<< "Incorrect Input" <<endl;
int frequencies[MAX_COLORS] ={ 0 };
for ( int i = 0; i < guess.length() ;i++)
{
if (guess[i] == solution[i])
hits++;
else
{
int codecolor = codeOfColor(solution[i]);
frequencies[codecolor]++;
}
}
for ( int i = 0; i < guess.length(); i++)
{
int codecolor = codeOfColor(guess[i]);
if (codecolor >= 0 && frequencies[codecolor] > 0
&& guess[i] != solution[i])
{
pseudoHits++;
frequencies[codecolor]--;
}
}
cout << "hits -> " << hits << " Pseudo hits -> "
<< pseudoHits << endl;
}
int main()
{
string solution = "GGRR" ;
string guess = "RGBY" ;
calcResult(solution, guess);
solution = "YGRR" ;
guess = "RGYB" ;
calcResult(solution, guess);
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CalcRes
{
static int hits = 0 ;
static int pseudoHits = 0 ;
static int codeOfColor( char c)
{
switch (c)
{
case 'B' : return 0 ;
case 'G' : return 1 ;
case 'R' : return 2 ;
case 'Y' : return 3 ;
default :
return - 1 ;
}
}
static int MAX_COLORS = 4 ;
static void calcResult(String guess, String solution)
{
hits = 0 ;
pseudoHits = 0 ;
if (guess.length() != solution.length())
System.out.println( "Incorrect Input" );
int [] frequencies = new int [MAX_COLORS];
for ( int i = 0 ; i < guess.length(); i++)
{
if (guess.charAt(i) == solution.charAt(i))
{
hits++;
}
else
{
int codeofcolor = codeOfColor(solution.charAt(i));
frequencies[codeofcolor]++;
}
}
for ( int i = 0 ; i < guess.length(); i++)
{
int codeofcolor = codeOfColor(guess.charAt(i));
if (codeofcolor >= 0 && frequencies[codeofcolor] > 0 &&
guess.charAt(i) != solution.charAt(i))
{
pseudoHits++;
frequencies[codeofcolor]--;
}
}
System.out.println( "hits -> " + hits +
" Pseudo hits -> " + pseudoHits);
}
public static void main(String[] args)
{
String solution = "GGRR" ;
String guess = "RGBY" ;
calcResult(solution, guess);
solution = "YGRR" ;
guess = "RGYB" ;
calcResult(solution, guess);
}
}
|
Python3
hits = 0
pseudoHits = 0
MAX_COLORS = 4
def codeOfColor(c):
if c = = 'B' :
return 0 ;
elif c = = 'G' :
return 1 ;
elif c = = 'R' :
return 2 ;
elif c = = 'Y' :
return 3 ;
else :
return - 1 ;
def calcResult(guess, solution):
hits = 0
pseudoHits = 0
if ( len (guess) ! = len (solution)):
print ( "Incorrect Input" )
frequencies = [ 0 for i in range (MAX_COLORS)]
for i in range ( len (guess)):
if (guess[i] = = solution[i]):
hits + = 1
else :
codecolor = codeOfColor(solution[i])
frequencies[codecolor] + = 1
for i in range ( len (guess)):
codecolor = codeOfColor(guess[i])
if codecolor > = 0 and frequencies[codecolor] > 0 and guess[i] ! = solution[i]:
pseudoHits + = 1
frequencies[codecolor] - = 1
print ( "hits -> " , hits, " Pseudo hits -> " , pseudoHits)
solution = "GGRR"
guess = "RGBY"
calcResult(solution, guess)
solution = "YGRR"
guess = "RGYB"
calcResult(solution, guess)
|
C#
using System;
class GFG {
static int hits = 0;
static int pseudoHits = 0;
static int codeOfColor( char c)
{
switch (c)
{
case 'B' : return 0;
case 'G' : return 1;
case 'R' : return 2;
case 'Y' : return 3;
default : return -1;
}
}
static int MAX_COLORS = 4;
static void calcResult( string guess, string solution)
{
hits = 0;
pseudoHits = 0;
if (guess.Length != solution.Length)
Console.Write( "Incorrect Input" );
int []frequencies = new int [MAX_COLORS];
for ( int i = 0; i < guess.Length; i++)
{
if (guess[i] == solution[i])
{
hits++;
}
else
{
int codeofcolor = codeOfColor(solution[i]);
frequencies[codeofcolor]++;
}
}
for ( int i = 0; i < guess.Length; i++)
{
int codeofcolor = codeOfColor(guess[i]);
if (codeofcolor >= 0 &&
frequencies[codeofcolor] > 0 &&
guess[i] != solution[i])
{
pseudoHits++;
frequencies[codeofcolor]--;
}
}
Console.WriteLine( "hits -> " + hits +
" Pseudo hits -> " + pseudoHits);
}
public static void Main()
{
string solution = "GGRR" ;
string guess = "RGBY" ;
calcResult(solution, guess);
solution = "YGRR" ;
guess = "RGYB" ;
calcResult(solution, guess);
}
}
|
Javascript
<script>
let hits = 0;
let pseudoHits = 0;
let MAX_COLORS = 4;
function codeOfColor(c) {
switch (c) {
case 'B' : return 0;
case 'G' : return 1;
case 'R' : return 2;
case 'Y' : return 3;
default :
return -1;
}
}
function calcResult(guess, solution) {
hits = 0;
pseudoHits = 0;
if (guess.length != solution.length)
document.write( "Incorrect Input<br>" );
let frequencies = new Array(MAX_COLORS).fill(0);
for (let i = 0; i < guess.length; i++) {
if (guess[i] == solution[i])
hits++;
else {
let codecolor = codeOfColor(solution[i]);
frequencies[codecolor]++;
}
}
for (let i = 0; i < guess.length; i++) {
let codecolor = codeOfColor(guess[i]);
if (codecolor >= 0 && frequencies[codecolor] > 0
&& guess[i] != solution[i]) {
pseudoHits++;
frequencies[codecolor]--;
}
}
document.write( "hits -> " + hits + " Pseudo hits -> "
+ pseudoHits + "<br>" );
}
let solution = "GGRR" ;
let guess = "RGBY" ;
calcResult(solution, guess);
solution = "YGRR" ;
guess = "RGYB" ;
calcResult(solution, guess);
</script>
|
Output
hits -> 1 Pseudo hits -> 1
hits -> 1 Pseudo hits -> 2
Time Complexity: O(N)
Auxiliary Space: O(N)
This article is contributed by Mr. Somesh Awasthi.
Last Updated :
17 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...