-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
70 lines (64 loc) · 1.91 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
from sys import argv
import sys
def main():
if len(argv) != 2:
print("Usage: python program input.txt")
sys.exit(1)
elif ".txt" not in argv[1]:
print("Input must be a txt file")
sys.exit(1)
validpasswordsA = 0
validpasswordsB = 0
with open(argv[1]) as f:
inputs = f.readlines()
for line in inputs:
if isPasswordValidOldPolicy(line):
validpasswordsA += 1
if isPasswordValidNewPolicy(line):
validpasswordsB += 1
print(f'Answer A is: {validpasswordsA}')
print(f'Answer B is: {validpasswordsB}')
def isPasswordValidOldPolicy(input):
counter = 0
left = input[0:input.index(":")]
right = input[input.index(':')+2:]
if left[1].isdigit():
min = int(left[0] + left[1])
else:
min = int(left[0])
if left[left.index("-")+2].isdigit():
max = int(left[left.index("-")+1] + left[left.index("-")+2])
else:
max = int(left[left.index("-")+1])
value = left[left.index(" ")+1]
for c in right:
if c == value:
counter += 1
if counter in range(min, max+1):
return True
else:
return False
def isPasswordValidNewPolicy(input):
left = input[0:input.index(":")]
right = input[input.index(':')+2:]
if left[1].isdigit():
pos1 = int(left[0] + left[1])
else:
pos1 = int(left[0])
if left[left.index("-")+2].isdigit():
pos2 = int(left[left.index("-")+1] + left[left.index("-")+2])
else:
pos2 = int(left[left.index("-")+1])
value = left[left.index(" ")+1]
if pos2 > len(right):
if value is right[pos1-1]:
return True
elif value is right[pos1-1] and value is right[pos2-1]:
return False
elif value is right[pos1-1]:
return True
elif value is right[pos2-1]:
return True
else:
return False
main()