JakeTheSnake003

AOC2020 Day 2

Dec 2nd, 2020
400
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #P2:
  2. import collections
  3.  
  4. dic = collections.defaultdict(int)
  5. valid = 0
  6. entries = open("input2.txt", "r").read().split('\n')[:-1]
  7. for entry in entries:
  8.     password = entry.split(':')[1][1:]
  9.     limits = ((entry.split(':')[0]).split(' ')[0]).split('-')
  10.     policy_char = (entry.split(':')[0]).split(' ')[1]
  11.     if (password[int(limits[0])-1] == policy_char) ^ (password[int(limits[1])-1] == policy_char):
  12.         valid += 1
  13. print(valid)
  14.  
  15. #P1:
  16. import collections
  17. dic = collections.defaultdict(int)
  18. valid = 0
  19. entries = open("input2.txt", "r").read().split('\n')[:-1]
  20. for entry in entries:
  21.     password = entry.split(':')[1][1:]
  22.     limits = ((entry.split(':')[0]).split(' ')[0]).split('-')
  23.     policy_char = (entry.split(':')[0]).split(' ')[1]
  24.     policy_char_repeats = 0
  25.     for char in password:
  26.         if char == policy_char:
  27.             policy_char_repeats += 1
  28.     if int(limits[0]) <= policy_char_repeats <= int(limits[1]):
  29.         valid += 1
  30.  
  31. print(valid)
RAW Paste Data