The data we will use will be
guesses = [1,2,0,3]
answers = [1,2,3,4]
First Expression
guessList.Zip(answerList, (g, s) => g == s)
The Zip extension 'zips' two list by performing the supplied lambda operation on each corresponding element between guessList and answerList, and returns a collection of the results.
Zip essentialy yields
[f(guesses[0], answers[0]), f(guesses[1],
answers[1]), f(guesses[2], answers[2]), f(guesses[3],answers[3])]
where function f is the lambda function supplied:
f(g,s) => g == s
The result is [true,true, false, false]
We then take
.Count(z => z)
of the result of the Zip method, which counts the number of elements in the zipresult that are true.
(read this as .Count(z => z != false) if it helps)
Thus, the Count is 2 because we have two elements that are true. So our RightCRightP is 2. Checking our input this is what we expected, because our first two guesses are the right color and placement, but the last two are not.
Second Expression
guessList.Intersect(answerList)
Quoting http://www.dotnetperls.com/intersect Intersect applies set theory. In set theory, an intersection is the subset of each collection that is found in both collections. In a nutshell it yields a set of items that exist in both lists. Remember that set elements are distinct.
So from our above guesses and answers, intersection of [1,2,0,3] and [1,2,3,4] will yield [1,2,3] ( notice how 3 is included here but its not in the right place! )
We then apply the Sum operation of the result of the intersect. Sum will iterate through each element and find the sum of the results yielded by the supplied lambda expression. The purpose of this intersect operation is to gives us a list of correctly guessed colors irregardless of correct placement.
Our lambda is
c => Min(answerList.Count(x => x == c), guessList.Count(x => x == c))
We count the number of values in answerList and guessList that are equal to color c, and take the smaller of the two. This sum operation gives us the number of items correctly guessed colors irregardless of correct placement. So iterating through the Intersection result of [1,2,3], the lambda will return the value 1 for all iterations, so the Sum will be 1 + 1 + 1 = 3. This is our count of correctly guessed colors.
Since we now have the Number of correct colors, we can deduce the number of correct colors with wrong placement by subtracting the number of right colors with right placements.
RightCWrongP = 3 - 2 => 1.
Checking our inputs guesses = [ 1, 2, 0, 3 ] and answers = [ 1, 2, 3, 4]
RightColorWrongP is 1 like we expected because our guess of colorvalue 3 is a correct color but not in the right place.
P.S. If anything I said is wrong, please let me know in the comments and don't just downvote, I am not a LINQ or C# wizard.