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

66 lines
1.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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 safety_factor(f:list[list[int]])->int:
q1, q2, q3, q4 = 0, 0, 0, 0
for i in range(len(f)//2):
for j in range(len(f[0])//2):
q1 += f[i][j]
for i in range(len(f)//2):
for j in range((len(f[0])//2)+1,len(f[0])):
q2 += f[i][j]
for i in range((len(f)//2)+1,len(f)):
for j in range(len(f[0])//2):
q3 += f[i][j]
for i in range((len(f)//2)+1,len(f)):
for j in range((len(f[0])//2)+1,len(f[0])):
q4 += f[i][j]
print("Quarter-Safety:",q1,q2,q3,q4)
return q1*q2*q3*q4
if __name__ == "__main__":
p1_f = parse_and_go(file, 100)
print_field(p1_f)
print(f"SafetyFactor-Part1: {safety_factor(p1_f)}")
print(f'Runtime: {time()-start_time:.2f} s')