def get(self): p = Page() p2 = Page2() info = input() info.__make = self.request.GET['make'] info.model = self.request.GET['model'] info.year = self.request.GET['year'] info.body_style = self.request.GET['bds'] info.vin = self.request.GET['vin'] info.miles = self.request.GET['miles']
def get_seat_ids() -> list: seat_ids = [] for boarding_pass in input(): seat_info = decode_boarding_pass(boarding_pass) seat_ids.append(seat_info[2]) seat_ids.sort() return seat_ids
def get_passports() -> list: # Iterate over input file and read passports (each passport is spread across an arbitrary number of consecutive lines) # Return passports as lists of attributes fields = [] for line in input(): if line == '': # Line is empty, return fields for current passport and reset field list for next passport yield fields fields = [] else: # Line has fields, split them (by space) and add them to field list for current passport fields += line.split() # Return last passport (no new line at end of input) yield fields
def find_double_sum(target_sum): # Find 2 numbers in the input that sum to `target_sum` # For each number in the input, calculate the theoretical matching number # and check if it is in the input, tracked through the `seen_nums` dictionary # Tracking dictionary seen_nums = {} # Read ints from input for num in input(is_ints=True): needed_number = target_sum - num if needed_number in seen_nums: # Matching number found in dictionary return [num, needed_number] else: # Add number to dictionary so it can be found if its match is later in the input seen_nums[num] = True # No matches found in input return []
def find_triple_sum(target_sum): # Find 3 numbers in the input that sum to `target_sum` # Sort the input list, then iterate over the list. At each iteration, initialize # two pointers at each end of the remaining range (one at the number after the # current number, and one at the end of the list). Move the pointers towards each # other, comparing the sum at each step. Once the pointers meet, move on to the # next overall iteration. # Load input numbers into a sorted array nums = list(input(is_ints=True)) nums.sort() # Overall iteration for pointer_main in range(len(nums) - 2): pointer_left = pointer_main + 1 pointer_right = len(nums) - 1 # Stay on the current step of the overall iteration until the left and right pointers meet while pointer_left < pointer_right: test_sum = nums[pointer_main] + nums[pointer_left] + nums[ pointer_right] if test_sum == target_sum: # Match found return [ nums[pointer_main], nums[pointer_left], nums[pointer_right] ] elif test_sum > target_sum: # Too large, move right pointer to the previous (smaller) number pointer_right -= 1 elif test_sum < target_sum: # Too small, move left pointer to the next (larger) number pointer_left += 1 # No matches found in input return []
# Part 2 # Count trees at 5 different angles, and multiply the result angles = [ # (right, down) (1, 1), (3, 1), # Angle for part 1 (5, 1), (7, 1), (1, 2) ] # Initialize results array results = [0] * len(angles) for line_index, line in enumerate(input()): for angle_index, angle in enumerate(angles): if (line_index % angle[1] == 0 # Only consider every nth line, indicated by the "down" component of the angle and # On each line, the current position is the "real" line index (found by dividing the line index by the "down" component of the angle) # times the "right" component of the angle. Modulo by the length of the line to simulate the infinite repetitions to the right. # Trees are indicated by '#' line[int(line_index / angle[1] * angle[0]) % len(line) ] == '#'): results[angle_index] += 1 print('Part 1 trees:', results[1]) print('Part 2 trees for each angle:', results)
def check_policy_part_2(indicator: tuple, test_letter: str, password: str): # Return True if `test_letter` appears in `password` in exactly one of the two positions indicated by `indicator` (1-indexed) position_matches = sum(1 for position in range(2) if password[indicator[position] - 1] == test_letter) return position_matches == 1 def parse_line(input: str): # Extract `indicator`, `test_letter`, and `password` from a line # `indicator` is always a pair of numbers, the usage of which is different in parts 1 and 2 # Example: # Raw: 1-3 a: abcde # indicator: (1, 3) # test_letter: 'a' # password: '******' m = re.match(r'(\d+)-(\d+) ([a-z]): ([a-z]+)', input) return { 'password': m.group(4), 'test_letter': m.group(3), 'indicator': tuple(map(int, m.group(1, 2))) } print('Part 1 matches:', sum(1 for line in input() if check_policy_part_1(**parse_line(line)))) print('Part 2 matches:', sum(1 for line in input() if check_policy_part_2(**parse_line(line))))