Still not done :(
This commit is contained in:
parent
84ef4499eb
commit
30f4a0b942
@ -7,99 +7,102 @@ from warnings import showwarning
|
|||||||
sys.path.insert(1, os.path.abspath('../../'))
|
sys.path.insert(1, os.path.abspath('../../'))
|
||||||
from python_tools.aoc_utils import *
|
from python_tools.aoc_utils import *
|
||||||
|
|
||||||
|
def run_program(r, p):
|
||||||
|
pc = 0
|
||||||
|
out = 0
|
||||||
|
while True:
|
||||||
|
operands = [0,1,2,3,r["A"],r["B"],r["C"], 7]
|
||||||
|
co = operands[p[pc+1]]
|
||||||
|
lo = p[pc+1]
|
||||||
|
match p[pc]:
|
||||||
|
case 0:
|
||||||
|
r["A"] = r["A"] >> co
|
||||||
|
pc += 2
|
||||||
|
case 1:
|
||||||
|
r["B"] = r["B"] ^ lo
|
||||||
|
pc += 2
|
||||||
|
case 2:
|
||||||
|
r["B"] = co % 8
|
||||||
|
pc += 2
|
||||||
|
case 3:
|
||||||
|
if r["A"] == 0:
|
||||||
|
pc += 2
|
||||||
|
else:
|
||||||
|
pc = lo
|
||||||
|
return out, pc
|
||||||
|
case 4:
|
||||||
|
r["B"] = r["B"] ^ r["C"]
|
||||||
|
pc += 2
|
||||||
|
case 5:
|
||||||
|
out = int(co % 8)
|
||||||
|
pc += 2
|
||||||
|
case 6:
|
||||||
|
r["B"] = r["A"] >> co
|
||||||
|
pc += 2
|
||||||
|
case 7:
|
||||||
|
r["C"] = r["A"] >> co
|
||||||
|
pc += 2
|
||||||
|
|
||||||
|
|
||||||
def part_one(registers, program):
|
def part_one(registers, program):
|
||||||
pc = 0
|
pc = 0
|
||||||
out = []
|
out = []
|
||||||
looping = True
|
looping = True
|
||||||
last_state = 0
|
|
||||||
current_state = [registers, program, pc]
|
|
||||||
while looping:
|
while looping:
|
||||||
if pc > len(program)-1 or current_state == last_state:
|
if pc > len(program)-1:
|
||||||
looping = False
|
looping = False
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
operands = [0,1,2,3,registers["A"],registers["B"],registers["C"], 7]
|
o, pc = run_program(registers, program)
|
||||||
co = operands[program[pc+1]]
|
out.append(o)
|
||||||
lo = program[pc+1]
|
|
||||||
|
|
||||||
match program[pc]:
|
|
||||||
case 0:
|
|
||||||
registers["A"] = registers["A"] >> co
|
|
||||||
pc += 2
|
|
||||||
case 1:
|
|
||||||
registers["B"] = registers["B"] ^ lo
|
|
||||||
pc += 2
|
|
||||||
case 2:
|
|
||||||
registers["B"] = co % 8
|
|
||||||
pc += 2
|
|
||||||
case 3:
|
|
||||||
if registers["A"] == 0:
|
|
||||||
pc += 2
|
|
||||||
else:
|
|
||||||
pc = lo
|
|
||||||
case 4:
|
|
||||||
registers["B"] = registers["B"] ^ registers["C"]
|
|
||||||
pc += 2
|
|
||||||
case 5:
|
|
||||||
out.append(int(co % 8))
|
|
||||||
pc += 2
|
|
||||||
case 6:
|
|
||||||
registers["B"] = registers["A"] >> co
|
|
||||||
pc += 2
|
|
||||||
case 7:
|
|
||||||
registers["C"] = registers["A"] >> co
|
|
||||||
pc += 2
|
|
||||||
print("P1: ", str(out).strip('[]').replace(" ", ""))
|
print("P1: ", str(out).strip('[]').replace(" ", ""))
|
||||||
|
return out
|
||||||
|
|
||||||
def part_two(r, p):
|
def part_two(r, p):
|
||||||
pc = 0
|
pc = 0
|
||||||
out = []
|
A = 0
|
||||||
looping = True
|
looping = True
|
||||||
last_state = 0
|
p_copy = p.copy()
|
||||||
current_state = [registers, program, pc]
|
p_copy.reverse()
|
||||||
while looping:
|
R = {"A": 0, "B": 0, "C": 0}
|
||||||
if pc > len(program)-1 or current_state == last_state:
|
for count, op in enumerate(p_copy):
|
||||||
looping = False
|
i = 0
|
||||||
break
|
while True:
|
||||||
else:
|
# print(A)
|
||||||
operands = [0,1,2,3,registers["A"],registers["B"],registers["C"], 7]
|
out = []
|
||||||
co = operands[program[pc+1]]
|
r = copy.deepcopy(R)
|
||||||
lo = program[pc+1]
|
r["A"] = A+i
|
||||||
|
# out = part_one(r, p)
|
||||||
|
o = -1
|
||||||
|
pc = 0
|
||||||
|
|
||||||
match program[pc]:
|
while pc == 0:
|
||||||
case 0:
|
o, pc = run_program(r, p)
|
||||||
registers["A"] = registers["A"] >> co
|
out.append(o)
|
||||||
pc += 2
|
# print(o)
|
||||||
case 1:
|
# if c > 0 and o != p[-(count-c)]:
|
||||||
registers["B"] = registers["B"] ^ lo
|
# failed = True
|
||||||
pc += 2
|
# break
|
||||||
case 2:
|
# c+=1
|
||||||
registers["B"] = co % 8
|
# if not failed:
|
||||||
pc += 2
|
if out == p[:count+1]:
|
||||||
case 3:
|
print(out)
|
||||||
if registers["A"] == 0:
|
A = (A+i)<<3
|
||||||
pc += 2
|
break
|
||||||
else:
|
i+=1
|
||||||
pc = lo
|
# else:
|
||||||
case 4:
|
# A += 1
|
||||||
registers["B"] = registers["B"] ^ registers["C"]
|
A = A>>3
|
||||||
pc += 2
|
|
||||||
case 5:
|
R["A"]=A
|
||||||
out.append(int(co % 8))
|
print("P2: ", A, R["A"], out)
|
||||||
pc += 2
|
part_one(R, p)
|
||||||
case 6:
|
|
||||||
registers["B"] = registers["A"] >> co
|
|
||||||
pc += 2
|
|
||||||
case 7:
|
|
||||||
registers["C"] = registers["A"] >> co
|
|
||||||
pc += 2
|
|
||||||
print("P2: ", count)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
program = []
|
program = []
|
||||||
registers = {}
|
registers = {}
|
||||||
pos = [0,0]
|
pos = [0,0]
|
||||||
contents = file2list("input.txt")
|
contents = file2list("test.txt")
|
||||||
for row, i in enumerate(contents):
|
for row, i in enumerate(contents):
|
||||||
if "Register" in i:
|
if "Register" in i:
|
||||||
a = i.split(":")
|
a = i.split(":")
|
||||||
|
Loading…
Reference in New Issue
Block a user