-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
81 lines (68 loc) · 2.4 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
input_file = 'input/day2.txt'
# Read in all the data and strip out any whitespace at the end of lines
all_lines = [line.rstrip('\n') for line in open(input_file)]
print(all_lines)
# Safety Criteria
# - The levels are either all increasing or all decreasing.
# - Any two adjacent levels differ by at least one and at most three.
def all_positive(levels):
safe = True
i=0
for i in range(len(levels)-1):
next_level = int(levels[i+1])
this_level = int(levels[i])
difference = next_level - this_level
# print(f'{next_level} - {this_level} = {difference}')
if difference <= 0 or difference > 3:
safe = False
break
return safe
def all_negative(levels):
safe = True
i=0
for i in range(len(levels)-1):
next_level = int(levels[i+1])
this_level = int(levels[i])
difference = this_level - next_level
# print(f'{this_level} - {next_level} = {difference}')
if difference <= 0 or difference > 3:
safe = False
break
return safe
safe_count = 0
line_count = 0
unsafe_reports = []
for line in all_lines:
levels=line.split()
# report = [int(level) for level in rep.split()]
# print(levels)
all_positive_check = all_positive(levels)
all_negative_check = all_negative(levels)
# print(f'Check all rising? {all_positive_check}')
# print(f'Check all falling? {all_negative_check}')
if all_positive_check is True or all_negative_check is True:
print(f'Report is safe!')
safe_count += 1
else:
print(f'Report is unsafe!')
unsafe_reports.append(line_count)
line_count += 1
print(f'Part 1: {safe_count} safe reports')
# Part 2
print(f'Starting part 2')
# Just check the reports recorded as unsafe in part 1
# Try removing an element and then checking pass criteria
for report in unsafe_reports:
level=all_lines[report].split()
for i in range(len(level)):
new_level = level[:]
new_level.pop(i)
all_positive_check = all_positive(new_level)
all_negative_check = all_negative(new_level)
# print(f'Check all rising? {all_positive_check}')
# print(f'Check all falling? {all_negative_check}')
if all_positive_check is True or all_negative_check is True:
print(f'Report is now safe!')
safe_count += 1
break
print(f'Part 2: {safe_count} safe reports')