From 23ff5e1a6cd78601f24e50fbf21406bb8179d2af Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Mon, 2 Dec 2024 16:37:54 +0900 Subject: [aoc/2024] Finish day 2 --- aoc/2024/02/part-two.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 aoc/2024/02/part-two.py (limited to 'aoc/2024/02/part-two.py') 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)))) -- cgit 1.4.1