This commit is contained in:
2024-12-17 15:43:54 +01:00
parent 0a36d9db83
commit 71629c5523
2 changed files with 167 additions and 0 deletions

97
17/17-2.py Normal file
View File

@@ -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')