Created
December 8, 2020 13:32
-
-
Save cra/c17944b13b180390a1ef7651c44c0d1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
from __future__ import annotations | |
import itertools | |
import pathlib | |
import operator | |
from typing import Dict, List, NamedTuple, Optional, Tuple, Union | |
def solve(puzzle_input: List[str], verbose: bool = False) -> int: | |
acc = 0 | |
instructions = puzzle_input[:] | |
i = 0 | |
lines_seen = {} | |
while i < len(instructions): | |
if verbose: | |
print(f'Current instruction #{i} == {instructions[i]}') | |
print(lines_seen) | |
if i in lines_seen: | |
if verbose: | |
print('found line that I saw earlier') | |
break | |
lines_seen[i] = True | |
command = instructions[i] | |
whatdo, withwhat = command.split() | |
if whatdo == 'nop': | |
i += 1 | |
elif whatdo == 'acc': | |
acc += int(withwhat) | |
i += 1 | |
elif whatdo == 'jmp': | |
i += int(withwhat) | |
success_run = (i == len(instructions)) | |
return acc, success_run | |
def get_all_permutations(puzzle_input: List[str]) -> List[Tuple[int, str, List[str]]]: | |
all_nop_positions = [] | |
all_jmp_positions = [] | |
for position, instruction in enumerate(puzzle_input): | |
if instruction.startswith('jmp'): | |
all_jmp_positions.append(position) | |
elif instruction.startswith('nop'): | |
all_nop_positions.append(position) | |
pertubations = [] | |
for position in all_nop_positions: | |
permuted_input = puzzle_input[:] | |
permuted_input[position] = permuted_input[position].replace('nop', 'jmp') | |
pertubations.append( | |
(position, 'nop', permuted_input) | |
) | |
for position in all_jmp_positions: | |
permuted_input = puzzle_input[:] | |
permuted_input[position] = permuted_input[position].replace('jmp', 'nop') | |
pertubations.append( | |
(position, 'jmp', permuted_input) | |
) | |
return pertubations | |
def main(): | |
test_puzzle_input = """ | |
nop +0 | |
acc +1 | |
jmp +4 | |
acc +3 | |
jmp -3 | |
acc -99 | |
acc +1 | |
jmp -4 | |
acc +6 | |
""".strip().split('\n') | |
print('Test puzzle result', solve(test_puzzle_input, verbose=True)) | |
permutations = get_all_permutations(test_puzzle_input) | |
for i, (pos, op, ops) in enumerate(permutations): | |
codes = ''.join(map(operator.itemgetter(0), ops)) | |
result, success_run = solve(ops) | |
print(f'{i}. Swaping {op} at position {pos}. {codes} -> {result}, finished normally? {success_run}') | |
print('---') | |
with open('puzzle_input') as fp: | |
puzzle_input = [line.strip() for line in fp] | |
permutations = get_all_permutations(puzzle_input) | |
for i, (pos, op, ops) in enumerate(permutations): | |
codes = ''.join(map(operator.itemgetter(0), ops)) | |
result, success_run = solve(ops) | |
if success_run: | |
print(f'{i}. Swaping {op} at position {pos}. SUCCESS. Result = {result}') | |
print('not going to look further, dis is the shit!') | |
break | |
else: | |
print(f'{i}. Swaping {op} at position {pos}. Infiloop') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment