aboutsummaryrefslogtreecommitdiff
path: root/2024/puzzle-2.py
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-12-04 02:43:12 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-12-04 02:43:12 +0000
commit61aa2c1ded3b2d38243432968d8e72e29322f820 (patch)
tree9f1fffbdc4a49646534d880dd79b5d6702ea6b40 /2024/puzzle-2.py
parente253bc500686b7d9adec1686d20c75714cf2ddf0 (diff)
downloadadvent-of-code-61aa2c1ded3b2d38243432968d8e72e29322f820.tar.gz
advent-of-code-61aa2c1ded3b2d38243432968d8e72e29322f820.tar.bz2
advent-of-code-61aa2c1ded3b2d38243432968d8e72e29322f820.zip
Optimise solutions a bit for 2024HEADmaster
Diffstat (limited to '2024/puzzle-2.py')
-rw-r--r--2024/puzzle-2.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/2024/puzzle-2.py b/2024/puzzle-2.py
index 98f2efd..ee5bf77 100644
--- a/2024/puzzle-2.py
+++ b/2024/puzzle-2.py
@@ -22,21 +22,21 @@ print(f"Round 1: {len(good_levels)}")
def check_two_levels(x, y, decreasing):
diff = abs(x - y)
- return not ((decreasing and x < y) or (not decreasing and x > y) or not (diff <= 3 and diff >= 1))
+ return not ((decreasing and x < y)\
+ or (not decreasing and x > y) \
+ or not (diff <= 3 and diff >= 1))
def is_good_level_2(level):
# 1) Is decreasing
# 2) Sliding window of two cells (x, y) => 1 <= |x-y| <= 3
# 3) Can remove any one item to make it safe
- ret = is_good_level_1(level)
- if not ret:
- # Consider slices of the level and check if they're good
- slices = [level[:i] + level[i + 1:] for i in range(len(level))]
- for s in slices:
- if is_good_level_1(s):
- return True
- else:
+ if is_good_level_1(level):
return True
+ # Consider slices of the level and check if they're good
+ slices = [level[:i] + level[i + 1:] for i in range(len(level))]
+ for s in slices:
+ if is_good_level_1(s):
+ return True
return False
good_levels = [level for level in levels if is_good_level_2(level)]