-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
73 lines (57 loc) · 2 KB
/
day2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import time
def parse_input(filename: str) -> list[str]:
with open(filename) as f:
lines = f.readlines()
return lines
def check_report(report):
for i, _ in enumerate(report[:-1]):
if report[i+1] > report[i] + 3:
return False
return True
def part_one(strings: list[str]) -> None:
safe = 0
for line in strings:
report = list(map(int, line.split(' ')))
increasing = all([report[i+1] > report[i] for i, _ in enumerate(report[:-1])])
if increasing:
if check_report(report):
safe += 1
decreasing = all([report[i+1] < report[i] for i, _ in enumerate(report[:-1])])
if decreasing:
if check_report(report[::-1]):
safe += 1
print(safe)
def part_two(strings: list[str]) -> None:
safe = 0
for line in strings:
report = list(map(int, line.split(' ')))
reports = []
for i in range(len(report)):
cur_report = report[:i] + report[i+1:]
reports.append(cur_report)
for report in reports:
increasing = all([report[i+1] > report[i] for i, _ in enumerate(report[:-1])])
if increasing:
cur_safe = check_report(report)
if cur_safe:
safe += 1
break
decreasing = all([report[i+1] < report[i] for i, _ in enumerate(report[:-1])])
if decreasing:
cur_safe = check_report(report[::-1])
if cur_safe:
safe += 1
break
print(safe)
def main(input_filename: str):
inp = parse_input(input_filename)
start_part_one = time.time()
part_one(inp)
start_part_two = time.time()
part_two(inp)
end_time = time.time()
print(f"Part one took {start_part_two - start_part_one:.2f} seconds")
print(f"Part two took {end_time - start_part_two:.2f} seconds")
if __name__ == "__main__":
main("input.txt")
# main("sample.txt")