<jupyter_start><jupyter_text>Day 1: No Time for a Taxicab Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th.Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection.There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination?For example:Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away.R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away.R5, L5, R5, R3 leaves you 12 blocks away.How many blocks away is Easter Bunny HQ?<jupyter_code>ORIENTATIONS = {
    'N': {'L': 'W', 'R': 'E'},
    'W': {'L': 'S', 'R': 'N'},
    'S': {'L': 'E', 'R': 'W'},
    'E': {'L': 'N', 'R': 'S'}
}

MOVEMENTS = {
    'N': (1, 0),
    'W': (0, -1),
    'S': (-1, 0),
    'E': (0, 1)
}

def onemove(position, orientation, direction):
    next_orientation = ORIENTATIONS[orientation][direction[0]]
    next_position = [a+int(direction[1:])*b for a,b in zip(position, MOVEMENTS[next_orientation])]
    return next_orientation, next_position

def blocksaway(directions):
    position = [0, 0]
    orientation = 'N'
    for s in directions.split(','):
        orientation, position = onemove(position, orientation, s.strip())
        
    return sum(abs(a) for a in position)<jupyter_output><empty_output><jupyter_text>Test examples<jupyter_code>blocksaway("R2, L3")
blocksaway("R2, R2, R2")
blocksaway("R5, L5, R5, R3")<jupyter_output><empty_output><jupyter_text>Process input<jupyter_code>with open("inputs/day1a.txt") as fd:
    print("Easter Bunny HQ is {} blocks away".format(blocksaway(fd.read())))<jupyter_output>Easter Bunny HQ is 291 blocks away<jupyter_text>Part Two Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice.For example, if your instructions are R8, R4, R4, R8, the first location you visit twice is 4 blocks away, due East.How many blocks away is the first location you visit twice?<jupyter_code>def firsttwice(directions):
    positions = set()
    position = (0, 0)
    positions.add(position)
    
    orientation = 'N'
    for s in directions.split(','):
        orientation, next_position = onemove(position, orientation, s.strip())
        
        if next_position[0] == position[0]:
            sign = -1 if next_position[1] < position[1] else 1 
            range_positions = [(position[0], a+sign) for a in range(position[1], next_position[1], sign)]
        else:
            sign = -1 if next_position[0] < position[0] else 1
            range_positions = [(a+sign, position[1]) for a in range(position[0], next_position[0], sign)]
        
        for position in range_positions:
            if position in positions:
                return sum(abs(a) for a in position)
            
            positions.add(position)
            
    return sum(abs(a) for a in position)
# Test example
firsttwice('R8, R4, R4, R8')
with open("inputs/day1a.txt") as fd:
    print("Real Easter Bunny HQ is {} blocks away".format(firsttwice(fd.read())))<jupyter_output>Real Easter Bunny HQ is 159 blocks away
{'fraction_non_alphanumeric': 0.104, 'fraction_numerical': 0.018666666666666668, 'mean_word_length': 3.808857808857809, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '373813', 'license': 'MIT', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1215, 'n_words': 510, 'path': '2016/jordi/Day 1 - No Time for a Taxicab.ipynb', 'repo_name': 'bbglab/adventofcode', 'size': '4125'}