Sean McLemon | Notes

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2021-12-26 - seven segment display

(original .ipynb)

After AOC 2021 day 8 I wanted to play with seven segment display a little.

tiny_template = """
bac 
b c 
edf 
e f 
ggf 
"""

little_template = """
baac 
b  c 
bddc 
e  f 
eggf 
"""
big_template = """
  aaaa  
 b    c 
 b    c 
  dddd  
 e    f 
 e    f 
  gggg  
"""

all_segments = "abcdefg"
digit_segments = {
    0: "abcefg",
    1: "cf",
    2: "acdeg",
    3: "acdfg",
    4: "bcdf",
    5: "abdfg",
    6: "abdefg",
    7: "acf",
    8: "abcdefg",
    9: "abcdfg"
}

def replace_line(line, segments):
    return "".join("#" if s in segments else " " for s in line)

def render(n, template=big_template):
    digits = [int(s) for s in str(n)]
    lines = []
    for template_line in template.split("\n"):
        line = []
        for d in digits:
            line += replace_line(template_line, digit_segments[d])
        lines.append("".join(line))
    print("\n".join(lines))

    
render(1234567890)

render(1234567890, little_template)

render(1234567890, tiny_template)


def digify(func):
    def wrapped(*args, **kwargs):
        res = func(*args, **kwargs)
        if type(res) == int:
            render(res)
        return res
    return wrapped

@digify
def add(a, b):
    return a + b

print(add(10, 20))
          ####    ####            ####    ####    ####    ####    ####    ####  
      #       #       #  #    #  #       #            #  #    #  #    #  #    # 
      #       #       #  #    #  #       #            #  #    #  #    #  #    # 
          ####    ####    ####    ####    ####            ####    ####          
      #  #            #       #       #  #    #       #  #    #       #  #    # 
      #  #            #       #       #  #    #       #  #    #       #  #    # 
          ####    ####            ####    ####            ####    ####    ####  


   #  ###  ### #  # ###  ###   ### #### #### #### 
   #    #    # #  # #    #       # #  # #  # #  # 
   #  ###  ### #### ###  ###     # #### #### #  # 
   # #       #    #    # #  #    # #  #    # #  # 
   # ###   ###    #  ### ####    # ####  ### #### 


  #  ##  ## # # ##  ##   ## ### ### ### 
  #   #   # # # #   #     # # # # # # # 
  # ##   ##  ##  ## ###   # ###  ## # # 
  # #     #   #   # # #   # # #   # # # 
  # ##  ###   # ### ###   # ### ### ### 


  ####    ####  
      #  #    # 
      #  #    # 
  ####          
      #  #    # 
      #  #    # 
  ####    ####  

30