Problem solution · Turing

CCC 1999 P1 - Card Game

CCC 1999 P1 - Card Game: a Turing solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
82 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 1999 P1 - Card Game, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 82 lines of Turing from the credited upstream file ccc99s1.t.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 1999 P1 - Card Game · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1999% problem A: CardGame%% read a deck of 52 cards (words)% high cards = jack and above. % keep score (alternating between players) % ace + none of next 4 are high cards = 4pts% king + none of next 3 are high cards = 3pts% queen + none of next 2 are high cards = 2pts% jack + none of next 1 are high cards = 1pt % file handling is used, the 52 cards given var infile : string := "card.in"var outfile : string := "card.out"var fi, fo : intvar card : array 1 .. 52 of stringvar a, b, v : int % checking if next x cards, starting at i, contain a high card. % at end of deck return false because no points are scored.function noHigh (i : int, x : int) : boolean    if i + x > 52 then        result false    end if    for j : i + 1 .. i + x        if card (j) = "ace"                or card (j) = "king"                or card (j) = "queen"                or card (j) = "jack" then            result false        end if    end for    result trueend noHigh open : fi, infile, getopen : fo, outfile, puta := 0b := 0for i : 1 .. 52    get : fi, card (i)end for for i : 1 .. 52    if card (i) = "ace" and noHigh (i, 4) then        v := 4    elsif card (i) = "king" and noHigh (i, 3) then        v := 3    elsif card (i) = "queen" and noHigh (i, 2) then        v := 2    elsif card (i) = "jack" and noHigh (i, 1) then        v := 1    else        v := 0    end if    if v > 0 then        var p : string        if i mod 2 = 1 then            p := "A"            a := a + v        else            p := "B"            b := b + v        end if        put : fo, "Player ", p, " scores ", v, " point(s)."    end ifend for put : fo, "Player A: ", a, " point(s)."put : fo, "Player B: ", b, " point(s)." close : ficlose : fo      

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