Working out solutions for Advent of Code
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

04b.py 975B

5 år sedan
123456789101112131415161718192021222324252627282930313233
  1. # Python 3.7
  2. def main():
  3. rangeStart = 109165
  4. rangeEnd = 576723
  5. count = 0
  6. for pw in range(rangeStart, rangeEnd):
  7. count += validatePassword(pw)
  8. print(count)
  9. def validatePassword(pw):
  10. pwStr = str(pw)
  11. # does it have a doubled digit
  12. # but not a tripled etc. digit!
  13. # does it have a descending digit
  14. c, d, m = 0, 0, ""
  15. for i in range(1, len(pwStr)):
  16. if pwStr[i-1] == pwStr[i]:
  17. if c == 0 and m == "": # it has a doubled digit we haven't seen before
  18. c = 1
  19. m = pwStr[i]
  20. elif c == 1 and m == pwStr[i]: # it has a doubled digit we HAVE seen before
  21. c = 0 # so it doesn't count :(
  22. else: # this digit isn't the same as the previous, so reset the memo
  23. m = ""
  24. if int(pwStr[i]) < int(pwStr[i-1]):
  25. d = 1
  26. if c == 0 or d == 1:
  27. return 0
  28. return 1
  29. if __name__ == "__main__":
  30. main()