solve day 2 part 1
This commit is contained in:
parent
40bc8ca23e
commit
8e4cf522d0
4 changed files with 1060 additions and 0 deletions
1000
day_02/input.txt
Normal file
1000
day_02/input.txt
Normal file
File diff suppressed because it is too large
Load diff
41
day_02/program.py
Executable file
41
day_02/program.py
Executable file
|
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# https://adventofcode.com/2024/day/2
|
||||
|
||||
def get_lines(filename: str) -> list:
|
||||
with open(filename, "r") as file:
|
||||
return [line.strip() for line in file.readlines()]
|
||||
|
||||
|
||||
def part_one(lines: list) -> int:
|
||||
safe_reports = 0
|
||||
for line in lines:
|
||||
reports = [int(x) for x in line.split(" ")]
|
||||
# print(reports)
|
||||
increasing = True
|
||||
decreasing = True
|
||||
safe = True
|
||||
for i in range(len(reports) - 1):
|
||||
if reports[i] >= reports[i + 1]:
|
||||
increasing = False
|
||||
if reports[i] <= reports[i + 1]:
|
||||
decreasing = False
|
||||
distance = abs(reports[i] - reports[i + 1])
|
||||
if distance < 1 or distance > 3:
|
||||
safe = False
|
||||
pass
|
||||
if safe and (increasing or decreasing):
|
||||
safe_reports += 1
|
||||
return safe_reports
|
||||
|
||||
|
||||
def main():
|
||||
# lines = get_lines("sample-input.txt")
|
||||
lines = get_lines("input.txt")
|
||||
|
||||
safe_reports = part_one(lines)
|
||||
print(f"Part 1: There are {safe_reports} safe reports.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
6
day_02/sample-input.txt
Normal file
6
day_02/sample-input.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
13
day_02/test_program.py
Executable file
13
day_02/test_program.py
Executable file
|
|
@ -0,0 +1,13 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import program
|
||||
import unittest
|
||||
|
||||
|
||||
class TestThing(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue