Advent of Code Day 8

In [19]:
def apply_input(filename):
    def inner(fn):
        with open(filename) as input_file:
            return fn(input_file.read().strip())
    return inner
In [4]:
width = 25
height = 6
In [27]:
import re

def solve(inp, width=width, height=height):
    current_zeros = float("inf")
    product = None
    
    while inp:
        layer = inp[:width * height]
        
        inp = inp[width * height:]
        
        zeros = len(re.findall('0', layer))
        
        if zeros < current_zeros:
            current_zeros = zeros
            
            ones = len(re.findall('1', layer))
            twos = len(re.findall('2', layer))
            
            product = ones * twos
    
    return product
In [29]:
solve("123456789012", width=3, height=2)
Out[29]:
1
In [30]:
apply_input("AoC/input8.txt")(solve)
Out[30]:
2064
In [87]:
def render(inp, width=width, height=height):
    image = ['2'] * (width * height)
    
    while inp:
        for i, px in enumerate(inp[:width * height]):
            if image[i] == '2':
                image[i] = px
                
        inp = inp[width*height:]
        
    while image:
        print(" ".join(image[:width]).replace("0", " ").replace("1", "#"))
        image = image[width:]
In [88]:
render("0222112222120000", 2, 2)
  #
#  
In [89]:
apply_input("AoC/input8.txt")(render)
#     #     # #     #     #   # # # #     # #    
#   #     #     #   #     #         #   #     #  
# #       #     #   #     #       #     #     #  
#   #     # # # #   #     #     #       # # # #  
#   #     #     #   #     #   #         #     #  
#     #   #     #     # #     # # # #   #     #  

Mostly about implementing quickly today, and deciphering what the text said. My first attempt was pretty hard to read, and I just manually traced it out instead of trying to make the visualization better -- which would probably have been faster.

I made 2 mistakes that cost me a lot of time: read the first question incorrectly and returned the value for the layer with the most zeros; and in the second I kept forcing a comparison between a string and an int and kept wondering why things weren't working.

Still, some of my strongest ranks so far: 306 (new PR!) / 352.