⛄ Day 8

Advent of Code is a coding challenge that releases a puzzle every day starting December 1st up until the 25th - like an Advent Calendar but no stale chocolate...

Part 1

Find the value of the 'Accumulator' in some almost assembly of some in-flight entertainment
Solution:
                                
                                    
    import csv
    import copy
    
    
    def process_line(line):
        instruction = line.split(' ')
        return {'ins': instruction[0], 'val': int(instruction[1])}
    
    
    def get_input(file_name):
        with open(file_name, 'r') as data:
            return [process_line(line[0]) for line in csv.reader(data)]
    
    
    instructions = get_input('day8.txt')
    
    
    def process_instructions(instructions, display_repeat=False):
        accumulator = 0
        current_index = 0
        instructions_run = 0
        used_indices = []
        used_instruc = []
        while True:
            if current_index >= len(instructions):
                print('------------------------')
                print('OUT OF INDEX - SUCCESS')
                print('ACC', accumulator)
                print('------------------------')
                return True
            if current_index in used_indices:
                if display_repeat:
                    print('------------------------')
                    print('REPEATED INSTRUCTION')
                    print('ACC', accumulator)
                    print('------------------------')
                return False
            current_ins = instructions[current_index]
            instructions_run += 1
            if current_ins['ins'] == 'nop':
                used_indices.append(current_index)
                current_index += 1
            elif current_ins['ins'] == 'acc':
                accumulator += current_ins['val']
                used_indices.append(current_index)
                current_index += 1
            elif current_ins['ins'] == 'jmp':
                used_indices.append(current_index)
                current_index += current_ins['val']
    
    process_instructions(instructions, display_repeat=True)
        
                                
                            

Part 2

One instruction is corrupted - fix the error and find the value of the Accumulator after the program terminates successfully
Solution:
                                
                                    
    for i in range(0, len(instructions)):
        count += 1
        _instructions = copy.deepcopy(instructions)
        instruction = _instructions[i]
        if instruction['ins'] == 'jmp':
            _instructions[i]['ins'] = 'nop'
        elif instruction['ins'] == 'nop':
            _instructions[i]['ins'] = 'jmp'
        success = process_instructions(_instructions)
        if success:
            print("INSTRUCTION CHANGED", instruction)
            break