-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.py
More file actions
65 lines (44 loc) · 1.24 KB
/
day02.py
File metadata and controls
65 lines (44 loc) · 1.24 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
"""Day 2: Red-Nosed Reports"""
from aocd import get_data
data = get_data(day=2, year=2024).splitlines()
list_of_data = []
for line in data:
line = line.split()
for i in range(len(line)):
line[i] = int(line[i])
list_of_data.append(line)
def safe_report(line):
sorted_line = line.copy()
sorted_line.sort()
reverse_line = line.copy()
reverse_line.sort(reverse=True)
line_length = len(line)
unique_items = len(set(line))
if (line == sorted_line or line == reverse_line) and line_length == unique_items:
safe = True
for i in range(len(sorted_line) - 1):
diff = int(sorted_line[i + 1]) - int(sorted_line[i])
if diff > 3:
safe = False
if safe:
return True
return False
# PART 1
safe_reports = 0
for line in list_of_data:
if safe_report(line):
safe_reports += 1
print(safe_reports)
# PART 2
safe_reports = 0
for line in list_of_data:
if safe_report(line):
safe_reports += 1
else:
for i in range(len(line)):
new_line = line.copy()
new_line.pop(i)
if safe_report(new_line):
safe_reports += 1
break
print(safe_reports)