Ejemplo n.º 1
0
    def test_load_bags(self):
        input_file = os.path.join(os.path.dirname(__file__), 'day7_input_test')
        data = read_input_raw(input_file)
        bags = day7.load_bags(data)
        self.assertEqual(len(bags.keys()), 9)

        self.assertTrue('shiny gold' in bags)
        shiny_gold_bag = bags['shiny gold']
        self.assertEqual(len(shiny_gold_bag.edges), 2)

        for edge in shiny_gold_bag.edges:
            if edge.node.name == "dark olive":
                self.assertEqual(edge.weight, 1)
                self.assertEqual(edge.node, bags['dark olive'])
                self.assertEqual(len(edge.node.edges), 2)
            else:
                self.assertTrue(edge.node.name == "vibrant plum")
                self.assertEqual(edge.weight, 2)
                self.assertEqual(edge.node, bags['vibrant plum'])
                self.assertEqual(len(edge.node.edges), 2)

        self.assertTrue('faded blue' in bags)
        faded_blue_bag = bags['faded blue']
        self.assertEqual(len(faded_blue_bag.edges), 0)
Ejemplo n.º 2
0
    for pwd in password_data:
        parts = re.search("(\d+)-(\d+) (\w): (\w+)", pwd)
        pos1 = int(parts[1]) - 1
        pos2 = int(parts[2]) - 1
        letter = parts[3]
        password = parts[4]
        if (password[pos1] == letter and password[pos2] != letter) or (
                password[pos1] != letter and password[pos2] == letter):
            correct_passwords = correct_passwords + 1
    return correct_passwords


def part1_solution(password_data):
    correct_passwords = 0
    for pwd in password_data:
        parts = re.search("(\d+)-(\d+) (\w): (\w+)", pwd)
        min_len = int(parts[1])
        max_len = int(parts[2])
        letter = parts[3]
        password = parts[4]
        occurances = count_occurances(letter, password)
        if occurances >= min_len and occurances <= max_len:
            correct_passwords = correct_passwords + 1
    return correct_passwords


if __name__ == '__main__':
    input_file = os.path.join(os.path.dirname(__file__), 'day2_input')
    #print(part1_solution(read_input_raw('day2_input')))
    print(part2_solution(read_input_raw(input_file)))
Ejemplo n.º 3
0
    computer = Computer()

    for i in range(len(instructions)):
        if instructions[i].instruction == "jmp":
            instructions[i].instruction = "nop"
        elif instructions[i].instruction == "nop":
            instructions[i].instruction = "jmp"
        else:
            continue

        computer.reset()
        if computer.execute(instructions):
            return computer.accumulator

        if instructions[i].instruction == "nop":
            instructions[i].instruction = "jmp"
        else:
            instructions[i].instruction = "nop"

    raise Exception("Didn't find infinite loop instruction")


if __name__ == '__main__':
    input_file = os.path.join(os.path.dirname(__file__), 'day8_input')
    data = read_input_raw(input_file)
    instructions = parse_instructions(data)

    print(f"Part 1: {part1(instructions)}")

    print(f"Part 2: {part2(instructions)}")
Ejemplo n.º 4
0
    for r1_instruction in route1:
        for _ in range(r1_instruction.steps, 0, -1):
            r1_current_point = step(r1_current_point, r1_instruction.direction)
            route1_points_visited.add(r1_current_point)

    r2_current_point = Point()
    for r2_instruction in route2:
        for _ in range(r2_instruction.steps, 0, -1):
            r2_current_point = step(r2_current_point, r2_instruction.direction)
            if r2_current_point in route1_points_visited:
                intersections.append(r2_current_point)

    return intersections


def manhattan_distance(point):
    return abs(point.x) + abs(point.y)


def shortest_manhattan_distance(points):
    return min(points, key=lambda p: abs(p.x) + abs(p.y))


if __name__ == '__main__':
    lines = read_input_raw('day3_input')
    route1 = lines[0]
    route2 = lines[1]
    d = manhattan_distance(
        shortest_manhattan_distance(find_intersections(route1, route2)))
    print(f"Shortest Manhattan Distance: {d}")
Ejemplo n.º 5
0
        if hgt_num < 59 or hgt_num > 76:
            print(f"invalid hgt: {hgt}")
            return False

    hcl = passport['hcl']
    if not re.fullmatch(r"#[0123456789abcdef]{6}", hcl):
        print(f"invalid hcl: {hcl}")
        return False

    ecl = passport['ecl']
    if not ecl in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
        print(f"invalid ecl: {ecl}")
        return False

    pid = passport['pid']
    if not re.fullmatch(r"[0123456789]{9}", pid):
        print(f"invalid pid: {pid}")
        return False

    return True


if __name__ == '__main__':
    input_file = os.path.join(os.path.dirname(__file__), 'day4_input')
    passports = load_passports(read_input_raw(input_file))
    # valid_passports = [passport for passport in passports if part1_valid(passport)]
    valid_passports = [
        passport for passport in passports if part2_valid(passport)
    ]
    print(len(valid_passports))
Ejemplo n.º 6
0
    seats = []
    for seat_chars in all_seat_chars:
        row = num_from_binary_chars(seat_chars[:7], 'B')
        col = num_from_binary_chars(seat_chars[7:10], 'R')
        seats.append((row, col))
    return seats


def seat_ids(seats):
    return [(seat[0] * 8 + seat[1]) for seat in seats]


def missing_seat_ids(seat_ids):
    m = []
    for row in range(0, 128):
        for col in range(0, 8):
            seat_id = row * 8 + col
            if seat_id not in seat_ids:
                m.append(seat_id)
                print(f"Row: {row}, Col: {col}, Seat ID: {seat_id}")
    return m


if __name__ == '__main__':
    input_file = os.path.join(os.path.dirname(__file__), 'day5_input')
    seats = load_seats(read_input_raw(input_file))
    # print(max(seat_ids(seats)))

    m = missing_seat_ids(seat_ids(seats))
    # picked the odd looking missing seat in the output, 659