From 2e0241fde8ba3b4a7d4ebf37138e7cf169f59743 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 3 Dec 2024 21:37:32 -0500 Subject: [PATCH] day2 part2 BRUTE FORCE YOLO --- 2024/python/day02.py | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/2024/python/day02.py b/2024/python/day02.py index 35d64b1..2b9045f 100644 --- a/2024/python/day02.py +++ b/2024/python/day02.py @@ -10,17 +10,14 @@ def is_decreasing(sequence): return all(sequence[i] > sequence[i+1] for i in range(len(sequence) - 1)) def parse_line(row): - diffs = [] for previous, current in zip(row, row[1:]): - diff = current - previous - diffs.append(diff) - diff = abs(diff) + diff = abs(current - previous) if diff >= 1 and diff <= 3: continue - return 0, diffs + return 0 if not(is_increasing(row) or is_decreasing(row)): - return 0, diffs - return 1, diffs + return 0 + return 1 # @shared.profile @@ -28,29 +25,29 @@ def part1(rows): rows = [list(map(int, row.split())) for row in rows] safe = 0 for row in rows: - check, _ = parse_line(row) - safe += check + safe += parse_line(row) print(safe) -def remove_tolerance(row, diffs): - mdiff = max(map(abs, diffs)) - try: - idx = diffs.index(mdiff) - except ValueError: - idx = diffs.index(-1 * mdiff) - row.pop(idx) - return row + +def brute_force(row): + check = parse_line(row) + if check == 1: + return 1 + for x in range(len(row)): + new_row = row[:x] + row[x+1:] + check = parse_line(new_row) + if check == 1: + return 1 + return 0 + + # @shared.profile def part2(rows): rows = [list(map(int, row.split())) for row in rows] safe = 0 for row in rows: - check, diffs = parse_line(row) - if check == 0: - row = remove_tolerance(row, diffs) - check, _ = parse_line(row) - safe += check + safe += brute_force(row) print(safe) @@ -63,7 +60,6 @@ def main(): rows = [row for row in shared.load_rows(2, True)] with shared.elapsed_timer() as elapsed: part2(rows) - print(706, 754) print("🕒", elapsed())