コード例 #1
0
def function(input):
    instructions = functions.get_data_from_string(input)
    accumulator = 0
    length = len(instructions)
    i = 0
    pos = []

    if length < 1:
        return accumulator

    while i < length:
        if i in pos:
            break

        pos.append(i)
        [operation,
         argument] = functions.get_data_from_string(instructions[i], ' ')
        argument_parts = re.match('(\+|-)([0-9]+)', argument, re.I).groups()
        [operator, number] = argument_parts

        if operation == 'acc':
            if operator == '+':
                accumulator += int(number)
            elif operator == '-':
                accumulator -= int(number)
        elif operation == 'jmp':
            if operator == '+':
                i += int(number)
            elif operator == '-':
                i -= int(number)

        if operation != 'jmp':
            i += 1

    return accumulator
コード例 #2
0
def function(input):
    data = functions.get_data_from_string(input)

    if not data:
        return 0

    [arrive_timestamp, bus_lines_string] = data
    bus_lines = functions.get_data_from_string(bus_lines_string, ',')
    selected_bus_line = 0
    wait_minutes = 0

    for bus_line in bus_lines:
        if bus_line == 'x':
            continue

        depart_time = int(bus_line)
        previous_depart_minutes = int(arrive_timestamp) % depart_time
        last_depart = int(arrive_timestamp) - previous_depart_minutes

        next_depart = last_depart + depart_time
        possible_wait_minutes = next_depart - int(arrive_timestamp)

        if selected_bus_line == 0 or possible_wait_minutes < wait_minutes:
            selected_bus_line = depart_time
            wait_minutes = possible_wait_minutes

    return selected_bus_line * wait_minutes
コード例 #3
0
def function(input):
    group_answers = functions.get_data_from_string(input, '\n\n')
    yes_answers = []
    sum = 0

    for group_answer in group_answers:
        answers = functions.get_data_from_string(group_answer)
        yes = 0
        questions = []

        for i, answer in enumerate(answers):
            letters = functions.split_string(answer)

            if i == 0:
                yes = len(letters)

                for letter in letters:
                    questions.append(letter)

                continue

            for letter in letters:
                if letter not in questions:
                    questions.append(letter)
                    yes += 1

        yes_answers.append(yes)

    for yes_answer in yes_answers:
        sum += yes_answer

    return sum
コード例 #4
0
def function(input):
    instructions = functions.get_data_from_string(input)
    accumulator = 0
    length = len(instructions)
    i = 0
    pos = []
    jmps = []
    nops = []
    jumped = False
    noped = False

    if length < 1:
        return accumulator

    while i < length:
        if i in pos:
            accumulator = 0
            i = 0
            pos = []
            noped = False
            jumped = False
            continue

        pos.append(i)
        [operation,
         argument] = functions.get_data_from_string(instructions[i], ' ')
        old_operation = operation
        argument_parts = re.match('(\+|-)([0-9]+)', argument, re.I).groups()
        [operator, number] = argument_parts

        if operation == 'acc':
            if operator == '+':
                accumulator += int(number)
            elif operator == '-':
                accumulator -= int(number)

        if operation == 'nop':
            if i not in nops and not noped and not jumped:
                operation = 'jmp'
                noped = True
                nops.append(i)

        if operation == 'jmp':
            if i not in jmps and not jumped and not noped and old_operation != 'nop':
                jmps.append(i)
                i += 1
                jumped = True
            else:
                if operator == '+':
                    i += int(number)
                elif operator == '-':
                    i -= int(number)

        if operation != 'jmp':
            i += 1

    return accumulator
コード例 #5
0
def function(input):
    map = functions.get_data_from_string(input)
    length = 0
    trees = 0
    pos = 1

    for i in range(len(map)):
        if length == 0:
            length = len(map[i])

        if length < pos:
            pos -= length

        if i == 0:
            pos += 3
            continue

        map_elements = functions.split_string(map[i])

        map_element = map_elements[pos - 1]

        if map_element == '#':
            trees += 1

        pos += 3

    return trees
コード例 #6
0
def function(input, invalid_number):
    numbers = functions.get_data_from_string(input)
    length = len(numbers)
    i = 0
    smallest_number = 0
    largest_number = 0

    while i < length:
        sum = int(numbers[i])
        smallest_number = int(numbers[i])
        j = i + 1

        while j < length:
            sum += int(numbers[j])

            if sum > invalid_number:
                break

            if int(numbers[j]) > largest_number:
                largest_number = int(numbers[j])

            if int(numbers[j]) < smallest_number:
                smallest_number = int(numbers[j])

            if sum == invalid_number:
                break

            j += 1

        if sum == invalid_number:
            break

        i += 1

    return smallest_number + largest_number
