示例#1
0
def part_2():
    slopes = [{
        "right": 1,
        "down": 1
    }, {
        "right": 3,
        "down": 1
    }, {
        "right": 5,
        "down": 1
    }, {
        "right": 7,
        "down": 1
    }, {
        "right": 1,
        "down": 2
    }]
    return np.prod(
        [get_number_of_trees(FileUtils.input(), slope) for slope in slopes])
示例#2
0
    for rule in rules:
        bag_name, contents = rule.split(" bags contain ")
        bag_rules[bag_name] = []
        if "no" in contents: continue
        inner_bags = contents.split(", ")
        for bag in inner_bags:
            num, attribute, color, *_ = bag.split(" ")
            bag_rules[bag_name] += (["%s %s" % (attribute, color)] * int(num))
    return bag_rules


def get_fittable_colors(bag_rules, my_bag="shiny gold"):
    color_list = set()
    for bag, contents in bag_rules.items():
        if my_bag in contents:
            color_list.add(bag)
            color_list.update(get_fittable_colors(bag_rules, my_bag=bag))
    return color_list


def get_bag_count_for(bag_rules, my_bag="shiny gold"):
    bag_count = 0
    for bag in bag_rules[my_bag]:
        bag_count += 1 + get_bag_count_for(bag_rules, bag)
    return bag_count


if __name__ == "__main__":
    bag_rules = parse_bag_rules(FileUtils.input())
    print(len(get_fittable_colors(bag_rules)))
    print(get_bag_count_for(bag_rules))
示例#3
0
    def run_iteration(self, n_times=1, max_occupied=4):
        for _ in range(n_times):
            new_layout = self.layout[:]  # Deep copy
            for ix, iy in np.ndindex(self.shape['x'], self.shape['y']):
                seat = self.layout[iy][ix]
                if (seat == 'L') & ('#' not in self.get_adjacent_seats(ix,
                                                                       iy)):
                    new_layout[iy] = new_layout[iy][:ix] + '#' + new_layout[
                        iy][ix + 1:]
                elif (seat == '#') & (self.get_adjacent_seats(
                        ix, iy).count('#') >= max_occupied):
                    new_layout[iy] = new_layout[iy][:ix] + 'L' + new_layout[
                        iy][ix + 1:]
            self.layout = new_layout
        return self.layout

    def stabilize_layout(self, max_occupied=4):
        layouts = [None, self.layout]
        while layouts[-1] != layouts[-2]:
            layouts.append(self.run_iteration(max_occupied=max_occupied))

    def __str__(self):
        return '\n'.join(self.layout)


if __name__ == "__main__":
    ferry = Ferry(FileUtils.input())
    ferry.stabilize_layout(max_occupied=5)
    print(ferry.get_num_of_occupied())
    print(ferry)
示例#4
0
    return numerical_valid and height_valid and hair_color_valid and eye_color_valid

def check_passport_validity(passport, check_values=False):
    if not check_values:
        return check_passport_fields(passport)
    return check_passport_values(passport)

def parse_passports(input):
    documents, document = [], {}
    for line in input:
        if line == "":
            documents.append(document)
            document = {}
            continue
        data_pairs = line.split(" ")
        for data_pair in data_pairs:
            field, value = data_pair.split(":")
            document[field] = value
    documents.append(document)
    return documents

def part_1(passports):
    return sum([check_passport_validity(passport) for passport in passports])

def part_2(passports):
    return sum([check_passport_validity(passport, True) for passport in passports])

if __name__ == "__main__":
    passports = parse_passports(FileUtils.input())
    print( part_1(passports) )
    print( part_2(passports) )
示例#5
0
def part_1():
    return get_number_of_trees(FileUtils.input(), {"right": 3, "down": 1})
示例#6
0
    def jmp(self, arg):
        return int(arg)

    def run(self):
        while True:
            if self.index in self.memory:
                return -1
            elif self.index == len(self.instructions):
                return 0
            self.memory.append(self.index)
            op, arg = self.instructions[self.index].split(" ")
            self.index += self.get_operation_func(op)(arg) or 1

    def fix_code(self, op1='jmp', op2='nop'):
        op_index = 0
        while self.run() != 0:
            self.clear_memory()
            swap_index, op = [(i, op) for i, op in enumerate(self.instructions)
                              if op1 in op or op2 in op][op_index]
            self.instructions[swap_index] = self.instructions[
                swap_index].replace(
                    op.split(" ")[0], op1 if op2 in op else op2)
            op_index += 1


if __name__ == "__main__":
    hhc = HandHeldConsole(FileUtils.input())
    hhc.run()
    print(hhc.accumulator)
    hhc.fix_code()
    print(hhc.accumulator)
示例#7
0
        "L": (rows, get_inner_interval(columns)),
        "R": (rows, get_inner_interval(columns, lower=False)),
    }
    if len(boarding_pass) > 0:
        arguments = switcher.get(boarding_pass[0])
        return get_seat(boarding_pass[1:], *arguments)
    # At this point lower and upper is the same
    return (rows[0], columns[0])


def get_seat_id(seat):
    return seat[0] * 8 + seat[1]


def get_my_seat_id(seat_list):
    missing_seats = []
    for row in range(128):
        for column in range(8):
            if (row, column) not in seat_list:
                missing_seats.append((row, column))
    seat_list_ids = [get_seat_id(seat) for seat in seat_list]
    for seat in missing_seats:
        seat_id = get_seat_id(seat)
        if seat_id + 1 in seat_list_ids and seat_id - 1 in seat_list_ids:
            return seat_id


if __name__ == "__main__":
    seats = [get_seat(boarding_pass) for boarding_pass in FileUtils.input()]
    print("Part_1:", max([get_seat_id(seat) for seat in seats]))
    print("Part_2:", get_my_seat_id(seats))
示例#8
0
def part_2():
    return get_number_of_valid(FileUtils.input(), is_valid_index)
示例#9
0
def part_1():
    return get_number_of_valid(FileUtils.input(), is_valid)
示例#10
0
def get_number_of_yes(groups, everyone=False):
    answers = ([reduce(lambda a1, a2: set(a1) & set(a2), group) for group in groups]) if everyone \
        else ([reduce(lambda a1, a2: a1 + a2, group) for group in groups])
    return [len(Counter(questions).keys()) for questions in answers]


def parse_answers(answers):
    groups, group = [], []
    for line in answers:
        if line == "":
            groups.append(group)
            group = []
            continue
        group.append(line)
    groups.append(group)
    return groups


def part_1(answer_groups):
    return sum(get_number_of_yes(answer_groups))


def part_2(answer_groups):
    return sum(get_number_of_yes(answer_groups, everyone=True))


if __name__ == "__main__":
    answer_groups = parse_answers(FileUtils.input())
    print(part_1(answer_groups))
    print(part_2(answer_groups))