Browse Source

Day 4

master
Noëlle Anthony 5 years ago
parent
commit
dcba549a45
2 changed files with 59 additions and 0 deletions
  1. 26
    0
      2019/04a.py
  2. 33
    0
      2019/04b.py

+ 26
- 0
2019/04a.py View File

@@ -0,0 +1,26 @@
# Python 3.7

def main():
rangeStart = 109165
rangeEnd = 576723
count = 0
for pw in range(rangeStart, rangeEnd):
count += validatePassword(pw)
print(count)

def validatePassword(pw):
pwStr = str(pw)
# does it have a doubled digit
# does it have a descending digit
c, d = 0, 0
for i in range(1, len(pwStr)):
if pwStr[i-1] == pwStr[i]:
c = 1
if int(pwStr[i]) < int(pwStr[i-1]):
d = 1
if c == 0 or d == 1:
return 0
return 1

if __name__ == "__main__":
main()

+ 33
- 0
2019/04b.py View File

@@ -0,0 +1,33 @@
# Python 3.7

def main():
rangeStart = 109165
rangeEnd = 576723
count = 0
for pw in range(rangeStart, rangeEnd):
count += validatePassword(pw)
print(count)

def validatePassword(pw):
pwStr = str(pw)
# does it have a doubled digit
# but not a tripled etc. digit!
# does it have a descending digit
c, d, m = 0, 0, ""
for i in range(1, len(pwStr)):
if pwStr[i-1] == pwStr[i]:
if c == 0 and m == "": # it has a doubled digit we haven't seen before
c = 1
m = pwStr[i]
elif c == 1 and m == pwStr[i]: # it has a doubled digit we HAVE seen before
c = 0 # so it doesn't count :(
else: # this digit isn't the same as the previous, so reset the memo
m = ""
if int(pwStr[i]) < int(pwStr[i-1]):
d = 1
if c == 0 or d == 1:
return 0
return 1

if __name__ == "__main__":
main()

Loading…
Cancel
Save