cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2010 · MediumLoops & simulation

The Stationary Ball

National Olympiad — Regional round (2010 season)

Submission name: thimbles.cpp

Problem summary

Three thimbles are initially labeled 1, 2, 3 from left to right. A ball stays at the left position on the table while thimbles swap places: A swaps left and middle, B swaps middle and right, and C swaps left and right. Find the label finally covering the ball.

Input

A sequence of 1 to 100 letters A, B, C, followed by a period.

Output

The label of the thimble in the left position after all swaps.

Constraints

1 ≤ number of moves ≤ 100.

Worked example

An independently prepared example for this explanation.

Input

CABB.

Output

2

Solution analysis

  1. Represent the labels currently at the three positions by an array initially containing 1, 2, 3.
  2. Process the moves in order and swap the corresponding array entries. The ball does not follow a numbered cup: the official explanation fixes it at the left position.
  3. For CABB, the arrangements are 3,2,1; then 2,3,1; then 2,1,3; then 2,3,1. Label 2 is finally at the left, so the answer is 2.

Why it works

Initially the array matches the physical labels. Each array swap reproduces the corresponding exchange of thimbles, preserving that correspondence after every move. The ball is fixed at the left position, so the first entry after all moves is its covering label.

Complexity: O(L) time for L moves and O(1) extra space.

Watch out. Track labels at positions, not a ball moving with label 1. The period is a terminator, not a move.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
#include <utility>
using namespace std;
int main() {
    int label[3] = {1, 2, 3};
    char move;
    while (cin >> move && move != '.') {
        if (move == 'A') {
            swap(label[0], label[1]);
        } else if (move == 'B') {
            swap(label[1], label[2]);
        } else {
            swap(label[0], label[2]);
        }
    }
    // The ball stays at the left table position, not under label 1.
    cout << label[0] << '\n';
}

Source: Arena Olimpiici ↗