advent-of-code/2024/python/day02.py
2024-12-03 21:03:33 -05:00

50 lines
1.1 KiB
Python

import matrix
import shared
import itertools
import functools
def is_increasing(sequence):
return all(sequence[i] < sequence[i+1] for i in range(len(sequence) - 1))
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
for previous, current in zip(row, row[1:]):
diff = abs(current - previous)
if diff >= 1 and diff <= 3:
continue
return 0
return 1
# @shared.profile
def part1(rows):
rows = [list(map(int, row.split())) for row in rows]
safe = 0
for row in rows:
safe += parse_line(row)
print(safe)
# @shared.profile
def part2(rows):
pass
def main():
rows = [row for row in shared.load_rows(2)]
with shared.elapsed_timer() as elapsed:
part1(rows)
print("🕒", elapsed())
rows = [row for row in shared.load_rows(2, True)]
with shared.elapsed_timer() as elapsed:
part2(rows)
print("🕒", elapsed())
if __name__ == "__main__":
main()