-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
87 lines (71 loc) · 1.97 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Part One
# fhand = open('day2sample.txt')
fhand = open('day2.txt')
reports = list()
for line in fhand:
levels = line.split()
levels = [int(x) for x in levels]
reports.append(levels)
monotonic = bool()
gradual = bool()
SafeCount = 0
for levels in reports:
if levels == sorted(levels):
monotonic = True
elif levels == sorted(levels, reverse=True):
monotonic = True
else:
monotonic = False
gradual = True
for i in range(len(levels)):
if i == 0: continue
if abs(levels[i] - levels[i-1]) < 1:
gradual = False
if abs(levels[i] - levels[i-1]) > 3:
gradual = False
if monotonic and gradual:
SafeCount += 1
# print(levels, "Monotonic:", monotonic, "Gradual", gradual)
# input()
print(SafeCount, "Safe Reports")
# Part Two
# fhand = open('day2sample.txt')
fhand = open('day2.txt')
reports = list()
for line in fhand:
levels = line.split()
levels = [int(x) for x in levels]
reports.append(levels)
def SafetyCheck(levels):
monotonic = bool()
gradual = bool()
if levels == sorted(levels):
monotonic = True
elif levels == sorted(levels, reverse=True):
monotonic = True
else:
monotonic = False
gradual = True
for i in range(len(levels)):
if i == 0: continue
if abs(levels[i] - levels[i-1]) < 1:
gradual = False
if abs(levels[i] - levels[i-1]) > 3:
gradual = False
if monotonic and gradual:
return(True)
else: return(False)
SafeCount = 0
for levels in reports:
# Check the normal way
if SafetyCheck(levels):
SafeCount += 1
continue
# Iterate through all subsets of levels that exclude one entry
for index, level in enumerate(levels):
levelsSubset = list(levels)
del levelsSubset[index]
if SafetyCheck(levelsSubset):
SafeCount += 1
break
print(SafeCount, "Safe Reports with Dampener")