コード例 #7
0
def function(input, preamble=25):
    numbers = functions.get_data_from_string(input)
    first_wrong_property = None
    length = len(numbers)
    i = preamble
    sum = 0

    while i < length:
        number = int(numbers[i])
        j = i - preamble
        l = i - 1

        while j < l:
            k = j + 1

            while k < i:
                sum = int(numbers[j]) + int(numbers[k])

                if sum == number:
                    break

                k += 1

            if sum == number:
                break

            j += 1

        if sum != number:
            first_wrong_property = number
            break

        i += 1

    return first_wrong_property
コード例 #8
0
def function(input, higher_than=0):
    data = functions.get_data_from_string(input)

    if not data:
        return 0

    bus_lines = functions.get_data_from_string(data[1], ',')
    length = len(bus_lines)
    i = 1
    add = 0

    if higher_than != 0:
        previous_minutes = higher_than % int(bus_lines[0])
        previous_departure = higher_than - previous_minutes
        timestamp = previous_departure + int(bus_lines[0])
    else:
        timestamp = int(bus_lines[0])

    while i < length:
        add += 1

        if bus_lines[i] == 'x':
            i += 1
            continue

        last_timestamp = timestamp + (length - 1)

        if last_timestamp % int(bus_lines[length-1]) != 0:
            i = 1
            add = 0
            timestamp += int(bus_lines[0])
            continue

        next_timestamp = timestamp + add

        if next_timestamp % int(bus_lines[i]) == 0:
            i += 1
        else:
            i = 1
            add = 0
            timestamp += int(bus_lines[0])

    return timestamp
コード例 #9
0
def function(input):
    jolts = list(
        map(int, sorted(functions.get_data_from_string(input), key=int)))
    possibilities = collections.defaultdict(int)
    possibilities[0] = 1

    for number in jolts:
        possibilities[number] = possibilities[number - 1] + possibilities[
            number - 2] + possibilities[number - 3]

    return possibilities[max(jolts)]
コード例 #10
0
def function(input):
    passports = functions.get_data_from_string(input, '\n\n')
    valid = 0
    required_fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid', 'cid']

    if len(passports) <= 1:
        return 0

    for passport in passports:
        lines = functions.get_data_from_string(passport)
        key_value_pair_strings = []
        key_value_pairs = []
        keys = []
        invalid = False

        for line in lines:
            data = functions.get_data_from_string(line, ' ')

            for string in data:
                key_value_pair_strings.append(string)

        for key_value_pair_string in key_value_pair_strings:
            key_value_pairs.append(
                functions.get_data_from_string(key_value_pair_string, ':'))

        for key_value_pair in key_value_pairs:
            [key, value] = key_value_pair
            keys.append(key)

        for required_field in required_fields:
            if required_field not in keys:
                if required_field == 'cid':
                    continue
                else:
                    invalid = True
                    break

        if not invalid:
            valid += 1

    return valid
コード例 #11
0
def get_seat_map(input):
    rows = functions.get_data_from_string(input)
    seat_map = dict()

    for i, row in enumerate(rows):
        seat_map[i] = dict()
        seats = functions.split_string(row)

        for j, seat in enumerate(seats):
            seat_map[i][j] = seat

    return seat_map
コード例 #12
0
def function(input):
    group_answers = functions.get_data_from_string(input, '\n\n')
    yes_answers = []
    sum = 0

    for group_answer in group_answers:
        answers = functions.get_data_from_string(group_answer)
        member = len(answers)
        yes = 0
        questions = {}

        for i, answer in enumerate(answers):
            letters = functions.split_string(answer)

            if i == 0:
                for letter in letters:
                    if letter not in questions:
                        questions[letter] = 1

                continue

            for letter in letters:
                if letter not in questions:
                    questions[letter] = 1
                else:
                    questions[letter] += 1

        for char in questions:
            num = questions[char]
            if num == member:
                yes += 1

        yes_answers.append(yes)

    for yes_answer in yes_answers:
        sum += yes_answer

    return sum
