-
-
Save yozhsh/4330f9d0bb2a0fda9f6ec474dfa25dfa to your computer and use it in GitHub Desktop.
day2_part1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools as it | |
def is_ascending(A): | |
for i in range(len(A) - 1): | |
if A[i] - A[i + 1] > 0: | |
return False | |
return True | |
def is_descending(A): | |
for i in range(len(A) - 1): | |
if A[i] - A[i+1] < 0: | |
return False | |
return True | |
def pairwise(seq): | |
"""s -> (s0,s1), (s1,s2), (s2,s3), ...""" | |
a, b = it.tee(seq) | |
next(b, None) | |
return zip(a, b) | |
def substract(left_op, right_op): | |
return left_op - right_op | |
def plus(left_op, right_op): | |
return left_op + right_op | |
def check_unsafe_order(lst): | |
# 0 - UNSAFE | |
# 1 - SAFE | |
p = list(pairwise(lst)) | |
for x in p: | |
if x[0] - x[1] == 0: | |
return 0 | |
if is_ascending(lst): | |
return 1 | |
if is_descending(lst): | |
return 1 | |
else: | |
# print(lst) | |
return 0 | |
def is_safe(lst): | |
# true or false | |
if check_unsafe_order(lst) == 0: | |
return False | |
pairs = pairwise(lst) | |
for p in pairs: | |
if abs(substract(p[0], p[1])) <= 3 and abs(substract(p[0], p[1])) >= 1: | |
continue | |
else: | |
return False | |
return True | |
def safe_report_count(): | |
safest_result = [] | |
f = open('examplereport.txt', 'r') | |
for l in f.readlines(): | |
level_line = [int(x) for x in l.split()] | |
if is_safe(level_line): | |
safest_result.append(True) | |
return len(safest_result)) | |
print(safe_report_count()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment