Skip to content

USACO 2022 January Contest Bronze Division - Herdle#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

First, we deal with the letters placed correctly, then we will keep frequency arrays to determine the smallest quantities available for each of the letters given.

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    char grid[3][3], grid2[3][3];
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            cin >> grid[i][j];
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            cin >> grid2[i][j];

    int green = 0, yellow = 0;

    int frq[26] = {0};
    int frq2[26] = {0};

    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            if(grid[i][j] == grid2[i][j])
                green++;
            else
            {
                ++frq[grid[i][j] - 'A'];
                ++frq2[grid2[i][j] - 'A'];
            }

    for(int i = 0; i < 26; i++)
        yellow += min(frq[i], frq2[i]);

    cout << green << '\n' << yellow << '\n';
    return 0;
}
grid = [list(input().strip()) for _ in range(3)]
grid2 = [list(input().strip()) for _ in range(3)]
green = 0
yellow = 0
frq = [0] * 26
frq2 = [0] * 26
for i in range(3):
    for j in range(3):
        if grid[i][j] == grid2[i][j]:
            green += 1
        else:
            frq[ord(grid[i][j]) - ord('A')] += 1
            frq2[ord(grid2[i][j]) - ord('A')] += 1
for i in range(26):
    yellow += min(frq[i], frq2[i])
print(green)
print(yellow)