コード例 #13
0
def get_content(content_description):
    contents = []
    content = {}

    if 'no other bags' in content_description:
        return None

    if ', ' in content_description:
        contents = functions.get_data_from_string(content_description, ', ')

    if len(contents) > 1:
        for content_string in contents:
            content_parts = functions.get_data_from_string(content_string, ' ')
            color = ' '.join([content_parts[1], content_parts[2]])

            content[color] = int(content_parts[0])
    else:
        content_parts = functions.get_data_from_string(content_description, ' ')
        color = ' '.join([content_parts[1], content_parts[2]])

        content[color] = int(content_parts[0])

    return content
コード例 #14
0
def function(input):
    bags = 0
    my_bag = 'shiny gold'
    rules = functions.get_data_from_string(input)
    bag_rules = {}

    for rule in rules:
        [bag_color,
         content_description] = day7.day_functions.get_rule_parts(rule)
        content = day7.day_functions.get_content(content_description)
        bag_rules[bag_color] = content

    bags = day7.day_functions.get_bags(my_bag, bag_rules)

    return bags
コード例 #15
0
def function(input):
    seats = functions.get_data_from_string(input)
    seat_ids = []
    missing_seat_id = 0

    for seat in seats:
        seat_desc = functions.split_string(seat)
        lowest_row = 0
        highest_row = 127
        lowest_col = 0
        highest_col = 7
        rows = 128
        cols = 8
        row = 0
        col = 0

        for direction in seat_desc:
            if direction == 'F':
                rows /= 2
                highest_row -= rows
            elif direction == 'B':
                rows /= 2
                lowest_row += rows
            elif direction == 'R':
                cols /= 2
                lowest_col += cols
            elif direction == 'L':
                cols /= 2
                highest_col -= cols

        if highest_col == lowest_col:
            col = highest_col

        if highest_row == lowest_row:
            row = highest_row

        seat_id = row * 8 + col
        seat_ids.append(int(seat_id))

    sorted_seat_ids = sorted(seat_ids)

    for i in range(sorted_seat_ids[0], sorted_seat_ids[-1] + 1):
        if i not in sorted_seat_ids:
            missing_seat_id = i
            break

    return missing_seat_id
コード例 #16
0
def function(input):
    list_entries = functions.get_data_from_string(input)
    valid_passwords = 0

    for entry in list_entries:
        parts = entry.split(' ')
        [pos1, pos2] = parts[0].split('-')
        letter = parts[1].strip(':')
        password_letters = functions.split_string(parts[2])

        if (password_letters[int(pos1) - 1] == letter
                and password_letters[int(pos2) - 1] != letter) or (
                    password_letters[int(pos1) - 1] != letter
                    and password_letters[int(pos2) - 1] == letter):
            valid_passwords += 1

    return valid_passwords
コード例 #17
0
def function(input):
    jolts = list(map(int, sorted(functions.get_data_from_string(input), key=int)))
    one_jolt_diff = 1
    three_jolt_diff = 1

    for i, jolt in enumerate(jolts):
        if (i + 1) >= len(jolts):
            break

        diff = jolts[i+1] - jolt

        if diff == 1:
            one_jolt_diff += 1
        elif diff == 3:
            three_jolt_diff += 1
        elif diff > 3:
            break

    return one_jolt_diff * three_jolt_diff
コード例 #18
0
def function(input):
    seats = functions.get_data_from_string(input)
    highest_seat_id = 0

    for seat in seats:
        seat_desc = functions.split_string(seat)
        lowest_row = 0
        highest_row = 127
        lowest_col = 0
        highest_col = 7
        rows = 128
        cols = 8
        row = 0
        col = 0

        for direction in seat_desc:
            if direction == 'F':
                rows /= 2
                highest_row -= rows
            elif direction == 'B':
                rows /= 2
                lowest_row += rows
            elif direction == 'R':
                cols /= 2
                lowest_col += cols
            elif direction == 'L':
                cols /= 2
                highest_col -= cols

        if highest_col == lowest_col:
            col = highest_col

        if highest_row == lowest_row:
            row = highest_row

        seat_id = row * 8 + col

        if seat_id > highest_seat_id:
            highest_seat_id = int(seat_id)

    return highest_seat_id
