diff --git a/2024/python/day02.py b/2024/python/day02.py index 12686a1..35d64b1 100644 --- a/2024/python/day02.py +++ b/2024/python/day02.py @@ -10,14 +10,17 @@ def is_decreasing(sequence): return all(sequence[i] > sequence[i+1] for i in range(len(sequence) - 1)) def parse_line(row): - if not(is_increasing(row) or is_decreasing(row)): - return 0 + diffs = [] for previous, current in zip(row, row[1:]): - diff = abs(current - previous) + diff = current - previous + diffs.append(diff) + diff = abs(diff) if diff >= 1 and diff <= 3: continue - return 0 - return 1 + return 0, diffs + if not(is_increasing(row) or is_decreasing(row)): + return 0, diffs + return 1, diffs # @shared.profile @@ -25,12 +28,30 @@ def part1(rows): rows = [list(map(int, row.split())) for row in rows] safe = 0 for row in rows: - safe += parse_line(row) + check, _ = parse_line(row) + safe += check 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 + # @shared.profile def part2(rows): - pass + 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 + print(safe) def main(): @@ -42,6 +63,7 @@ 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())