My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2024/02

+22
+22
2024/day02.py
··· 1 + import fileinput 2 + 3 + def is_safe(levels): 4 + increasing = all((a < b) for a, b in zip(levels, levels[1:])) 5 + decreasing = all((a > b) for a, b in zip(levels, levels[1:])) 6 + good_deltas = all((abs(a - b) <= 3) for a, b in zip(levels, levels[1:])) 7 + 8 + return (increasing or decreasing) and good_deltas 9 + 10 + def gen_part_2(levels): 11 + yield levels 12 + for i in range(len(levels)): 13 + yield levels[:i] + levels[i+1:] 14 + 15 + 16 + reports = [] 17 + 18 + for line in fileinput.input(): 19 + reports.append([int(n) for n in line.split()]) 20 + 21 + print("Part 1:", sum(is_safe(r) for r in reports)) 22 + print("Part 2:", sum(any(is_safe(r) for r in gen_part_2(report)) for report in reports))