<commit_before>"""Day 1, problem 1, of Advent of Code."""

TEST_DIRECTIONS_1 = ['R2', 'L3']
TEST_DIRECTIONS_2 = ['R2', 'R2', 'R2']
TEST_DIRECTIONS_3 = ['R5', 'L5', 'R5', 'R3']
DIRECTIONS = [
    'R1', 'R3', 'L2', 'L5', 'L2', 'L1', 'R3', 'L4', 'R2', 'L2', 'L4', 'R2',
    'L1', 'R1', 'L2', 'R3', 'L1', 'L4', 'R2', 'L5', 'R3', 'R4', 'L1', 'R2',
    'L1', 'R3', 'L4', 'R5', 'L4', 'L5', 'R5', 'L3', 'R2', 'L3', 'L3', 'R1',
    'R3', 'L4', 'R2', 'R5', 'L4', 'R1', 'L1', 'L1', 'R5', 'L2', 'R1', 'L2',
    'R188', 'L5', 'L3', 'R5', 'R1', 'L2', 'L4', 'R3', 'R5', 'L3', 'R3', 'R45',
    'L4', 'R4', 'R72', 'R2', 'R3', 'L1', 'R1', 'L1', 'L1', 'R192', 'L1', 'L1',
    'L1', 'L4', 'R1', 'L2', 'L5', 'L3', 'R5', 'L3', 'R3', 'L4', 'L3', 'R1',
    'R4', 'L2', 'R2', 'R3', 'L5', 'R3', 'L1', 'R1', 'R4', 'L2', 'L3', 'R1',
    'R3', 'L4', 'L3', 'L4', 'L2', 'L2', 'R1', 'R3', 'L5', 'L1', 'R4', 'R2',
    'L4', 'L1', 'R3', 'R3', 'R1', 'L5', 'L2', 'R4', 'R4', 'R2', 'R1', 'R5',
    'R5', 'L4', 'L1', 'R5', 'R3', 'R4', 'R5', 'R3', 'L1', 'L2', 'L4', 'R1',
    'R4', 'R5', 'L2', 'L3', 'R4', 'L4', 'R2', 'L2', 'L4', 'L2', 'R5', 'R1',
    'R4', 'R3', 'R5', 'L4', 'L4', 'L5', 'L5', 'R3', 'R4', 'L1', 'L3', 'R2',
    'L2', 'R1', 'L3', 'L5', 'R5', 'R5', 'R3', 'L4', 'L2', 'R4', 'R5', 'R1',
    'R4', 'L3']

def interpret_directions(directions):
    """Interpret given directions.

    Args:
        directions (list): A list of strings indication turn direction and
                           length of distance.
    Returns:
        (int): Distance, in blocks, from origin.
    """
    index = 0
    ns_parity = 1
    ew_parity = 1
    direction_parity_table = {'R': 1, 'L': -1}

    block_distance = 0

    for direction in directions:
        direction_parity = direction_parity_table[direction[0]]

        if index % 2 == 0: #heading ew
            if ns_parity == 1: #facing north
                ew_parity = direction_parity
            else: #facing south
                ew_parity = -direction_parity

            block_distance += ew_parity * int(direction[1:])
        else: #heading ns
            if ew_parity == 1: #facing east
                ns_parity = -direction_parity
            else: #facing west
                ns_parity = direction_parity

            block_distance += ns_parity * int(direction[1:])

        index += 1

    return abs(block_distance)


def main():
    """Main of the file.
    """
    print(interpret_directions(TEST_DIRECTIONS_1))
    print(interpret_directions(TEST_DIRECTIONS_2))
    print(interpret_directions(TEST_DIRECTIONS_3))
    print(interpret_directions(DIRECTIONS))

if __name__ == '__main__':
    main()<commit_msg>Fixed a big bug in day 1, project 1.<commit_after>"""Day 1, problem 1, of Advent of Code."""

