Created
December 3, 2024 05:42
-
-
Save object/9e418d3c127036496353c79a989fee9c to your computer and use it in GitHub Desktop.
Advent of Code 2024, day 02
This file contains hidden or 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
| with open("./data/input02.txt") as inputFile: | |
| input = inputFile.read().splitlines() | |
| reports = [] | |
| for line in input: | |
| report = list(map(lambda x: int(x), line.split(' '))) | |
| reports.append(report) | |
| def report_is_safe(report): | |
| safe = True | |
| asc = None | |
| prev = None | |
| for level in report: | |
| if prev != None: | |
| if level == prev: | |
| safe = False | |
| elif level > prev and level - prev >= 1 and level - prev <= 3: | |
| if asc == False: | |
| safe = False | |
| else: | |
| asc = True | |
| elif level < prev and prev - level >= 1 and prev - level <= 3: | |
| if asc == True: | |
| safe = False | |
| else: | |
| asc = False | |
| else: | |
| safe = False | |
| prev = level | |
| if not safe: | |
| break | |
| return safe | |
| # Part 1 | |
| res = 0 | |
| for report in reports: | |
| if report_is_safe(report): | |
| res += 1 | |
| print(res) | |
| # Part 2 | |
| res = 0 | |
| for report in reports: | |
| test_reports = [report] | |
| for i in range(len(report)): | |
| test_report = report.copy() | |
| del test_report[i] | |
| test_reports.append(test_report) | |
| safe = False | |
| for test_report in test_reports: | |
| if report_is_safe(test_report): | |
| safe = True | |
| break | |
| if safe: res += 1 | |
| print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment