Advent of Code 2021 | Day 1 | Solution in Python3
These are the solutions for both the parts in the Advent of Code 2021 Day 1.
Contents hide
Part 1 Solution
from math import inf def get_input(): data = [] with open("input.txt") as f: data = f.readlines() return [int(line) for line in data] def solve(depths): prev = inf count = 0 for depth in depths: if depth > prev: count += 1 prev = depth return count print(solve(get_input()))
Part 2 Solution
from math import inf def get_input(): data = [] with open("input.txt") as f: data = f.readlines() return [int(line) for line in data] def solve(depths): prev = inf count = 0 n = len(depths) for i in range(2, n): window = sum(depths[i - 2 : i + 1]) if window > prev: count += 1 prev = window return count print(solve(get_input()))
Recommended Posts
Gas Station | InterviewBit | Solution Explained
June 13, 2022
Furthest Building You Can Reach | LeetCode | Solution Explained
January 20, 2022
Burst Balloons | LeetCode | Solution Explained
January 1, 2022