-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo.py
More file actions
88 lines (78 loc) · 1.91 KB
/
two.py
File metadata and controls
88 lines (78 loc) · 1.91 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
eg = """
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
"""
class Report:
levels: list = []
safe: bool = False
def __repr__(self):
return f"safe: {self.safe}, levels: {self.levels}"
def safe(levels):
ascending = True
for i in range(0, len(levels)-1):
a = levels[i]
b = levels[i+1]
if i == 0:
ascending = a < b
if (a > b and ascending) or (a < b and not ascending):
return False
gap = abs(a - b)
if gap > 3 or gap < 1:
return False
return True
def one(source):
reports = []
num_safe = 0
for line in source.splitlines():
levels = line.split()
if len(levels) == 0:
continue
report = Report()
report.levels = [int(l) for l in levels]
report.safe = safe(report.levels)
reports.append(report)
if report.safe:
num_safe += 1
print(f"total safe: {num_safe}")
print("example")
one(eg)
print("---")
print("one")
with open("two.txt", "r") as file:
one(file.read())
def two(source):
reports = []
num_safe = 0
for line in source.splitlines():
levels = line.split()
if len(levels) == 0:
continue
report = Report()
report.levels = [int(l) for l in levels]
if safe(report.levels):
num_safe += 1
report.safe = True
reports.append(report)
#print(report)
continue
for i in range(0, len(report.levels)):
levels = report.levels.copy()
del levels[i]
if safe(levels):
num_safe += 1
report.safe = True
break
reports.append(report)
#print(report)
print(f"total safe: {num_safe}")
print("---")
print("example")
two(eg)
print("---")
print("two")
with open("two.txt", "r") as file:
two(file.read())