diff options
author | Nguyễn Gia Phong <cnx@loang.net> | 2024-12-02 16:37:54 +0900 |
---|---|---|
committer | Nguyễn Gia Phong <cnx@loang.net> | 2024-12-02 16:46:24 +0900 |
commit | 23ff5e1a6cd78601f24e50fbf21406bb8179d2af (patch) | |
tree | e4f4b9053b0d2797fb25791c3bf57e0dabe6105d | |
parent | ec0ad80483918127691ad127ef5bfb4191ee6774 (diff) | |
download | cp-23ff5e1a6cd78601f24e50fbf21406bb8179d2af.tar.gz |
[aoc/2024] Finish day 2
-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)))) |