Problem solution · Turing

CCC 2000 S1 - Slot Machines

CCC 2000 S1 - Slot Machines: 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
58 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 2000 S1 - Slot Machines, 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

  • 58 lines of Turing from the credited upstream file ccc00s1.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 2000 S1 - Slot Machines · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2000% problem J3: Slot Machines % given a starting number of coins(x), play three machines in turn (a,b,c)% a pays out 30 coins every 35 turns% b pays out 60 coins every 100 turns% c pays out 9 coins every 10 turns% count the numbers of plays before x = 0 % a big loop until x is zero does the trick.% (Turing allows for multiple exit whens in a loop % so this simplifies the process.) var x, a, b, c : intvar count : int := 0put "How many quaters does Martha have in the jar:"get xput ""put "How many times has the first machine been played since paying out?"get aput ""put "How many times has the second machine been played since paying out?"get bput ""put "How many times has the third machine been played since paying out?"get cput "" loop    exit when x = 0    x := x - 1    count := count + 1    a := a + 1    if a = 35 then        a := 0        x := x + 30    end if    exit when x = 0    x := x - 1    count := count + 1    b := b + 1    if b = 100 then        b := 0        x := x + 60    end if    exit when x = 0    x := x - 1    count := count + 1    c := c + 1    if c = 10 then        c := 0        x := x + 9    end ifend loop put "Martha plays ", count, " times before going broke."  

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 ↗