TEST_DIRECTIONS_1 = ['R2', 'L3']
TEST_DIRECTIONS_2 = ['R2', 'R2', 'R2']
TEST_DIRECTIONS_3 = ['R5', 'L5', 'R5', 'R3']
DIRECTIONS = [
    'R1', 'R3', 'L2', 'L5', 'L2', 'L1', 'R3', 'L4', 'R2', 'L2', 'L4', 'R2',
    'L1', 'R1', 'L2', 'R3', 'L1', 'L4', 'R2', 'L5', 'R3', 'R4', 'L1', 'R2',
    'L1', 'R3', 'L4', 'R5', 'L4', 'L5', 'R5', 'L3', 'R2', 'L3', 'L3', 'R1',
    'R3', 'L4', 'R2', 'R5', 'L4', 'R1', 'L1', 'L1', 'R5', 'L2', 'R1', 'L2',
    'R188', 'L5', 'L3', 'R5', 'R1', 'L2', 'L4', 'R3', 'R5', 'L3', 'R3', 'R45',
    'L4', 'R4', 'R72', 'R2', 'R3', 'L1', 'R1', 'L1', 'L1', 'R192', 'L1', 'L1',
    'L1', 'L4', 'R1', 'L2', 'L5', 'L3', 'R5', 'L3', 'R3', 'L4', 'L3', 'R1',
    'R4', 'L2', 'R2', 'R3', 'L5', 'R3', 'L1', 'R1', 'R4', 'L2', 'L3', 'R1',
    'R3', 'L4', 'L3', 'L4', 'L2', 'L2', 'R1', 'R3', 'L5', 'L1', 'R4', 'R2',
    'L4', 'L1', 'R3', 'R3', 'R1', 'L5', 'L2', 'R4', 'R4', 'R2', 'R1', 'R5',
    'R5', 'L4', 'L1', 'R5', 'R3', 'R4', 'R5', 'R3', 'L1', 'L2', 'L4', 'R1',
    'R4', 'R5', 'L2', 'L3', 'R4', 'L4', 'R2', 'L2', 'L4', 'L2', 'R5', 'R1',
    'R4', 'R3', 'R5', 'L4', 'L4', 'L5', 'L5', 'R3', 'R4', 'L1', 'L3', 'R2',
    'L2', 'R1', 'L3', 'L5', 'R5', 'R5', 'R3', 'L4', 'L2', 'R4', 'R5', 'R1',
    'R4', 'L3']

def interpret_directions(directions):
    """Interpret given directions.

    Args:
        directions (list): A list of strings indication turn direction and
                           length of distance.
    Returns:
        (int): Distance, in blocks, from origin.
    """
    index = 0
    ns_parity = 1
    ew_parity = 1
    direction_parity_table = {'R': 1, 'L': -1}

    ns_distance = 0
    ew_distance = 0

    for direction in directions:
        direction_parity = direction_parity_table[direction[0]]

        if index % 2 == 0: #heading ew
            if ns_parity == 1: #facing north
                ew_parity = direction_parity
            else: #facing south
                ew_parity = -direction_parity

            ew_distance += ew_parity * int(direction[1:])
        else: #heading ns
            if ew_parity == 1: #facing east
                ns_parity = -direction_parity
            else: #facing west
                ns_parity = direction_parity

            ns_distance += ns_parity * int(direction[1:])

        index += 1

    return abs(ns_distance) + abs(ew_distance)


def main():
    """Main of the file.
    """
    print(interpret_directions(['R8', 'R4']))
    print(interpret_directions(TEST_DIRECTIONS_1))
    print(interpret_directions(TEST_DIRECTIONS_2))
    print(interpret_directions(TEST_DIRECTIONS_3))
    print(interpret_directions(DIRECTIONS))

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.25510962467484205, 'fraction_numerical': 0.07785209959123003, 'mean_word_length': 2.8698777857656363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6902362', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2423, 'n_words': 598}