-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
86 lines (69 loc) · 2.05 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
with open("./2.txt", "r") as f:
f = f.read()
safe = 0
unsafe = 0
for line in f.splitlines():
firstNum = int(line.split()[0])
lastSeenNum = firstNum
lastGoing = ""
going = ""
currentStatus = ""
lastStatus = "safe"
for number in line.split()[1:]:
if abs(int(number) - lastSeenNum) in (1, 2, 3):
currentStatus = "safe"
else:
currentStatus = "unsafe"
if int(number) > lastSeenNum:
going = "up"
elif int(number) == lastSeenNum:
currentStatus = "unsafe"
else:
going = "down"
if going != lastGoing and lastGoing != "":
currentStatus = "unsafe"
if currentStatus != lastStatus:
currentStatus = "unsafe"
lastStatus = currentStatus
lastSeenNum = int(number)
lastGoing = going
if currentStatus == "unsafe":
break
#print(currentStatus)
if currentStatus == "safe":
safe += 1
else:
unsafe += 1
print("part 1:", safe)
safe = 0
for line in f.splitlines():
firstNum = int(line.split()[0])
lastSeenNum = firstNum
lastGoing = ""
going = ""
currentStatus = "safe"
lastStatus = "safe"
bad = 0
numbers = [int(x) for x in line.split()]
for number in line.split()[1:]:
if abs(int(number) - lastSeenNum) in (1, 2, 3):
currentStatus = "safe"
else:
currentStatus = "unsafe"
if int(number) > lastSeenNum:
going = "up"
elif int(number) == lastSeenNum:
currentStatus = "unsafe"
else:
going = "down"
if going != lastGoing and lastGoing != "":
currentStatus = "unsafe"
lastStatus = currentStatus
lastSeenNum = int(number)
lastGoing = going
if currentStatus == "unsafe":
bad += 1
currentStatus = "safe"
if bad == 0 or bad == 1:
safe += 1
print("part 2:", safe + 1) # this worked for me. I don't know if it would work for you.