From 2b84767d5b493bde658b443637b8733cfaf57a34 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 13 Jan 2017 18:18:51 +0100 Subject: FoodDelivery: improve solution for b Now requires truck to return to a location, also accept any location instead of just the supply location S. --- food_delivery/food-to-table.py | 11 ++++++++--- food_delivery/generate-food.py | 25 +++++++++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/food_delivery/food-to-table.py b/food_delivery/food-to-table.py index b94ad0c..c66a774 100755 --- a/food_delivery/food-to-table.py +++ b/food_delivery/food-to-table.py @@ -7,6 +7,8 @@ n = 5 matrix = [] nextNodes = {} +# The pair that shows a loop (for b) +pair_for_b = [None] * 2 for line in sys.stdin: line = line.strip() @@ -23,6 +25,10 @@ for line in sys.stdin: matrix.append([None] * n) matrix[step][villagenr] = value + elif line.startswith('k'): + m = re.match(r'k(\d+) -> (\d+)', line) + step, value = map(int, m.groups()) + pair_for_b[step] = value else: m = re.match(r'l(\d+) -> (\d+)', line) step, value = map(int, m.groups()) @@ -45,8 +51,7 @@ for step, cells in enumerate(matrix): else: line += " & %5d" % value line += r" & %s \\" % "SABCD"[nextNodes[step]] - if step > 0: - if all(x >= y for x, y in zip(matrix[step][1:], matrix[0][1:])): - line += " % HIGHER" + if any(x == step for x in pair_for_b): + line += " % PAIR" #line += r" \\" print(line) diff --git a/food_delivery/generate-food.py b/food_delivery/generate-food.py index dc87c03..192131b 100755 --- a/food_delivery/generate-food.py +++ b/food_delivery/generate-food.py @@ -118,17 +118,22 @@ for j in range(1, m + 1): preds += ["(or %s)" % "\n".join(altpreds)] if is_b: - # If stock is higher or equal to initial state, then we found a loop. + # If any stock is higher or equal to another state in the same location, + # then we found a loop. stuff = [] - for j in range(1, m + 1): - jprev = j - 1 - highers = " ".join([ - "(>= s{i}_{j} s{i}_0)".format(i=i, j=j) - for i in range(1, len(states)) - ]) - stuff += fillin([ - "(and {highers})" - ], vars()) + literals += ["k0", "k1"] + for k in range(1, m + 1): + for j in range(1, m + 1): + if k >= j: + continue + jprev = j - 1 + highers = " ".join([ + "(>= s{i}_{j} s{i}_{k})".format(i=i, j=j, k=k) + for i in range(1, len(states)) + ]) + stuff += fillin([ + "(and {highers} (= l{j} l{k}) (= k0 {j}) (= k1 {k}))" + ], vars()) preds += ["(or %s)" % " ".join(stuff)] -- cgit v1.2.1