day 8 with pic for part2

This commit is contained in:
2025-02-23 10:08:27 +01:00
parent 7ab66a8e6f
commit b7e8ed8cfb
5 changed files with 275 additions and 0 deletions

109
08/08.py Normal file
View File

@@ -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')

4
08/ex.txt Normal file
View File

@@ -0,0 +1,4 @@
rect 3x2
rotate column x=1 by 1
rotate row y=0 by 4
rotate column x=1 by 1

162
08/input.txt Normal file
View File

@@ -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

BIN
08/solution-part2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 B

BIN
requirements.txt Normal file

Binary file not shown.