コード例 #19
0
def function(input):
    list_entries = functions.get_data_from_string(input)
    valid_passwords = 0

    for entry in list_entries:
        parts = entry.split(' ')
        [min, max] = parts[0].split('-')
        letter = parts[1].strip(':')
        password_letters = functions.split_string(parts[2])
        letters_count = 0

        for password_letter in password_letters:
            if password_letter != letter:
                continue

            letters_count += 1
            if letters_count > int(max):
                break

        if int(max) >= letters_count >= int(min):
            valid_passwords += 1

    return valid_passwords
コード例 #20
0
def function(input):
    my_bag = 'shiny gold'
    rules = functions.get_data_from_string(input)
    valid_bags = []

    for rule in rules:
        bag_color = day7.day_functions.get_bag_color(rule)

        if my_bag == bag_color:
            continue

        if my_bag in rule:
            valid_bags.append(bag_color)

    for valid_bag in valid_bags:
        for rule in rules:
            bag_color = day7.day_functions.get_bag_color(rule)

            if valid_bag in rule:
                if bag_color not in valid_bags:
                    valid_bags.append(bag_color)

    return len(valid_bags)
コード例 #21
0
def function(input):
    instructions = functions.get_data_from_string(input)
    north = 0
    east = 0
    south = 0
    west = 0
    direction_1 = 'east'
    direction_2 = ''

    for instruction in instructions:
        [direction, steps] = re.match('([NSEWLRF])([0-9]+)', instruction,
                                      re.I).groups()
        steps = int(steps)

        if direction == 'F':
            if direction_1 == 'north':
                (north, south) = day12.day_functions.update_two_directions(
                    north, south, steps)
            elif direction_1 == 'east':
                (east, west) = day12.day_functions.update_two_directions(
                    east, west, steps)
            elif direction_1 == 'south':
                (south, north) = day12.day_functions.update_two_directions(
                    south, north, steps)
            elif direction_1 == 'west':
                (west, east) = day12.day_functions.update_two_directions(
                    west, east, steps)
        elif direction == 'N':
            if direction_1 == 'north':
                (north, south) = day12.day_functions.update_two_directions(
                    north, south, steps)
            elif direction_1 == 'south':
                if south != 0:
                    if south - steps < 0:
                        steps -= south
                        south = 0
                        north += steps
                        direction_1 = 'north'
                    else:
                        south -= steps
                else:
                    north += steps
            elif direction_2 == '':
                direction_2 = 'north'
                north += steps
            else:
                if south != 0:
                    if south - steps < 0:
                        steps -= south
                        south = 0
                        north += steps
                        direction_2 = 'north'
                    else:
                        south -= steps
                else:
                    north += steps
                    direction_2 = 'north'
        elif direction == 'E':
            if direction_1 == 'east':
                (east, west) = day12.day_functions.update_two_directions(
                    east, west, steps)
            elif direction_1 == 'west':
                if west != 0:
                    if west - steps < 0:
                        steps -= west
                        west = 0
                        east += steps
                        direction_1 = 'east'
                    else:
                        west -= steps
                else:
                    east += steps
            elif direction_2 == '':
                direction_2 = 'east'
                east += steps
            else:
                if west != 0:
                    if west - steps < 0:
                        steps -= west
                        west = 0
                        east += steps
                        direction_2 = 'east'
                    else:
                        west -= steps
                else:
                    east += steps
                    direction_2 = 'east'
        elif direction == 'S':
            if direction_1 == 'south':
                (south, north) = day12.day_functions.update_two_directions(
                    south, north, steps)
            elif direction_1 == 'north':
                if north != 0:
                    if north - steps < 0:
                        steps -= north
                        north = 0
                        south += steps
                        direction_1 = 'south'
                    else:
                        north -= steps
                else:
                    south += steps
            elif direction_2 == '':
                direction_2 = 'south'
                south += steps
            else:
                if north != 0:
                    if north - steps < 0:
                        steps -= north
                        north = 0
                        south += steps
                        direction_2 = 'south'
                    else:
                        north -= steps
                else:
                    south += steps
                    direction_2 = 'south'
        elif direction == 'W':
            if direction_1 == 'west':
                (west, east) = day12.day_functions.update_two_directions(
                    west, east, steps)
            elif direction_1 == 'east':
                if east != 0:
                    if east - steps < 0:
                        steps -= east
                        east = 0
                        west += steps
                        direction_1 = 'west'
                    else:
                        east -= steps
                else:
                    west += steps
            elif direction_2 == '':
                direction_2 = 'west'
                west += steps
            else:
                if east != 0:
                    if east - steps < 0:
                        steps -= east
                        east = 0
                        west += steps
                        direction_2 = 'west'
                    else:
                        east -= steps
                else:
                    west += steps
                    direction_2 = 'west'
        elif direction == 'R':
            if direction_1 == 'north':
                if steps == 90:
                    direction_1 = 'east'
                elif steps == 180:
                    direction_1 = 'south'
                elif steps == 270:
                    direction_1 = 'west'
            elif direction_1 == 'east':
                if steps == 90:
                    direction_1 = 'south'
                elif steps == 180:
                    direction_1 = 'west'
                elif steps == 270:
                    direction_1 = 'north'
            elif direction_1 == 'south':
                if steps == 90:
                    direction_1 = 'west'
                elif steps == 180:
                    direction_1 = 'north'
                elif steps == 270:
                    direction_1 = 'east'
            elif direction_1 == 'west':
                if steps == 90:
                    direction_1 = 'north'
                elif steps == 180:
                    direction_1 = 'east'
                elif steps == 270:
                    direction_1 = 'south'
        elif direction == 'L':
            if direction_1 == 'north':
                if steps == 90:
                    direction_1 = 'west'
                elif steps == 180:
                    direction_1 = 'south'
                elif steps == 270:
                    direction_1 = 'east'
            elif direction_1 == 'east':
                if steps == 90:
                    direction_1 = 'north'
                elif steps == 180:
                    direction_1 = 'west'
                elif steps == 270:
                    direction_1 = 'south'
            elif direction_1 == 'south':
                if steps == 90:
                    direction_1 = 'east'
                elif steps == 180:
                    direction_1 = 'north'
                elif steps == 270:
                    direction_1 = 'west'
            elif direction_1 == 'west':
                if steps == 90:
                    direction_1 = 'south'
                elif steps == 180:
                    direction_1 = 'east'
                elif steps == 270:
                    direction_1 = 'north'

    return north + east + south + west
