#!/usr/bin/python3 """ANSI-art-animate a simple algorithm for solving an Advent of Code puzzle. The objective is to transform a string like 'sevenineoeo3seveightxon' into a list of digits like [7, 9, 3, 8]. """ import sys import time # ANSI escape sequences cls, home, sgr = '\033[2J', '\033[H', lambda c: f'\033[{c}m' # https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit normal, blue, red = sgr(0), sgr('1;34'), sgr('1;31') def main(s): digits = dict((str(d), d) for d in range(10)) words = 'zero one two three four five six seven eight nine'.split() for d, word in enumerate(words): digits[word] = d matches = [] for p in range(len(s)): for d in digits: match = (s[p:p+len(d)] == d) if match: matches.append(digits[d]) print(f''' {cls}{home}{normal }{" " * p}{ d } { s[:p]}{red if match else blue}{s[p:p+len(d)]}{normal}{s[p+len(d):]} {blue if match else normal}{matches} ''') time.sleep(1.4632 if match else .0542) if __name__ == '__main__': main('sevenineoeo3seveightxon' if len(sys.argv) < 2 else sys.argv[1])