Problem solution · Turing

CCC 2006 J1 - Canadian Calorie Counting

CCC 2006 J1 - Canadian Calorie Counting: 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
62 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 J1 - Canadian Calorie Counting, 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

  • 62 lines of Turing from the credited upstream file ccc06j1.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 J1 - Canadian Calorie Counting · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2006 Junior problem 1% The New CCC (Canadian Calorie Counting)%% simple I/O and if's var burger, side, drink, dessert : intvar calorie : int put "Welcome to Chip's Fast Food Emporium"put "Please enter your burger choice: "get burgerput "Please enter your side order choice: "get sideput "Please enter your drink choice: "get drinkput "Please enter your desert choice: "get dessert if burger = 1 then    calorie := 461elsif burger = 2 then    calorie := 431elsif burger = 3 then    calorie := 420else    calorie := 0end if if side = 1 then    calorie := calorie + 100elsif side = 2 then    calorie := calorie + 57elsif side = 3 then    calorie := calorie + 70else    calorie := calorie + 0end if if drink = 1 then    calorie := calorie + 130elsif drink = 2 then    calorie := calorie + 160elsif drink = 3 then    calorie := calorie + 118else    calorie := calorie + 0end if if dessert = 1 then    calorie := calorie + 167elsif dessert = 2 then    calorie := calorie + 266elsif dessert = 3 then    calorie := calorie + 75else    calorie := calorie + 0end if put ""put "Your total Calorie count is ", calorie, "."  

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 ↗