Problem solution · Turing

CCC 2010 J3 - Punchy

CCC 2010 J3 - Punchy: 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 2010 J3 - Punchy, 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 ccc10j3.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 2010 J3 - Punchy · TuringTuring
Use this to learn the idea, then write your own version.
% J3: 2010: Punchy%% straight forward loop with if's% var a, b : intvar instruction : intvar var1, var2 : string (1)var n : int a := 0b := 0 loop    get instruction    exit when instruction = 7    if instruction = 1 then	get var1	get n	if var1 = "A" then	    a := n	else	    b := n	end if    elsif instruction = 2 then	get var1	if var1 = "A" then	    put a	else	    put b	end if    else   % it's 3 4 5 6 (+ * - /)	get var1	get var2	if var1 = "A" then	    if var2 = "A" then		if instruction = 3 then		    a := a + a		elsif instruction = 4 then		    a := a * a		elsif instruction = 5 then		    a := a - a		else		    a := a div a		end if	    else		if instruction = 3 then		    a := a + b		elsif instruction = 4 then		    a := a * b		elsif instruction = 5 then		    a := a - b		else		    a := a div b		end if	    end if	else	    if var2 = "A" then		if instruction = 3 then		    b := b + a		elsif instruction = 4 then		    b := b * a		elsif instruction = 5 then		    b := b - a		else		    b := b div a		end if	    else		if instruction = 3 then		    b := b + b		elsif instruction = 4 then		    b := b * b		elsif instruction = 5 then		    b := b - b		else		    b := b div b		end if	    end if	end if    end ifend loop 

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 ↗