day2 part2 BRUTE FORCE YOLO

This commit is contained in:
Tyrel Souza 2024-12-03 21:37:32 -05:00
parent cb9cc445c0
commit 2e0241fde8

View File

@ -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())