diff --git a/17/17-1.py b/17/17-1.py new file mode 100644 index 0000000..c5611c4 --- /dev/null +++ b/17/17-1.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from sys import exc_info + +from time import time + +start_time = time() + +ex_input = """Register A: 729 +Register B: 0 +Register C: 0 +Program: 0,1,5,4,3,0 +""" + +real_input = """Register A: 25358015 +Register B: 0 +Register C: 0 +Program: 2,4,1,1,7,5,0,3,4,7,1,6,5,5,3,0 +""" + +def read_input(input_var:str)->tuple[int, int, int, list[int]]: + inp = input_var.split("\n") + a, b, c = int(inp[0].split(':')[1]), int(inp[1].split(':')[1]), int(inp[2].split(':')[1]) + program = list(map(int, inp[3].split(':')[1].split(","))) + return a, b, c, program + +def do(instruction:int, operand:int, point:int): + global ra, rb, rc, out + if operand in [0, 1, 2, 3]: + ope = operand + elif operand == 4: + ope = ra + elif operand == 5: + ope = rb + elif operand == 6: + ope = rc + match instruction: + case 0: + ra = int(ra/(2**ope)) + case 1: + rb = rb ^ operand + case 2: + rb = ope % 8 + case 3: + if ra != 0: + point = ope-2 + case 4: + rb = rb ^ rc + case 5: + out.append(f"{ope%8}") + case 6: + rb = int(ra/(2**ope)) + case 7: + rc = int(ra/(2**ope)) + case _: + pass + return point + +if __name__ == "__main__": + out = [] +# ra, rb, rc, prog = read_input(ex_input) + ra, rb, rc, prog = read_input(real_input) + print(ra, rb, rc, prog) + pointer = 0 + while pointer < len(prog): + pointer = do(prog[pointer], prog[pointer+1], pointer) + pointer += 2 + print(out) + print(",".join(out)) + print(f'Runtime: {time()-start_time:.5f} s') \ No newline at end of file diff --git a/17/17-2.py b/17/17-2.py new file mode 100644 index 0000000..17f0dea --- /dev/null +++ b/17/17-2.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from sys import exc_info + +from time import time + +start_time = time() + +ex_input = """Register A: 729 +Register B: 0 +Register C: 0 +Program: 0,1,5,4,3,0 +""" + +ex_p2_input = """Register A: 2024 +Register B: 0 +Register C: 0 +Program: 0,3,5,4,3,0 +""" + +real_input = """Register A: 25358015 +Register B: 0 +Register C: 0 +Program: 2,4,1,1,7,5,0,3,4,7,1,6,5,5,3,0 +""" + +def read_input(input_var:str)->list[int]: + inp = input_var.split("\n") + program = list(map(int, inp[3].split(':')[1].split(","))) + return program + +def do(instruction:int, operand:int, point:int): + global ra, rb, rc, out + if operand in [0, 1, 2, 3]: + ope = operand + elif operand == 4: + ope = ra + elif operand == 5: + ope = rb + elif operand == 6: + ope = rc + match instruction: + case 0: + ra = int(ra/(2**ope)) + case 1: + rb = rb ^ operand + case 2: + rb = ope % 8 + case 3: + if ra != 0: + point = ope-2 + case 4: + rb = rb ^ rc + case 5: + out.append(ope%8) + case 6: + rb = int(ra/(2**ope)) + case 7: + rc = int(ra/(2**ope)) + case _: + pass + return point + +def teile_bereich(a, b, c): + if c <= 0: + return [] + if a > b: + a, b = b, a # Tauscht a und b, falls a größer ist als b + ergebnis = [a] + schritt = (b - a) // c # Ganzzahlige Division + rest = (b - a) % c + for i in range(1, c): + nächster_wert = a + i * schritt + if i <= rest: + nächster_wert += 1 + ergebnis.append(nächster_wert) + ergebnis.append(b) + return ergebnis + + +if __name__ == "__main__": + # ra, rb, rc, prog = read_input(ex_input) + prog = read_input(ex_p2_input) + #ra, rb, rc, prog = read_input(real_input) + #print(rb, rc, prog) + print(teile_bereich(614124,1142214142,5)) + for i in range(11744234): + ra = i + rc, rb = 0, 0 + out = [] + pointer = 0 + while pointer < len(prog): + pointer = do(prog[pointer], prog[pointer+1], pointer) + pointer += 2 + if out == prog: + print(i) + print(f'Runtime: {time()-start_time:.5f} s') \ No newline at end of file