From f9ab75b54f22e3ee51680cb81d2a5826b6ff5a01 Mon Sep 17 00:00:00 2001 From: tebarius Date: Thu, 19 Dec 2024 17:38:34 +0100 Subject: [PATCH] 24-part1 --- 24/24-1.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/24/24-1.py b/24/24-1.py index ab953cd..25d4936 100644 --- a/24/24-1.py +++ b/24/24-1.py @@ -5,12 +5,12 @@ from time import time import numpy as np start_time = time() -#file = "./input.txt" -#XY_MIN = 200000000000000 -#XY_MAX = 400000000000000 -file = "./ex.txt" -XY_MIN = 7 -XY_MAX = 27 +file = "./input.txt" +XY_MIN = 200000000000000 +XY_MAX = 400000000000000 +#file = "./ex.txt" +#XY_MIN = 7 +#XY_MAX = 27 def berechne_schnittpunkt(p1, v1, p2, v2): @@ -50,34 +50,32 @@ def read_input(filename:str)->list[list[int]]: if line == "": continue else: - pat = r'^(-?\d+), (-?\d+), -?\d+ \@ (-?\d+), (-?\d+), -?\d+$' + pat = r'^(-?\d+), +(-?\d+), +-?\d+ +\@ +(-?\d+), +(-?\d+), +-?\d+$' match = re.match(pat, line) - if match: - result.append(list(map(int,[match.group(1), match.group(2), match.group(3), match.group(4)]))) + result.append(list(map(int,[match.group(1), match.group(2), match.group(3), match.group(4)]))) input_file.close() return result if __name__ == "__main__": sol = 0 - ''' # Beispielaufruf + ''' # Beispielaufruf p_1 = (18, 19) v_1 = (-1, -1) p_2 = (12, 31) v_2 = (-1, -2) schnitt_punkt = berechne_schnittpunkt(p_1, v_1, p_2, v_2) if schnitt_punkt: - print(f"Der Schnittpunkt liegt bei: {schnitt_punkt[0]} {type(schnitt_punkt[0])} {schnitt_punkt[1]}") + print(f"Der Schnittpunkt liegt bei: {schnitt_punkt[0]} {schnitt_punkt[1]}") else: print("Die Strahlen schneiden sich nicht.") ''' + d = read_input(file) for i in range(len(d)): - for j in range(i, len(d)): - s = berechne_schnittpunkt((d[i][0],d[j][0]), (d[i][2],d[j][2]), (d[i][1],d[j][1]), (d[i][3],d[j][3])) - if s: - print(s[0],s[1]) + for j in range(i+1, len(d)): + s = berechne_schnittpunkt((d[i][0],d[i][1]), (d[i][2],d[i][3]), + (d[j][0],d[j][1]), (d[j][2],d[j][3])) if s and XY_MIN < s[0] < XY_MAX and XY_MIN < s[1] < XY_MAX: - print("------------", s[0], s[1]) sol += 1 print(f"Solution Part1: {sol}") print(f'Runtime: {time()-start_time:.2f} s')