#!/usr/bin/env python3 import re import sys # Order of all "k" elements order = "NPSCD" k = len(order) # Weights of the elements (in the above order) stuff = [ (4, "Nuzzles", 700), (-1, "Prittles", 400), (8, "Skipples", 1000), (10, "Crottles", 2500), (20, "Dupples", 200), ] weights = [item[2] for item in stuff] counts = [item[0] for item in stuff] matrix = [] for line in sys.stdin: line = line.strip() if line == "unsat": sys.exit('unsat!') if line == "sat": continue m = re.match(r'([A-Z])(\d+) -> (\d+)', line) item_type, trucknr, value = m.groups() trucknr, value = int(trucknr), int(value) # Map letter to column col = order.index(item_type) # auto-detect size, extending matrix size if necessary. while len(matrix) < trucknr: matrix.append([None] * k) matrix[trucknr - 1][col] = value # Number of trucks n = len(matrix) line = " " for col in range(k): line += " & %5s" % order[col] line += " & weight" line += r" & \#pallets" line += r" \\" print(line) print(r"\hline") for step, cells in enumerate(matrix): # Add truck nr line = "%2d" % (step + 1) # Add building blocks and calculate weight and number of pallets weight = 0 number_of_pallets = 0 for col, value in enumerate(cells): line += " & %5d" % value number_of_pallets += value weight += value * weights[col] # Add weight and number of pallets line += " & %5d & %d" % (weight, number_of_pallets) line += r" \\" print(line) # Print the total numbers line = " " helperline = "% " # for displaying the expected counts for col in range(len(matrix[0])): total_pallets_for_item = sum(truck[col] for truck in matrix) line += " & %5d" % total_pallets_for_item helperline += " & %5d" % counts[col] print(r"\hline") print(line) #print(helperline)