Problem solution · Turing

CCC 2007 J5 - Keep on Truckin'

CCC 2007 J5 - Keep on Truckin': 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
76 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 2007 J5 - Keep on Truckin', 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

  • 76 lines of Turing from the credited upstream file ccc07j5.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 2007 J5 - Keep on Truckin' · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2007% J5: Keep on Truckin'%%  Arrays and dynamic programming%  This approach is due to Konstantin Lopyrev%%  This is a more straight forward dp approach %  than my version. Simply stated the ways to a motel i%  = the sum of all ways to previous motels in the range%  i-max to i-min. %%  The use of a boolean array eliminates the need for%  sorting.  var motel : array 0 .. 7000 of booleanvar ways : array 0 .. 7000 of intvar minn, maax, n, d, a : int for i : 0 .. 7000    motel (i) := falseend formotel (0) := truemotel (990) := truemotel (1010) := truemotel (1970) := truemotel (2030) := truemotel (2940) := truemotel (3060) := truemotel (3930) := truemotel (4060) := truemotel (4970) := truemotel (5030) := truemotel (5990) := truemotel (6010) := truemotel (7000) := true % get inputget minnget maaxget nfor i : 1 .. n    get d    motel (d) := trueend for % you can get to zero 1 wayways (0) := 1 for i : 1 .. 7000     % for each motel    if motel (i) then                % the ways to that motel = the sum of the ways        % from the previous motels in the range        % i-max to i-min (check that i-max is not less than 0)        ways (i) := 0        a := i - maax        if a < 0 then            a := 0        end if        for j : a .. i - minn            if motel (j) then                ways (i) := ways (i) + ways (j)            end if        end for    end ifend for put ways (7000)      

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 ↗