Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1-3 a: abcde
- # 1-3 b: cdefg
- # 2-9 c: ccccccccc
- # split line into three parts by space
- # split first part by - to get min max of required letter
- # slice second part by one to get the letter needed
- # count different letters in third part
- db = []
- with open("2 - input", 'r') as file:
- lines = file.readlines()
- for line in lines:
- line_split = line.split(' ')
- minmax_split = line_split[0].split("-")
- line_dict = {"min": int(minmax_split[0]), "max": int(minmax_split[1]), "letter": line_split[1][0], "password": line_split[2].strip('\n')}
- db.append(line_dict)
- correct_count = 0
- for entry in db:
- count = 0
- for letter in entry["password"]:
- if letter == entry["letter"]:
- count += 1
- if entry["min"] <= count <= entry["max"]:
- correct_count += 1
- # part 1
- print(correct_count)
- # part 2
- # Given the same example list from above:
- #
- # 1-3 a: abcde is valid: position 1 contains a and position 3 does not.
- # 1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
- # 2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.
- # Exactly one position must contain letter
- correct_count = 0
- for entry in db:
- ## Position one contains letter XOR position 2 contains letter
- if ( ( entry["password"][entry["min"] - 1] == entry["letter"]) ) != ( (entry["password"][entry["max"] - 1] == entry["letter"] ) ):
- correct_count += 1
- print(correct_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement