advent-of-code/2021-python/solutions/day_01.py
Anders Ytterström 9976edc457 Solve 2021:1 "Sonar sweep"
Felt a bit slow and rusty. Tooling was not set up properly on the
computer which decreased the flow.

Anyhow, fun first day! Could have been done more pythonic with list sequences, and readability would have increased with more use of sum() and lambdas. But this is not what Advent of Code is about.
2021-12-01 06:38:45 +01:00

31 lines
841 B
Python

from solutions import BaseSolution
class Solution(BaseSolution):
input_file = "01.txt"
def __str__(self):
return "Day 1: Sonar Sweep"
def parse_input(self, data):
return [int(n) for n in data.split()]
def solve(self, puzzle_input):
return sum(
[puzzle_input[i] > puzzle_input[i - 1] for i in range(1, len(puzzle_input))]
)
def solve_again(self, puzzle_input):
puzzle_input.append(max(puzzle_input) + 1)
return sum(
[
(puzzle_input[i] + puzzle_input[i - 1] + puzzle_input[i - 2])
> (puzzle_input[i - 1] + puzzle_input[i - 2] + puzzle_input[i - 3])
for i in range(2, len(puzzle_input) - 1)
]
)
if __name__ == "__main__":
solution = Solution()
solution.show_results()