コード例 #22
0
def function(input):
    passports = functions.get_data_from_string(input, '\n\n')
    valid = 0
    required_fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid', 'cid']
    eye_colors = ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']

    if len(passports) <= 1:
        return 0

    for passport in passports:
        lines = functions.get_data_from_string(passport)
        key_value_pair_strings = []
        key_value_pairs = []
        keys = []
        values = []
        invalid = False

        for line in lines:
            data = functions.get_data_from_string(line, ' ')

            for string in data:
                key_value_pair_strings.append(string)

        for key_value_pair_string in key_value_pair_strings:
            key_value_pairs.append(
                functions.get_data_from_string(key_value_pair_string, ':'))

        for key_value_pair in key_value_pairs:
            [key, value] = key_value_pair
            keys.append(key)
            values.append(value)

        for required_field in required_fields:
            if required_field not in keys:
                if required_field == 'cid':
                    continue
                else:
                    invalid = True
                    break

            pos = keys.index(required_field)
            value = values[pos]

            if required_field == 'byr':
                if len(value) < 4:
                    invalid = True
                    break
                elif int(value) < 1920 or int(value) > 2002:
                    invalid = True
                    break
            elif required_field == 'iyr':
                if len(value) < 4:
                    invalid = True
                    break
                elif int(value) < 2010 or int(value) > 2020:
                    invalid = True
                    break
            elif required_field == 'eyr':
                if len(value) < 4:
                    invalid = True
                    break
                elif int(value) < 2020 or int(value) > 2030:
                    invalid = True
                    break
            elif required_field == 'hgt':
                match = re.match('([0-9]+)([a-z]+)', value, re.I)

                if not match:
                    invalid = True
                    break
                elif match[2] == 'cm' and (int(match[1]) < 150
                                           or int(match[1]) > 193):
                    invalid = True
                    break
                elif match[2] == 'in' and (int(match[1]) < 59
                                           or int(match[1]) > 76):
                    invalid = True
                    break
            elif required_field == 'hcl':
                match = re.match('#[0-9a-f]{6}', value, re.I)

                if not match:
                    invalid = True
                    break
            elif required_field == 'ecl':
                if value not in eye_colors:
                    invalid = True
                    break
            elif required_field == 'pid':
                if len(value) > 9 or len(value) < 9:
                    invalid = True
                    break

        if not invalid:
            valid += 1

    return valid
