def main(verbose=False): COLORS = ['\033[98m', '\033[96m', '\033[95m', '\033[94m', '\033[92m', '\033[91m', '\033[98m'] # Loops back to beginning ENDC = '\033[0m' path = successful_path() result = [[path[i][0] + path[i + 1][0], path[i + 1][1]] for i in range(5)] result.append([path[-1][0] + path[0][0], path[0][1]]) result = [elt + [reverse_polygonal_number(elt[1], int(elt[0]))] for elt in result] display = "" for i, entry in enumerate(result): value, sides, number = entry left = str(value)[:2] right = str(value)[-2:] colored_val = "".join([COLORS[i], left, ENDC, COLORS[i + 1], right, ENDC]) display += "\nP_(%s,%s) = %s" % (sides, number, colored_val) if verbose: return "%s.%s" % (sum(int(match[0]) for match in result), display) else: return sum(int(match[0]) for match in result)
def num_triangle(): # Assumes file is "A","ABILITIY","ABLE",... words = get_data(42).strip('"').split('","') vals = [word_to_value(word) for word in words] triangle_hash = {} count = 0 for val in vals: if reverse_polygonal_number(3, val, triangle_hash) != -1: count += 1 return count
def main(verbose=False): # Not only finds the minimum, but also checks to make sure # it is the smallest. Since P_j - P_k >= P_j - P_(j-1) = 3j - 2 # If 3j - 2 > D, then P_j - P_k > D, and we not longer need to # check the minimum pair = [1, 2] D = -1 while D == -1 or 3*pair[1] - 2 <= D: vals = [polygonal_number(5, val) for val in pair] difference = abs(vals[0] - vals[1]) if D != -1 and difference > D: # since increment decreases the first argument, if # we go past the difference, we can stop checking # [k, j] for a fixed j, and we just bypass # by incrementing j last = pair[1] pair = [last, last + 1] else: if reverse_polygonal_number(5, difference) != -1: if reverse_polygonal_number(5, sum(vals)) != -1: if D == -1 or difference < D: D = difference pair = increment_pair(pair) return D