Problem solution · Python

CCC 2011 J4 - Boring Business

CCC 2011 J4 - Boring Business: a Python 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
82 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 2011 J4 - Boring Business, 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

  • 82 lines of Python from the credited upstream file ccc11j4.py.
  • 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 2011 J4 - Boring Business · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2011 Junior 4: Boring Business# written by C. Robart# March 2011 # dictionary (or 2d arrays)# relatively straight forward 2d movement # create the original well planwellPlan = {}for row in range(-1,-201,-1):    for col in range (-200,201):        wellPlan[(row,col)] = False        wellPlan[(-1,0)] = TruewellPlan[(-2,0)] = TruewellPlan[(-3,0)] = TruewellPlan[(-3,1)] = TruewellPlan[(-3,2)] = TruewellPlan[(-3,3)] = TruewellPlan[(-4,3)] = TruewellPlan[(-5,3)] = TruewellPlan[(-5,4)] = TruewellPlan[(-5,5)] = TruewellPlan[(-4,5)] = TruewellPlan[(-3,5)] = TruewellPlan[(-3,6)] = TruewellPlan[(-3,7)] = TruewellPlan[(-4,7)] = TruewellPlan[(-5,7)] = TruewellPlan[(-6,7)] = TruewellPlan[(-7,7)] = TruewellPlan[(-7,6)] = TruewellPlan[(-7,5)] = TruewellPlan[(-7,4)] = TruewellPlan[(-7,3)] = TruewellPlan[(-7,2)] = TruewellPlan[(-7,1)] = TruewellPlan[(-7,0)] = TruewellPlan[(-7,-1)] = TruewellPlan[(-6,-1)] = TruewellPlan[(-5,-1)] = True # read instructionsline = raw_input()direction = line[0:1]distance = eval(line[2:]) ok = Truerow = -5col = -1 # do the drilling!while ok and direction != "q":    dr = 0    dc = 0    if direction == "l":        dc = -1           elif direction == "u":        dr = 1    elif direction == "r":        dc = 1    elif direction == "d":        dr = -1     while distance > 0:        row = row + dr        col = col + dc        if wellPlan[(row,col)]:            ok = False        else:            wellPlan[(row,col)] = True        distance = distance - 1     if ok:        print col, row, "safe"    else:        print col, row, "DANGER"     line = raw_input()    direction = line[0:1]    distance = eval(line[2:])  

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 ↗