-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
95 lines (71 loc) · 1.55 KB
/
day02.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
88
89
90
91
92
93
94
95
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
reports = []
with open(inputfile,"r") as input:
for line in input:
linelist = line.rstrip().split(" ")
reports.append([int(x) for x in linelist])
safe = []
def run_part1():
incs = []
decs = []
for report in reports:
copy = report.copy()
copy.sort()
if report == copy:
incs.append(report)
copy.sort(reverse=True)
if report == copy:
decs.append(report)
for report in incs:
ok = check(report)
if ok:
safe.append(report)
for report in decs:
ok = check(report)
if ok:
safe.append(report)
# print(incs)
# print(decs)
# print(safe)
print(len(safe))
def run_part2():
safe2 = []
for report in reports:
if report in safe:
safe2.append(report)
elif check2(report):
safe2.append(report)
#print(safe2)
print(len(safe2))
def check(report):
prev = None
for num in report:
if prev == None:
prev = num
else:
diff = abs(num - prev)
if diff > 0 and diff < 4:
prev = num
else:
return 0
return 1
def check2(report):
for i in range(len(report)):
dropped = report.copy()
dropped.pop(i)
copy = dropped.copy()
copy.sort()
if dropped == copy and check(dropped):
return 1
else:
copy.sort(reverse=True)
if dropped == copy and check(dropped):
return 1
return 0
run_part1()
run_part2()