Prize37 Chapter 5, Exercise 2 Water per boat for three traffic patterns Task A busy lock is used by eight boats in one of three patterns: four up then four down, two up then two down repeated, and strict alternation. Using the narrow lock volume, find the water per boat for each pattern, and say which one a lock keeper would prefer and why. (assumed figures throughout) Code (run with Python) def fills(pattern): high = False from_above = 0 for boat in pattern: if boat == "U": from_above += 1 # an up boat always needs a fill from above high = True else: if not high: from_above += 1 # a down boat needs the lock full first high = False return from_above volume = 105.8 for p in ["UUUUDDDD", "UUDDUUDD", "UDUDUDUD"]: f = fills(p) print(p, "fills:", f, "water per boat (m3):", round(f * volume / len(p), 1)) Output (exactly as printed) UUUUDDDD fills: 7 water per boat (m3): 92.6 UUDDUUDD fills: 6 water per boat (m3): 79.3 UDUDUDUD fills: 4 water per boat (m3): 52.9 Answer Four up then four down needs 7 chambers from above (four fills for the climbers, then three more for the descenders, because only the first descender finds the lock already full): 92.6 cubic metres per boat. Two up and two down repeated needs 6 chambers, 79.3 cubic metres per boat. Strict alternation needs only 4 chambers, 52.9 cubic metres per boat, about half of the 105.8 cubic metres a boat would use in a steady stream one way. A lock keeper would prefer alternation, because each boat leaves the lock full for the next one coming the other way, so no water and no time are spent on refilling or emptying. The figures assume the lock starts empty and that every boat is the same size.