return max_ def get_row(self): '''Row-wise Binary Space Partitioning''' return self.iter_split(self.row_instructions) def get_seat(self): '''Column-wise Binary Space Partitioning''' return self.iter_split(self.seat_instructions, upper='R', lower='L') def get_seat_id(self): row = self.get_row() seat = self.get_seat() return (row * 8) + seat if __name__ == '__main__': boarding_passes = readfile('inputs/day5.txt') seat_ids = [] for p in boarding_passes: seat_ids.append(BoardingPass(p).get_seat_id()) seat_ids = sorted(seat_ids) aprint(1, seat_ids[-1]) seat_ids_lag = seat_ids[1:] seat_ids_lag.append(0) for seat, seatl in zip(seat_ids, seat_ids_lag): if seatl - seat == 2: aprint(2, seat + 1)
if p not in '0123456789': return False return True def strict_validate_passports(passports): '''Validate each field's values''' # Reduce passport space to those with all required fields passports = validate_fields(passports) strict_valid = [] for passport in passports: validator = [] validator.append(validate_pid(passport['pid'])) validator.append(validate_eye_color(passport['ecl'])) validator.append(validate_hair_color(passport['hcl'])) validator.append(validate_height(passport['hgt'])) validator.append(validate_between(passport['eyr'], 2020, 2030)) validator.append(validate_between(passport['iyr'], 2010, 2020)) validator.append(validate_between(passport['byr'], 1920, 2002)) if sum(validator) == 7: strict_valid.append(passport) return strict_valid if __name__ == '__main__': passports = read_passports_to_dict('inputs/day4.txt') aprint(1, len(validate_fields(passports))) aprint(1, len(strict_validate_passports(passports)))
def get_unique_answers(group): '''Return number of unique 'yes' answers for a group''' return len(set([l for l in group.replace(' ', '')])) def get_all_answers_yes(group): '''Return count of unanimous "yes" answers within group by member''' group_dict = {} members = group.split(' ') for member in members: for answer in member: if answer not in group_dict: group_dict[answer] = 1 else: group_dict[answer] += 1 count_all_yes = 0 for ans in group_dict: if group_dict[ans] == len(members): count_all_yes += 1 return count_all_yes if __name__ == '__main__': with open('inputs/day6.txt', 'r+') as f: text = [ t.replace('\n', ' ') for t in str(f.read()).split('\n\n') ] count, count_all_yes = 0, 0 for group in text: count += get_unique_answers(group) count_all_yes += get_all_answers_yes(group) aprint(1, count) aprint(2, count_all_yes)
def get_n_bags(bag, bags_dict, bag_of_bags=[], debug=True): '''Recursively step through a bag color to find number of nested bags''' if debug: print(f'Working on bag: {bag}') bag_of_bags.append(len(bags_dict[bag])) for ibag in bags_dict[bag]: if bags_dict[ibag]: get_n_bags(ibag, bags_dict, bag_of_bags=bag_of_bags, debug=debug) return sum(bag_of_bags) if __name__ == '__main__': DEBUG = False text = readfile('inputs/day7.txt') # Create dict: key: bag color, value: list of bags the bag color contains bags = {} for rule in text: k, v = parse_rule(rule) bags[k] = v count = 0 for bag in bags: if DEBUG: print(f'Current count: {count}, checking bag: {bag}') if get_children(bag, bags, 'shiny gold'): count += 1 aprint(1, count) aprint(2, get_n_bags('shiny gold', bags, debug=DEBUG))
elif instruction == 'jmp': i += instruction_value else: i += 1 return run_boot(instructions, run_instructions=run_instructions, accumulator=accumulator, i=i, end=end) if __name__ == '__main__': instructions = readfile('inputs/day8.txt', split=None) aprint(1, run_boot(instructions)) # Brute force solve the corruption for ind, c in enumerate(instructions): # Make a single change if a change operation present if 'jmp' in c or 'nop' in c: # Temporary copy of instructions to check corruption c_instructions = list(instructions) if 'jmp' in c: c_instructions[ind] = c.replace('jmp', 'nop') else: c_instructions[ind] = c.replace('nop', 'jmp') if run_boot(c_instructions,
if not validate_number(number, encoded[curr_index:preamble_len + curr_index]): if find_contiguous: return find_contiguous_range(number, encoded) else: return number def validate_number(number, priors): '''Ensure at least one pair of numbers within current preamble-based list sum to current number''' for i, p in enumerate(priors): for j, p2 in enumerate(priors): if i != j: if p + p2 == number: return True return False def find_contiguous_range(checksum, segment, min_len=2): '''Find the contiguous set of at least two numbers in the list''' for a in range(len(segment)): for z in range(len(segment)): if (sum(segment[a:z]) == checksum) and (z + 1 - a >= min_len): return min(segment[a:z]) + max(segment[a:z]) if __name__ == '__main__': encoded = readfile('inputs/day9.txt', split=None) encoded = [int(e) for e in encoded] aprint(1, xmas_decoder(encoded)) aprint(2, xmas_decoder(encoded, find_contiguous=True))
checks = 0 for i in [self.policy_first - 1, self.policy_second - 1]: if self._password[i] == self.alpha_instance: checks += 1 return checks == 1 @classmethod def _count_valid_from_file(cls, file): '''Returns the number of valid passwords under old policy''' pass_policies = readfile(file) c = 0 for p in pass_policies: if cls(p)._validate_password(): c += 1 return c @classmethod def count_valid_from_file(cls, file): '''Returns the number of valid passwords under current policy''' pass_policies = readfile(file) c = 0 for p in pass_policies: if cls(p).validate_password(): c += 1 return c if __name__ == '__main__': aprint(1, OTCP._count_valid_from_file('inputs/day2.txt')) aprint(2, OTCP.count_valid_from_file('inputs/day2.txt'))
trees_encountered = 0 for c in slope_coords: curr_x, curr_y = c # Adjust x values for repeating pattern to the right if curr_x >= width: curr_x = curr_x % width if curr_y >= height: return trees_encountered elif coords[curr_y][curr_x] == '#': trees_encountered += 1 return trees_encountered if __name__ == '__main__': coords = readfile('inputs/day3.txt') aprint(1, move_from_slope(coords, 3, 1)) trees = [] mult_trees = 1 for c in [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]: num_trees = move_from_slope(coords, c[0], c[1]) trees.append(num_trees) mult_trees *= num_trees aprint(2, mult_trees)
#!/usr/bin/python3 from helper_functions import readfile, aprint # Day 1: Report Repair -- AOC 2020 def find_two_entries(entries): for i in entries: for j in entries: if int(i) + int(j) == 2020: return int(i) * int(j) def find_three_entries(entries): for i in entries: for j in entries: for k in entries: if int(i) + int(j) + int(k) == 2020: return int(i) * int(j) * int(k) if __name__ == '__main__': entries = readfile('inputs/day1.txt') aprint(1, find_two_entries(entries)) aprint(2, find_three_entries(entries))