Problem solution · Python

CCC 2013 S2 - Bridge Transport

CCC 2013 S2 - Bridge Transport: 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
43 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 2013 S2 - Bridge Transport, 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

  • 43 lines of Python from the credited upstream file ccc13s2.py.
  • The implementation visibly relies on sequence storage.
  • 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 2013 S2 - Bridge Transport · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2013 S2: Bridge Transport## This solution is due largely to Nenad Bauk# (also thanks to Victor Wang of Tecumseh Elementary# re: what if the last car makes the bridge collapse?# This is NOT tested in the test data, but is the example given.# This program passed this test :-), as well as the 13 test cases.)## Generally speaking one wishes to calculate the weight of four cars.# The weights are stored in an array (list in python).## Nenad's insight is to simplify things by starting the array with 3# zero weight cars. This way there are no special cases to consider# wrt the first three cars. There are always four cars to add up:# the current one and the 3 previous! # To simplify issues around the last car, I also added a sentinel to# the end, a car with a weight in excess of the max.   #input the datafile = open("s2.0.in", 'r')W = int(file.readline())N = int(file.readline())carWeight = [0,0,0]for i in range(N):    carWeight.append(int(file.readline()))carWeight.append (W+1) # calculate the weight of each group of four cars# stopping when the max weight is exceeded.# the senteniel guarantees I'll never fall off the end of the array# and thus no need to check for that. carsAcross = 0i = 3totalWeight = carWeight[i-3] + carWeight[i-2] + carWeight[i-1] + carWeight[i]while totalWeight <= W:    carsAcross = carsAcross + 1    i = i + 1    totalWeight = carWeight[i-3] + carWeight[i-2] + carWeight[i-1] + carWeight[i]print carsAcross 

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 ↗