Advent of Code 2019 馃く (讬讜诐 8)

讘讛爪诇讞讛 :slight_smile:
讬讜诐 8

2 诇讬讬拽讬诐
讞诇拽讬诐 1 讜- 2
import math
from colored import fg, bg, attr


def get_input(path):
    with open(path, 'r') as puzzle_input:
        return puzzle_input.read()


def verify_image(puzzle_input, layer_size=150):
    num_layers = int(len(puzzle_input) / layer_size)
    min_num_zeros = math.inf
    for i in range(num_layers):
        num_zeros = puzzle_input[i * layer_size: (i + 1) * layer_size].count('0')
        if num_zeros < min_num_zeros:
            min_num_zeros = num_zeros
            min_layer_idx = i

    return (puzzle_input[min_layer_idx * layer_size: (min_layer_idx + 1) * layer_size].count('1') 
            * puzzle_input[min_layer_idx * layer_size: (min_layer_idx + 1) * layer_size].count('2'))


def get_final_image(puzzle_input, layer_size=150):
    TRANSPARENT = '2'
    final_image = []
    num_layers = int(len(puzzle_input) / layer_size)
    for i in range(layer_size):
        final_image.append(puzzle_input[i])

    for i in range(1, num_layers):
        current_layer = puzzle_input[i * layer_size: (i + 1) * layer_size]
        for j in range(layer_size):
            if final_image[j] == TRANSPARENT:
                final_image[j] = current_layer[j]
    
    return final_image


def display_message(final_image):
    BLACK = 0
    WHITE = 15
    WIDTH = 25
    HEIGHT = 6
    for i in range(HEIGHT):
        for j in range(WIDTH):
            idx = i * WIDTH + j
            if final_image[idx] == '1':
                color = WHITE
            else:
                color = BLACK
            print(f'%s%s{"*"}%s' %(fg(color), bg(BLACK), attr(0)), end='')
        print()


def main():
    puzzle_input = get_input('input.txt')

    # First Part
    verification_result = verify_image(puzzle_input)

    # Second Part
    final_image = get_final_image(puzzle_input)
    display_message(final_image)


main()
2 诇讬讬拽讬诐
讞诇拽 1 注诐 讟专讬拽 诪讙谞讬讘 诇讞讜诇诇 砖讻讘讜转:

3 诇讬讬拽讬诐

诇拽讞 拽爪转 讝诪谉 诇讛讙讬注 诇讝讛.
讝讛 讛讬讛 拽诇 讘讗讜驻谉 诪驻转讬注 讘讬讞住 诇讬讜诐 7 (砖注讚讬讬谉 诇讗 讛讘谞转讬).

讞诇拽讬诐 1 讜-2
from PIL import Image


PATH = "Day 8.txt"
WIDTH = 25
HEIGHT = 6


def get_challenge_input(path):
    with open(path, 'r') as file:
        challege_input = file.read()
    return challege_input.strip()


def seperate_layers(challege_input, width, height):
    layer_size = width * height
    layers = []
    current_layer = ''
    for char in challege_input:
        if len(current_layer) < layer_size:
            current_layer += char
        else:
            layers.append(current_layer)
            current_layer = char
    layers.append(current_layer)
    return layers


def find_fewest_zeroes_layer(layers):
    return sorted(layers, key=lambda layer: layer.count('0'))[0]


def get_ones_twos_product(layer):
    return layer.count('1') * layer.count('2')


def all_in_one(challege_input, width, length):
    layers = seperate_layers(challege_input, width, length)
    layer = find_fewest_zeroes_layer(layers)
    return get_ones_twos_product(layer)


def determine_pixel_color(layers, index):
    for layer in layers:
        if layer[index] != '2':
            return int(layer[index])


def get_pixel_colors(layers):
    return [determine_pixel_color(layers, index) for index in range(len(layers[0]))]


def draw_image(challege_input, width, height):
    layers = seperate_layers(challenge_input, width, height)
    colors = get_pixel_colors(layers)
    img = Image.new('1', (width, height), 1)
    pixels = img.load()
    for index, pixel in enumerate(colors):
        if not pixel:
            pixels[(index % width, index // width)] = 0
    return img
诇讬讬拽 1