diff options
-rw-r--r-- | aoc/2024/02/part-one.py | 6 | ||||
-rw-r--r-- | aoc/2024/02/part-two.py | 21 |
2 files changed, 27 insertions, 0 deletions
diff --git a/aoc/2024/02/part-one.py b/aoc/2024/02/part-one.py new file mode 100644 index 0000000..e0d74db --- /dev/null +++ b/aoc/2024/02/part-one.py @@ -0,0 +1,6 @@ +from sys import stdin + +print(sum(all(0 < d < 4 for d in diffs) or all(-4 < d < 0 for d in diffs) + for diffs in (tuple(map(int.__sub__, levels, levels[1:])) + for levels in (tuple(map(int, line)) + for line in map(str.split, stdin))))) diff --git a/aoc/2024/02/part-two.py b/aoc/2024/02/part-two.py new file mode 100644 index 0000000..ad5f5a6 --- /dev/null +++ b/aoc/2024/02/part-two.py @@ -0,0 +1,21 @@ +from collections import Counter +from itertools import chain, count +from sys import stdin + + +def safe(levels, dampened=False): + head = levels[0 if 0 < levels[1] - levels[0] < 4 else 1] - 1, + tail = levels[-1 if 0 < levels[-1] - levels[-2] < 4 else -2] + 1, + dampenable = tuple((not dampened and 0 < a - c < 4 + and safe(levels[:i]+levels[i+1:], True)) + for i, a, b, c in zip(count(), + chain(levels[1:], tail), + levels, + chain(head, levels)) + if not (0 < a - b < 4 and 0 < b - c < 4)) + return not dampenable or any(dampenable) + + +print(sum(safe(levels) or safe(tuple(map(int.__neg__, levels))) + for levels in (tuple(map(int, line)) + for line in map(str.split, stdin)))) |