day 2
aoc 2020 » Devlog
2a.py
# Each line gives the password policy and then the password. # The password policy indicates the lowest and highest number of times a given letter # must appear for the password to be valid. # For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times. # input with open('2.txt', 'r') as file: input = file.read() # turn the input into a list; each entry is '1-3 a: abcde' input_list = list(input.split('\n')) # break it up into tuples password_rules = [] for str in input_list: tmp_tuple = str.split(' ') # ('1-3', 'a:', 'abcde') range = tmp_tuple[0].split('-') # ('1', '3') letter = tmp_tuple[1][:-1] # 'a' (removes last character) password_rules.append((int(range[0]), int(range[1]), letter, tmp_tuple[2])) # (1, 3, 'a', 'abcde') # check each string against the rules good_pw = [] # these pass the check bad_pw = [] # these fail the check for tuple in password_rules: min = tuple[0] max = tuple[1] letter = tuple[2] pw_temp = password = tuple[3] is_good = False let_count = 0 while len(pw_temp) > 0: # while the string is nonempty pw_let = pw_temp[0] # first character if pw_let == letter: # if the pw_let is the letter you're checking for let_count += 1 pw_temp = pw_temp[1:] # removes first character from string if let_count >= min and let_count <= max: # it satisfies the conditions good_pw.append(password) # add to good password list else: # it does not satisfy the conditions bad_pw.append(password) # add to bad password list # the passwords are in good_pw and bad_pw lists print(len(good_pw))
2b.py
# Each line gives the password policy and then the password. # Each policy actually describes two positions in the password, where 1 means the first character, # 2 means the second character, and so on. # (Be careful; Toboggan Corporate Policies have no concept of "index zero"!) # Exactly one of these positions must contain the given letter. # Other occurrences of the letter are irrelevant for the purposes of policy enforcement. # For example, 1-3 a means that the password must contain a at position 1 (0) xor at position 3 (0). # input with open('2.txt', 'r') as file: input = file.read() # turn the input into a list; each entry is '1-3 a: abcde' input_list = list(input.split('\n')) # break it up into tuples password_rules = [] for str in input_list: tmp_tuple = str.split(' ') # ('1-3', 'a:', 'abcde') range = tmp_tuple[0].split('-') # ('1', '3') letter = tmp_tuple[1][:-1] # 'a' (removes last character) password_rules.append((int(range[0]), int(range[1]), letter, tmp_tuple[2])) # (1, 3, 'a', 'abcde') # check each string against the rules good_pw = [] # these pass the check bad_pw = [] # these fail the check for tuple in password_rules: i = tuple[0] j = tuple[1] letter = tuple[2] password = tuple[3] letij = 0 # how many times the letter appears in position i or j if password[i-1] == letter: letij += 1 if password[j-1] == letter: letij += 1 if letij == 1: # if the letter appears in i xor j ((i and ¬j) or (¬i and j)) good_pw.append(password) # it is a good password else: # the letter is in neither or both positions bad_pw.append(password) # it is a bad password # the passwords are in good_pw and bad_pw lists print(len(good_pw))
overall idea for this was to format the list into tuples so that i could easily refer to the variables (min/max number of letters, the letter i was checking for, the password itself) - for the first challenge i just took letters off the string one by one, counted it if it was the right letter, and then added the password to the good list if this count was in the right range, and for the second i.. never bothered looking up if i could use xor in python, so i just counted the number of times that letter appeared if i took just the i^th and j^th characters and added it to the good list if this was exactly 1.
Files
2.txt 21 kB
Dec 04, 2020
2a.py 1.6 kB
Dec 04, 2020
2b.py 1.7 kB
Dec 04, 2020
Get aoc 2020
aoc 2020
not a mod, just some code
Status | Released |
Category | Other |
Author | riv |
Tags | advent-of-code, advent-of-code-2020, python |
Leave a comment
Log in with itch.io to leave a comment.