Advertisement
Armandur

Untitled

Dec 2nd, 2020
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. # 1-3 a: abcde
  2. # 1-3 b: cdefg
  3. # 2-9 c: ccccccccc
  4. # split line into three parts by space
  5. # split first part by - to get min max of required letter
  6. # slice second part by one to get the letter needed
  7. # count different letters in third part
  8.  
  9. db = []
  10.  
  11. with open("2 - input", 'r') as file:
  12.     lines = file.readlines()
  13.     for line in lines:
  14.         line_split = line.split(' ')
  15.         minmax_split = line_split[0].split("-")
  16.         line_dict = {"min": int(minmax_split[0]), "max": int(minmax_split[1]), "letter": line_split[1][0], "password": line_split[2].strip('\n')}
  17.         db.append(line_dict)
  18.  
  19. correct_count = 0
  20. for entry in db:
  21.  
  22.     count = 0
  23.     for letter in entry["password"]:
  24.         if letter == entry["letter"]:
  25.             count += 1
  26.     if entry["min"] <= count <= entry["max"]:
  27.         correct_count += 1
  28. # part 1
  29. print(correct_count)
  30.  
  31. # part 2
  32. # Given the same example list from above:
  33. #
  34. #     1-3 a: abcde is valid: position 1 contains a and position 3 does not.
  35. #     1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
  36. #     2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.
  37. #     Exactly one position must contain letter
  38.  
  39. correct_count = 0
  40. for entry in db:
  41.     ## Position one contains letter XOR position 2 contains letter
  42.     if ( ( entry["password"][entry["min"] - 1] == entry["letter"]) ) != ( (entry["password"][entry["max"] - 1] == entry["letter"] ) ):
  43.         correct_count += 1
  44.  
  45. print(correct_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement