Problem solution · Turing

CCC 2006 J4 - Tough Being a Teen

CCC 2006 J4 - Tough Being a Teen: 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
106 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 2006 J4 - Tough Being a Teen, 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

  • 106 lines of Turing from the credited upstream file ccc06j4.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 2006 J4 - Tough Being a Teen · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2006 Junior problem 4% Its tough being a teen%% given a set of ordered intructions determine the% order it which to do them.%% this involves array processing% before = the task which must be done first (before)% after = the task which must be done second (after)% taken = true/false depending whether ith element has been done% done = the final order to do the tasks (a string) % to determine the order% cycle thru the numbers 1 to 7 and find first one that can be done.% if one can be done, add it (get out of loop) and start again at 1%                                                    (seven times)% if at any point you get past 7, then the entire thing can't be done.   var before, after : array 1 .. 15 of intvar taken : array 1 .. 15 of booleanvar done : stringvar x, y : intvar size : intvar i, k : intvar stop, cantdoit : boolean % a task can be done if it is never in "after"% or all the tasks before it are done.function canBeDone (i : int) : boolean    var ok : boolean    var neverafter : boolean    var taskbeforedone : boolean    ok := not taken (i)    if ok then        neverafter := true        for j : 1 .. size            if after (j) = i then                neverafter := false            end if        end for        if not neverafter then            taskbeforedone := true            for j : 1 .. size                if after (j) = i and not taken (before (j)) then                    taskbeforedone := false                end if            end for            ok := taskbeforedone        end if    end if    result okend canBeDone   % get the constraintsbefore (1) := 1after (1) := 7before (2) := 1after (2) := 4before (3) := 2after (3) := 1before (4) := 3after (4) := 4before (5) := 3after (5) := 5size := 5loop    get x, y    exit when x = 0 and y = 0    size := size + 1    before (size) := x    after (size) := yend loopfor j : 1 .. 7    taken (j) := falseend fordone := "" % get the orderk := 1cantdoit := falseloop    exit when cantdoit or k > 7    i := 1    stop := false    loop        exit when i > 7 or stop or cantdoit        if not taken (i) and canBeDone (i) then            taken (i) := true            done := done + intstr (i) + " "            stop := true        end if        i := i + 1        if i > 7 and not stop then            done := "Cannot complete these tasks.  Going to bed."            cantdoit := true        end if    end loop    k := k + 1end loopput done  

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 ↗