Files
AdventOfCode2024/14/14-2.py
2024-12-14 14:44:32 +01:00

71 lines
1.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
from PIL import Image
file = "./input.txt"
Y_TALL = 103
X_WIDE = 101
#file = "./ex.txt"
#Y_TALL = 7
#X_WIDE = 11
from time import time
import re
start_time = time()
def parse_and_go(input_file:str, seconds) -> list[list[int]]:
global Y_TALL, X_WIDE
field = []
for i in range(Y_TALL):
temp = []
for j in range(X_WIDE):
temp.append(0)
field.append(temp)
with open(input_file, 'r') as d:
datei = d.read()
pat = r'p\=(-?\d+),(-?\d+) v\=(-?\d+),(-?\d+)'
match = re.findall(pat, datei)
for m in match:
x, y, vx, vy = map(int, m)
# move X seconds
x = (x + (seconds * vx)) % X_WIDE
y = (y + (seconds * vy)) % Y_TALL
field[y][x] += 1
return field
def print_field(f):
for r in f:
for s in r:
if s == 0:
print(".", end="")
else:
print(s, end="")
print()
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] == 0:
image.putpixel((x, y), (0, 0, 0)) # Schwarz
else:
image.putpixel((x, y), (0, 255, 0)) # Grün
return image
if __name__ == "__main__":
for sec in range(6517): #my solution 6516
field_s = parse_and_go(file, sec)
img = array_to_image(field_s)
img.save(f'./p2_pics/img_{sec}.png')
print(f'Runtime: {time()-start_time:.2f} s')