day2 part2 BRUTE FORCE YOLO
This commit is contained in:
parent
cb9cc445c0
commit
2e0241fde8
@ -10,17 +10,14 @@ def is_decreasing(sequence):
|
|||||||
return all(sequence[i] > sequence[i+1] for i in range(len(sequence) - 1))
|
return all(sequence[i] > sequence[i+1] for i in range(len(sequence) - 1))
|
||||||
|
|
||||||
def parse_line(row):
|
def parse_line(row):
|
||||||
diffs = []
|
|
||||||
for previous, current in zip(row, row[1:]):
|
for previous, current in zip(row, row[1:]):
|
||||||
diff = current - previous
|
diff = abs(current - previous)
|
||||||
diffs.append(diff)
|
|
||||||
diff = abs(diff)
|
|
||||||
if diff >= 1 and diff <= 3:
|
if diff >= 1 and diff <= 3:
|
||||||
continue
|
continue
|
||||||
return 0, diffs
|
return 0
|
||||||
if not(is_increasing(row) or is_decreasing(row)):
|
if not(is_increasing(row) or is_decreasing(row)):
|
||||||
return 0, diffs
|
return 0
|
||||||
return 1, diffs
|
return 1
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
@ -28,29 +25,29 @@ def part1(rows):
|
|||||||
rows = [list(map(int, row.split())) for row in rows]
|
rows = [list(map(int, row.split())) for row in rows]
|
||||||
safe = 0
|
safe = 0
|
||||||
for row in rows:
|
for row in rows:
|
||||||
check, _ = parse_line(row)
|
safe += parse_line(row)
|
||||||
safe += check
|
|
||||||
print(safe)
|
print(safe)
|
||||||
|
|
||||||
def remove_tolerance(row, diffs):
|
|
||||||
mdiff = max(map(abs, diffs))
|
def brute_force(row):
|
||||||
try:
|
check = parse_line(row)
|
||||||
idx = diffs.index(mdiff)
|
if check == 1:
|
||||||
except ValueError:
|
return 1
|
||||||
idx = diffs.index(-1 * mdiff)
|
for x in range(len(row)):
|
||||||
row.pop(idx)
|
new_row = row[:x] + row[x+1:]
|
||||||
return row
|
check = parse_line(new_row)
|
||||||
|
if check == 1:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
def part2(rows):
|
def part2(rows):
|
||||||
rows = [list(map(int, row.split())) for row in rows]
|
rows = [list(map(int, row.split())) for row in rows]
|
||||||
safe = 0
|
safe = 0
|
||||||
for row in rows:
|
for row in rows:
|
||||||
check, diffs = parse_line(row)
|
safe += brute_force(row)
|
||||||
if check == 0:
|
|
||||||
row = remove_tolerance(row, diffs)
|
|
||||||
check, _ = parse_line(row)
|
|
||||||
safe += check
|
|
||||||
print(safe)
|
print(safe)
|
||||||
|
|
||||||
|
|
||||||
@ -63,7 +60,6 @@ def main():
|
|||||||
rows = [row for row in shared.load_rows(2, True)]
|
rows = [row for row in shared.load_rows(2, True)]
|
||||||
with shared.elapsed_timer() as elapsed:
|
with shared.elapsed_timer() as elapsed:
|
||||||
part2(rows)
|
part2(rows)
|
||||||
print(706, 754)
|
|
||||||
print("🕒", elapsed())
|
print("🕒", elapsed())
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user