Problem solution · Turing

CCC 2010 J4 - Global Warming

CCC 2010 J4 - Global Warming: 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
55 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 J4 - Global Warming, 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

  • 55 lines of Turing from the credited upstream file ccc10j4.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 J4 - Global Warming · TuringTuring
Use this to learn the idea, then write your own version.
% J4: 2010: Global Warming%% while not totally obvious from the given examples% you need to find a repeated parttern, STARTING FROM THE FIRST% position in the difference array.%% To find that pattern length, try pattern lengths of 1, 2, 3, ..% if its a true pattern, then% diff(a) and diff(a+pattern) (for all a >=1) should be equal.   var n, a, b : intvar diff : array 1 .. 19 of intvar pattern : int loop    get n    exit when n = 0     % handle the case of size 1    if n = 1 then	get a	put 0    else 	% normal case, get differences	get a	for i : 1 .. n - 1	    get b	    diff (i) := b - a	    a := b	end for 	% try a pattern length of 1, 2,3... until one works.	pattern := 1	loop	    a := 1	    loop		exit when a + pattern > n - 1 or diff (a) not= diff (a + pattern)		a := a + 1	    end loop 	    % you've found the correct pattern if a is at the end.	    % ie. you've tried them all and diif(a) = diff (a+pattern)	    exit when a + pattern > n - 1 	    % okay, try the next pattern length	    pattern := pattern + 1	end loop	put pattern    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 ↗