You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day2a.py 752B

1234567891011121314151617181920212223242526272829
  1. import re
  2. def checkpw(a, b, l, pw):
  3. try:
  4. c = pw[a-1]
  5. d = pw[b-1]
  6. if c == l or d == l:
  7. if c == l and d == l:
  8. return False
  9. return True
  10. return False
  11. except IndexError as e:
  12. print(a, b, l, pw)
  13. print(e)
  14. def main():
  15. patt = re.compile("^(\d+)-(\d+) (.): (.+)$")
  16. with open("input2.txt") as file:
  17. lines = file.readlines()
  18. ln = []
  19. for line in lines:
  20. m = re.match(patt, line)
  21. ln.append([int(m.group(1)), int(m.group(2)), m.group(3), m.group(4)])
  22. valid = [pw for pw in ln if checkpw(pw[0], pw[1], pw[2], pw[3])]
  23. print(f"There are {len(valid)} valid passwords out of {len(ln)}.")
  24. if __name__ == "__main__":
  25. main()