Day 2 - Advent of Code 2024
Working solutions for the day 2 puzzles.
Part One
""" day_02_01.py """
# usage: python3 day_02_01.py < input
import sys
def values(text):
""" report values """
return list(map(int, text.split()))
def safe(nums):
""" are report values safe by puzzle description """
diffs = [i - j for i, j in zip(nums, nums[1:])]
if 0 in diffs:
return False
if max(map(abs, diffs)) > 3:
return False
if abs(sum(1 if d > 0 else -1 for d in diffs)) < len(diffs):
return False
return True
with sys.stdin as infile:
print(sum(safe(values(report)) for report in infile))
Part Two
""" day_02_02.py """
# usage: python3 day_02_02.py < input
import sys
def values(text):
""" report values """
return list(map(int, text.split()))
def safe(nums):
""" are report values safe by puzzle description """
diffs = [i - j for i, j in zip(nums, nums[1:])]
if 0 in diffs:
return False
if max(map(abs, diffs)) > 3:
return False
if abs(sum(1 if d > 0 else -1 for d in diffs)) < len(diffs):
return False
return True
def dampener_safe(nums):
""" a dampened report drops one value """
return any(safe(nums[0:i] + nums[i + 1:]) for i in range(len(nums)))
with sys.stdin as infile:
print(sum(dampener_safe(values(report)) for report in infile))