diff --git a/q2.py b/q2.py new file mode 100644 index 0000000..df6944b --- /dev/null +++ b/q2.py @@ -0,0 +1,49 @@ +def q1(text: str): + levels = text.splitlines() + + count = 0 + for level in levels: + curr = [int(l) for l in level.split()] + + increasing = all(curr[i] < curr[i + 1] for i in range(len(curr) - 1)) + decreasing = all(curr[i] > curr[i + 1] for i in range(len(curr) - 1)) + diff_criterion = all( + 1 <= abs(curr[i] - curr[i + 1]) <= 3 for i in range(len(curr) - 1) + ) + + count += 1 if diff_criterion and (increasing or decreasing) else 0 + + return count + + +def q2(text: str): + levels = text.splitlines() + + count = 0 + for level in levels: + curr = [int(l) for l in level.split()] + + for i in range(len(curr) + 1): + curr_i = curr.copy() + if i < len(curr): + del curr_i[i] + + increasing = all(curr_i[i] < curr_i[i + 1] for i in range(len(curr_i) - 1)) + decreasing = all(curr_i[i] > curr_i[i + 1] for i in range(len(curr_i) - 1)) + diff_criterion = all( + 1 <= abs(curr_i[i] - curr_i[i + 1]) <= 3 for i in range(len(curr_i) - 1) + ) + + if diff_criterion and (increasing or decreasing): + count += 1 + break + + return count + + +if __name__ == "__main__": + with open("i2.txt") as f: + text = f.read() + + print(q1(text)) + print(q2(text))