input_file = sys.argv[1] def ft_input_parser(raw_input): '''Convert input into list of public keys (integers).''' return [int(line) for line in raw_input] def ft_get_encryption_key(public_key_1, public_key_2): '''Calculate encryption key of given public key pair.''' def get_loop_size(public_key): '''Find loop size of given public key.''' nbr = 1 loop_size = 0 while nbr != public_key: nbr = (nbr * 7) % 20201227 loop_size += 1 return loop_size return pow(public_key_1, get_loop_size(public_key_2), 20201227) def ft_part1(data): '''Get encryption key of handshake.''' return ft_get_encryption_key(*data) if __name__ == '__main__': ft_core(input_file, ft_input_parser, ft_part1, False)
return False return True def check_fields(dc_passport): dc_fields = {'hgt': check_height, \ 'hcl': check_hair_colour, \ 'ecl': check_eye_colour, \ 'pid': check_pid} for key in dc_fields: if dc_fields[key](dc_passport.get(key)) == False: return False return True if check_years(dc_passport) == False or \ check_fields(dc_passport) == False: return False return True def ft_part1(data): return len(validate_passports(data)) def ft_part2(data): ls_valid_passports = validate_passports(data) return len([dc_item for dc_item in ls_valid_passports if validate_fields(dc_item)]) if __name__ == '__main__': ft_core(input_file, ft_input_parser, ft_part1, ft_part2, delimiter="\n\n")
def check(pwd, ind_1, ind_2): ''' Check if given password is valid according to password policy. ''' count = pwd[STR].count(pwd[CHAR]) if ind_1 <= count <= ind_2: return True return False return len([pwd for pwd in data \ if check(pwd, int(pwd[IND_1]), int(pwd[IND_2]))]) def ft_part2(data): def count_checks(line, ind_1, ind_2): ''' Check if given password is valid according to password policy. ''' def check(line, ind): if ind < len(line[STR]): if line[CHAR] == line[STR][ind]: return 1 return 0 if (check(line, ind_1) + check(line, ind_2)) == 1: return True return False return len([pwd for pwd in data \ if count_checks(pwd, int(pwd[IND_1]) - 1, int(pwd[IND_2]) - 1)]) if __name__ == '__main__': ft_core(input_file, ft_input_parser, ft_part1, ft_part2)
# Import requirements import itertools as it import numpy as np def ft_input_parser(raw_input): ''' Convert input into list of numbers (integers). ''' return [int(line) for line in raw_input] def ft_find_nbrs(ls_nbrs, qty): ''' Find numbers in given list (ls_nbrs) whose sum is equal to 2020. ''' for nbrs in it.combinations(ls_nbrs, qty): if sum(nbrs) == 2020: return nbrs def ft_part1(data): ''' Get the product of the 2 numbers found. ''' return np.prod(ft_find_nbrs(data, 2)) def ft_part2(data): ''' Get the product of the 3 numbers found. ''' return np.prod(ft_find_nbrs(data, 3)) if __name__ == '__main__': ft_core(sys.argv[1], ft_input_parser, ft_part1, ft_part2)