diff --git a/08/08.py b/08/08.py new file mode 100644 index 0000000..698ee57 --- /dev/null +++ b/08/08.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +#file = "./ex.txt" +#f_rows = 3 +#f_columns = 7 + +file = "./input.txt" +f_rows = 6 +f_columns = 50 + +from time import time +from copy import deepcopy +import numpy as np +from PIL import Image, ImageDraw, ImageFont + +start_time = time() + +def create_field(columns:int, rows:int) -> list[list[str]]: + result = [] + for r in range(rows): + row = [] + for col in range(columns): + row.append(".") + result.append(row) + return result + +def print_field(f): + for r in f: + for s in r: + print(s, end="") + print() + +def read_file(filename:str)->list[list[str]]: + result = [] + input_file = open(filename, "r") + for line in input_file: + line = line.strip() + if line == "": + continue + else: + result.append(line.split()) + input_file.close() + return result + +def do_command(c:list[str], f:list[list[str]])->list[list[str]]: + result = deepcopy(f) + if c[0] == "rect": + co, ro = command[1].split("x") + for i in range(int(ro)): + for j in range(int(co)): + result[i][j] = "#" + + elif c[0] == "rotate" and c[1] == "row": + selected_row = int(c[2].split("=")[1]) + row_shift_by = int(c[4]) + for _ in range(row_shift_by): + r_tmp = result[selected_row][f_columns-1] + for i in range(f_columns-1): + result[selected_row][f_columns-i-1] = result[selected_row][f_columns-i-2] + result[selected_row][0] = r_tmp + + elif c[0] == "rotate" and c[1] == "column": + selected_column = int(c[2].split("=")[1]) + column_shift_by = int(c[4]) + for _ in range(column_shift_by): + r_tmp = result[f_rows-1][selected_column] + for i in range(f_rows-1): + result[f_rows-i-1][selected_column] = result[f_rows-i-2][selected_column] + result[0][selected_column] = r_tmp + return result + +def count_pixel(f:list[list[str]])->int: + result = 0 + for r in f: + for c in r: + if c == "#": + result += 1 + return result + +def array_to_image(ar): + # Konvertiere das Array in ein NumPy-Array + np_array = np.array(ar) + # Erstelle ein leeres RGB-Bild + height, width = np_array.shape + image = Image.new('RGB', (width, height)) + # Fülle das Bild mit Pixeln basierend auf den Array-Werten + for y in range(height): + for x in range(width): + if np_array[y, x] == "#": + image.putpixel((x, y), (0, 0, 0)) # Schwarz + else: + image.putpixel((x, y), (0, 255, 0)) # Grün + return image + +if __name__ == "__main__": + field = create_field(int(f_columns), int(f_rows)) + commands = read_file(file) + for command in commands: + field = do_command(command, field) + print(f"Solution Part1: {count_pixel(field)}") + + print(f"Solution Part2:") + print_field(field) + img = array_to_image(field) + img.save("./solution-part2.png") + + print(f'Runtime: {time()-start_time:.4f} s') \ No newline at end of file diff --git a/08/ex.txt b/08/ex.txt new file mode 100644 index 0000000..a9df2bc --- /dev/null +++ b/08/ex.txt @@ -0,0 +1,4 @@ +rect 3x2 +rotate column x=1 by 1 +rotate row y=0 by 4 +rotate column x=1 by 1 diff --git a/08/input.txt b/08/input.txt new file mode 100644 index 0000000..6085312 --- /dev/null +++ b/08/input.txt @@ -0,0 +1,162 @@ +rect 1x1 +rotate row y=0 by 6 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 4 +rect 2x1 +rotate row y=0 by 5 +rect 2x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 5 +rect 4x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 6 +rect 4x1 +rotate row y=0 by 4 +rotate column x=0 by 1 +rect 3x1 +rotate row y=0 by 6 +rotate column x=0 by 1 +rect 4x1 +rotate column x=10 by 1 +rotate row y=2 by 16 +rotate row y=0 by 8 +rotate column x=5 by 1 +rotate column x=0 by 1 +rect 7x1 +rotate column x=37 by 1 +rotate column x=21 by 2 +rotate column x=15 by 1 +rotate column x=11 by 2 +rotate row y=2 by 39 +rotate row y=0 by 36 +rotate column x=33 by 2 +rotate column x=32 by 1 +rotate column x=28 by 2 +rotate column x=27 by 1 +rotate column x=25 by 1 +rotate column x=22 by 1 +rotate column x=21 by 2 +rotate column x=20 by 3 +rotate column x=18 by 1 +rotate column x=15 by 2 +rotate column x=12 by 1 +rotate column x=10 by 1 +rotate column x=6 by 2 +rotate column x=5 by 1 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 35x1 +rotate column x=45 by 1 +rotate row y=1 by 28 +rotate column x=38 by 2 +rotate column x=33 by 1 +rotate column x=28 by 1 +rotate column x=23 by 1 +rotate column x=18 by 1 +rotate column x=13 by 2 +rotate column x=8 by 1 +rotate column x=3 by 1 +rotate row y=3 by 2 +rotate row y=2 by 2 +rotate row y=1 by 5 +rotate row y=0 by 1 +rect 1x5 +rotate column x=43 by 1 +rotate column x=31 by 1 +rotate row y=4 by 35 +rotate row y=3 by 20 +rotate row y=1 by 27 +rotate row y=0 by 20 +rotate column x=17 by 1 +rotate column x=15 by 1 +rotate column x=12 by 1 +rotate column x=11 by 2 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=7 by 1 +rotate column x=5 by 1 +rotate column x=3 by 2 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 19x1 +rotate column x=20 by 3 +rotate column x=14 by 1 +rotate column x=9 by 1 +rotate row y=4 by 15 +rotate row y=3 by 13 +rotate row y=2 by 15 +rotate row y=1 by 18 +rotate row y=0 by 15 +rotate column x=13 by 1 +rotate column x=12 by 1 +rotate column x=11 by 3 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=7 by 1 +rotate column x=6 by 1 +rotate column x=5 by 1 +rotate column x=3 by 2 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 14x1 +rotate row y=3 by 47 +rotate column x=19 by 3 +rotate column x=9 by 3 +rotate column x=4 by 3 +rotate row y=5 by 5 +rotate row y=4 by 5 +rotate row y=3 by 8 +rotate row y=1 by 5 +rotate column x=3 by 2 +rotate column x=2 by 3 +rotate column x=1 by 2 +rotate column x=0 by 2 +rect 4x2 +rotate column x=35 by 5 +rotate column x=20 by 3 +rotate column x=10 by 5 +rotate column x=3 by 2 +rotate row y=5 by 20 +rotate row y=3 by 30 +rotate row y=2 by 45 +rotate row y=1 by 30 +rotate column x=48 by 5 +rotate column x=47 by 5 +rotate column x=46 by 3 +rotate column x=45 by 4 +rotate column x=43 by 5 +rotate column x=42 by 5 +rotate column x=41 by 5 +rotate column x=38 by 1 +rotate column x=37 by 5 +rotate column x=36 by 5 +rotate column x=35 by 1 +rotate column x=33 by 1 +rotate column x=32 by 5 +rotate column x=31 by 5 +rotate column x=28 by 5 +rotate column x=27 by 5 +rotate column x=26 by 5 +rotate column x=17 by 5 +rotate column x=16 by 5 +rotate column x=15 by 4 +rotate column x=13 by 1 +rotate column x=12 by 5 +rotate column x=11 by 5 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=2 by 5 +rotate column x=1 by 5 diff --git a/08/solution-part2.png b/08/solution-part2.png new file mode 100644 index 0000000..e058643 Binary files /dev/null and b/08/solution-part2.png differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9c705ab Binary files /dev/null and b/requirements.txt differ