Problem solution · Turing

CCC 2004 J5 - Fractals

CCC 2004 J5 - Fractals: 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
148 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 2004 J5 - Fractals, 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

  • 148 lines of Turing from the credited upstream file ccc04j5.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 2004 J5 - Fractals · TuringTuring
Use this to learn the idea, then write your own version.
% 2004 J5: Fractals%% Long ugly array and geometry calculations% using 1D array storage% % ox,oy are arrays of the old endpoints of the lines making up the fractal% using these lines, the nx,ny arrays of end points are created.% (brute force: all 4 directions are considered.)% then the old arrays are replaced by the new ones% repeat for all the levels% % finally all the points (xcoor,y) where y = 1,2,3,... are checked to see if % they are on any of the lines. If they are, the  var ox : array 1 .. 7000 of intvar oy : array 1 .. 7000 of intvar nx : array 1 .. 7000 of intvar ny : array 1 .. 7000 of intvar ans : array 1 .. 100 of intvar level, xcoor, width, w, size, k, n : intvar done : boolean get level, width, xcoorox (1) := 0oy (1) := 1ox (2) := widthoy (2) := 1size := 2for i : 1 .. level    k := 0    for j : 1 .. size - 1 % for each line        k := k + 1        nx (k) := ox (j)        ny (k) := oy (j)        if oy (j) = oy (j + 1) and ox (j + 1) > ox (j) then % right            w := (ox (j + 1) - ox (j)) div 3            k := k + 1            nx (k) := ox (j) + w            ny (k) := oy (j) + 0            k := k + 1            nx (k) := ox (j) + w            ny (k) := oy (j) + w            k := k + 1            nx (k) := ox (j) + 2 * w            ny (k) := oy (j) + w            k := k + 1            nx (k) := ox (j) + 2 * w            ny (k) := oy (j) + 0            k := k + 1            nx (k) := ox (j + 1)            ny (k) := oy (j + 1)        elsif oy (j) = oy (j + 1) and ox (j + 1) < ox (j) then % left            w := (ox (j) - ox (j + 1)) div 3            k := k + 1            nx (k) := ox (j) - w            ny (k) := oy (j) + 0            k := k + 1            nx (k) := ox (j) - w            ny (k) := oy (j) - w            k := k + 1            nx (k) := ox (j) - 2 * w            ny (k) := oy (j) - w            k := k + 1            nx (k) := ox (j) - 2 * w            ny (k) := oy (j) + 0            k := k + 1            nx (k) := ox (j + 1)            ny (k) := oy (j + 1)        elsif ox (j) = ox (j + 1) and oy (j + 1) < oy (j) then % down            w := (oy (j) - oy (j + 1)) div 3            k := k + 1            nx (k) := ox (j) + 0            ny (k) := oy (j) - w            k := k + 1            nx (k) := ox (j) + w            ny (k) := oy (j) - w            k := k + 1            nx (k) := ox (j) + w            ny (k) := oy (j) - 2 * w            k := k + 1            nx (k) := ox (j) + 0            ny (k) := oy (j) - 2 * w            k := k + 1            nx (k) := ox (j + 1)            ny (k) := oy (j + 1)        elsif ox (j) = ox (j + 1) and oy (j + 1) > oy (j) then % up            w := (oy (j + 1) - oy (j)) div 3            k := k + 1            nx (k) := ox (j) + 0            ny (k) := oy (j) + w            k := k + 1            nx (k) := ox (j) - w            ny (k) := oy (j) + w            k := k + 1            nx (k) := ox (j) - w            ny (k) := oy (j) + 2 * w            k := k + 1            nx (k) := ox (j) + 0            ny (k) := oy (j) + 2 * w            k := k + 1            nx (k) := ox (j + 1)            ny (k) := oy (j + 1)        end if    end for    size := k    for m : 1 .. size        ox (m) := nx (m)        oy (m) := ny (m)    end forend for % graph, just for fun :-)for m : 1 .. size - 1    drawline (ox (m) * 5, oy (m) * 5, ox (m + 1) * 5, oy (m + 1) * 5,        black)end for  % check if the points (xcoor,1), (xcoor,2), ... are on any lines.for i : 1 .. 81    k := 1    done := false    loop        exit when k = size or done        if ox (k) = ox (k + 1) and oy (k) < oy (k + 1)                and xcoor = ox (k) and (i >= oy (k) and i <= oy (k + 1)) then            put i, " " ..            done := true        elsif ox (k) = ox (k + 1) and oy (k) > oy (k + 1)                and xcoor = ox (k) and (i <= oy (k) and i >= oy (k + 1)) then            put i, " " ..            done := true        elsif oy (k) = oy (k + 1) and ox (k) < ox (k + 1)                and xcoor >= ox (k) and xcoor <= ox (k + 1) and i = oy (k)                then            put i, " " ..            done := true        elsif oy (k) = oy (k + 1) and ox (k) > ox (k + 1)                and xcoor <= ox (k) and xcoor >= ox (k + 1) and i = oy (k)                then            put i, " " ..            done := true        end if        k := k + 1    end loopend for  

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 ↗