Exemplo n.º 1
0
 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
Exemplo n.º 2
0
        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)
Exemplo n.º 3
0

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)
Exemplo n.º 4
0
        accumulator += instruction_value
        i += 1
    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')
Exemplo n.º 5
0
        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))
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
#!/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))