Python!
lines = [] with open('input.txt') as file: for line in file: line = line.rstrip().split(' ') lines.append([line[0], int(line[1])]) def move(lines, part_1=False): seen = set() accumulator = 0 idx = 0 while True: if idx >= len(lines): return accumulator move, arg = lines[idx] if idx in seen: return accumulator if part_1 else False seen.add(idx) if move == 'nop': idx += 1 elif move == 'acc': accumulator += arg idx += 1 elif move == "jmp": idx += arg def flip(val): return 'jmp' if val == 'nop' else 'nop' def change_piece(lines): for idx, turn in enumerate(lines): if turn[0] == 'nop' or turn[0] == 'jmp': prev = turn[0] lines[idx][0] = flip(turn[0]) if accumulator:= move(lines): return accumulator lines[idx][0] = prev print("Part 1", move(lines, True)) print("Part 2", change_piece(lines))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Python!