From 83c2415177293e6b67e7208ca95a2f01e30fb344 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Sat, 14 Dec 2024 12:15:46 +0100 Subject: 2024 day 14 --- 2024/day14/solve.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 2024/day14/solve.py (limited to '2024/day14/solve.py') diff --git a/2024/day14/solve.py b/2024/day14/solve.py new file mode 100644 index 0000000..acbd6af --- /dev/null +++ b/2024/day14/solve.py @@ -0,0 +1,86 @@ +input_file = "input" + +def print_positions(robots, col_count, row_count, iter_count): + print_data = {} + for robot in robots: + key = (robot[1], robot[0]) + if key not in print_data: + print_data[key] = 0 + + print_data[key] += 1 + + longest_line_threshold = 10 + longest_line = 0 + print_this = False + for row in range(row_count): + for col in range(col_count): + key = (row, col) + if key in print_data: + longest_line += 1 + if longest_line >= longest_line_threshold: + print_this = True + break + else: + longest_line = 0 + + if print_this: + break + + if print_this: + print(f"Positions after {iter_count + 1:000} seconds") + for row in range(row_count): + for col in range(col_count): + key = (row, col) + if key in print_data: + print(print_data[key], end="") + else: + print(".", end="") + print() + + input() + + +def move_robots(robots: list[list[int]], col_count: int, row_count: int): + for robot in robots: + robot[0] += robot[2] + robot[1] += robot[3] + + if robot[0] < 0: + robot[0] += col_count + elif robot[0] >= col_count: + robot[0] -= col_count + + if robot[1] < 0: + robot[1] += row_count + elif robot[1] >= row_count: + robot[1] -= row_count + +data: list[list[int]] = [] +with open(input_file) as f: + for line in f: + data.append([int(elem) for elem in line.strip().replace("p=", "").replace(" v=", ",").split(",")]) + +wideness = 101 if input_file == "input" else 11 +height = 103 if input_file == "input" else 7 + +print_mode_on = True +# second part + +range_max = 100 if not print_mode_on else 10000 +for ii in range(range_max): + move_robots(data, wideness, height) + if print_mode_on: + print_positions(data, wideness, height, ii) + +middle_col = wideness // 2 +middle_row = height // 2 + +quads = [[0, 0], [0, 0]] +for elem in data: + if elem[0] == middle_col or elem[1] == middle_row: + continue + + quads[0 if elem[0] < middle_col else 1][0 if elem[1] < middle_row else 1] += 1 + +result = quads[0][0] * quads[1][1] * quads[0][1] * quads[1][0] +print(result) -- cgit v1.2.3-70-g09d2