Let's play Hit and Blow game. A imagines four numbers and B guesses the numbers. After B…
- User
Let's play Hit and Blow game. A imagines four numbers and B guesses the numbers. After B picks out four numbers, A answers:
- The number of numbers which have the same place with numbers A imagined (Hit)
- The number of numbers included (but different place) in the numbers A imagined (Blow)
For example, if A imagined numbers:
9 1 8 2
and B chose:
4 1 5 9
A should say 1 Hit and 1 Blow.
Write a program which reads four numbers A imagined and four numbers B chose and prints the number of Hit and Blow respectively. You may assume that the four numbers are all different and within from 0 to 9. Input
The input consists of multiple datasets. Each dataset set consists of:
a1 a2 a3 a4 b1 b2 b3 b4
, where ai (0 ≤ ai ≤ 9) is i-th number A imagined and bi (0 ≤ bi ≤ 9) is i-th number B chose.
The input ends with EOF. The number of datasets is less than or equal to 50.
Output
For each dataset, print the number of Hit and Blow in a line. These two numbers should be separated by a space.
Example
Input
9 1 8 2 4 1 5 9 4 6 8 2 4 6 3 2
Output
1 1 3 0
Assistant
We need to write a program that reads multiple datasets. Each dataset consists of two lines: first line: a1 a2 a3 a4; second line: b1 b2 b3 b4. All numbers are digits 0-9, all distinct within each set? They say "the four numbers are all different". That applies to both A and B? Probably each set of four numbers (both A's secret and B's guess) have all different digits? Usually in Hit and Blow (Mastermind) both the secret and the guess have no duplicates. They say "You may assume that the four numbers are all different and within from 0 to 9." So both the secret and guess will have distinct numbers. That simplifies.
We need to compute hits: count of positions i where a_i == b_i. Then blows: count of numbers that appear in both sets but not in the same position. So blow = (size of intersection) - hits. Since all numbers distinct, blow = number of common digits minus hits.
We need to read until EOF. For each dataset, output hits and blow separated by space.
Number of datasets <= 50, trivial.
We need to produce code in some language? The problem statement is generic. Usually for such tasks we output code in C or Python etc. But the user "Let's play Hit and Blow game" is a typical programming problem. Probably expects a solution in C or C++. But the user is ChatGPT and wants a program. We'll assume they want a solution in Python? Since it's simpler. But to be safe, I'll provide a solution in Python that reads from stdin until EOF and prints accordingly.
Edge cases: The input might have multiple datasets, each of two lines. There might be trailing newlines. We'll read all lines and process in pairs.
Let's provide code:
import sys
def main(): lines = sys.stdin.read().strip().splitlines() # Process lines in pairs out_lines = [] i = 0 while i < len(lines): # skip empty lines? but sample no empty. To be robust, if line is empty, skip? if not lines[i].strip(): i += 1 continue a_line = lines[i].strip() i += 1 if i >= len(lines): break # incomplete dataset, ignore b_line = lines[i].strip() i += 1 a = list(map(int, a_line.split())) b = list(map(int, b_line.split())) hits = sum(1 for j in range(4) if a[j] == b[j]) common = len(set(a) & set(b)) blow = common - hits out_lines.append(f"{hits} {blow}") sys.stdout.write("\n".join(out_lines))
if name == "main": main()
That should work.
Alternatively, we could read line by line using sys.stdin, but this is fine.
We should note that the problem might expect to handle up to 50 datasets. That's fine.
Now output for the example:
Input:
9 1 8 2 4 1 5 9 4 6 8 2 4 6 3 2
Processing first pair: a=[9,1,8,2], b=[4,1,5,9]. hits: index1: both 1 -> 1 hit; others none. common: {9,1} intersection? Actually a set = {9,1,8,2}, b set = {4,1,5,9} => common = {1,9} => size 2. blow = 2-1=1 => "1 1". Second pair: a=[4,6,8,2], b=[4,6,3,2] => hits: positions 0 (4), 1 (6), 3 (2) => 3 hits; common set: {4,6,8,2} & {4,6,3,2} = {4,6,2}? Actually 8 not in b, 3 not in a => common = {4,6,2} size 3; blow = 3-3=0 => "3 0". Output matches.
Thus answer.
We can also mention that this is a straightforward solution.
But the user might want just a code snippet. I'll provide the Python code.
If they wanted C, I could also provide, but Python is typical for quick solutions.
I'll include comments.
Also note that input numbers are all distinct, so the blow calculation as intersection minus hits works.
Alright.
I'll output final answer with code. ```python
import sys
def main(): # Read all lines from standard input lines = sys.stdin.read().strip().splitlines() out = [] i = 0 while i < len(lines): # Skip empty lines (just in case) if not lines[i].strip(): i += 1 continue a_line = lines[i].strip() i += 1 if i >= len(lines): break # Incomplete dataset – ignore b_line = lines[i].strip() i += 1
# Parse the four integers a = list(map(int, a_line.split())) b = list(map(int, b_line.split())) # Count Hits hits = sum(1 for j in range(4) if a[j] == b[j]) # Count common numbers using set intersection common = len(set(a) & set(b)) # Blows = common numbers not in the correct position blow = common - hits out.append(f"{hits} {blow}") # Print results sys.stdout.write("\n".join(out))if name == "main": main()