コード例 #23
0
def function(input):
    instructions = functions.get_data_from_string(input)
    waypoint_north = 1
    waypoint_east = 10
    waypoint_south = 0
    waypoint_west = 0
    north = 0
    east = 0
    south = 0
    west = 0

    for instruction in instructions:
        [direction, steps] = re.match('([NSEWLRF])([0-9]+)', instruction,
                                      re.I).groups()
        steps = int(steps)

        if direction == 'F':
            if waypoint_north != 0:
                (north, south) = day12.day_functions.update_two_directions(
                    north, south, waypoint_north * steps)
            if waypoint_east != 0:
                (east, west) = day12.day_functions.update_two_directions(
                    east, west, waypoint_east * steps)
            if waypoint_south != 0:
                (south, north) = day12.day_functions.update_two_directions(
                    south, north, waypoint_south * steps)
            if waypoint_west != 0:
                (west, east) = day12.day_functions.update_two_directions(
                    west, east, waypoint_west * steps)
        elif direction == 'N':
            if waypoint_south != 0:
                if waypoint_south - steps < 0:
                    steps -= waypoint_south
                    waypoint_south = 0
                    waypoint_north += steps
                else:
                    waypoint_south -= steps
            else:
                waypoint_north += steps
        elif direction == 'E':
            if waypoint_west != 0:
                if waypoint_west - steps < 0:
                    steps -= waypoint_west
                    waypoint_west = 0
                    waypoint_east += steps
                else:
                    waypoint_west -= steps
            else:
                waypoint_east += steps
        elif direction == 'S':
            if waypoint_north != 0:
                if waypoint_north - steps < 0:
                    steps -= waypoint_north
                    waypoint_north = 0
                    waypoint_south += steps
                else:
                    waypoint_north -= steps
            else:
                waypoint_south += steps
        elif direction == 'W':
            if waypoint_east != 0:
                if waypoint_east - steps < 0:
                    steps -= waypoint_east
                    waypoint_east = 0
                    waypoint_west += steps
                else:
                    waypoint_east -= steps
            else:
                waypoint_west += steps
        elif direction == 'R':
            tmp_north = waypoint_north
            tmp_east = waypoint_east
            tmp_south = waypoint_south
            tmp_west = waypoint_west

            if steps == 90:
                waypoint_north = tmp_west
                waypoint_east = tmp_north
                waypoint_south = tmp_east
                waypoint_west = tmp_south
            elif steps == 180:
                waypoint_north = tmp_south
                waypoint_east = tmp_west
                waypoint_south = tmp_north
                waypoint_west = tmp_east
            elif steps == 270:
                waypoint_north = tmp_east
                waypoint_east = tmp_south
                waypoint_south = tmp_west
                waypoint_west = tmp_north
        elif direction == 'L':
            tmp_north = waypoint_north
            tmp_east = waypoint_east
            tmp_south = waypoint_south
            tmp_west = waypoint_west

            if steps == 90:
                waypoint_north = tmp_east
                waypoint_east = tmp_south
                waypoint_south = tmp_west
                waypoint_west = tmp_north
            elif steps == 180:
                waypoint_north = tmp_south
                waypoint_east = tmp_west
                waypoint_south = tmp_north
                waypoint_west = tmp_east
            elif steps == 270:
                waypoint_north = tmp_west
                waypoint_east = tmp_north
                waypoint_south = tmp_east
                waypoint_west = tmp_south

    return north + east + south + west
コード例 #24
0
def function(input):
    map = functions.get_data_from_string(input)
    slopes = [
        [
            1,
            1
        ],
        [
            3,
            1
        ],
        [
            5,
            1
        ],
        [
            7,
            1
        ],
        [
            1,
            2
        ]
    ]
    length = 0
    trees_per_slope = []

    for slope in slopes:
        [right, down] = slope
        trees = 0
        pos = 1
        downwards = 0

        for i in range(len(map)):
            if length == 0:
                length = len(map[i])

            if length < pos:
                pos -= length

            if i == 0:
                pos += right
                downwards += 1
                continue

            if downwards < down:
                downwards += 1
                continue
            else:
                downwards = 0

            map_elements = functions.split_string(map[i])

            map_element = map_elements[pos - 1]

            if map_element == '#':
                trees += 1

            pos += right
            downwards += 1

        trees_per_slope.append(trees)

    result = 1

    for trees in trees_per_slope:
        result *= trees

    return result
コード例 #25
0
def get_rule_parts(rule):
    return functions.get_data_from_string(rule, ' bags contain ')