Second attempt on 17
This commit is contained in:
365
17/17.ipynb
365
17/17.ipynb
@@ -2,101 +2,124 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 220,
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def decombo(operand):\n",
|
||||
" match operand:\n",
|
||||
" case x if x >=0 and x<=3:\n",
|
||||
" return operand\n",
|
||||
" case 4:\n",
|
||||
" return rxa\n",
|
||||
" case 5:\n",
|
||||
" return rxb\n",
|
||||
" case 6:\n",
|
||||
" return rxc\n",
|
||||
" case _:\n",
|
||||
" raise ValueError\n",
|
||||
"class ElfberryPiCPU:\n",
|
||||
" def __init__(self, rxa, rxb, rxc, prog):\n",
|
||||
" self.rxa = rxa\n",
|
||||
" self.rxb = rxb\n",
|
||||
" self.rxc = rxc\n",
|
||||
" self.ip = 0\n",
|
||||
" self.prog = prog\n",
|
||||
" self.output = None\n",
|
||||
"\n",
|
||||
"def operation(opcode,operand, debug=False):\n",
|
||||
" global rxa,rxb,rxc,ip,output\n",
|
||||
" match(opcode):\n",
|
||||
" case 0:\n",
|
||||
" operand = decombo(operand)\n",
|
||||
" result = int(rxa/(2**operand))\n",
|
||||
" if debug: print(f\"adv: {rxa} / 2^{operand} = {result}\")\n",
|
||||
" rxa = result\n",
|
||||
" ip += 2\n",
|
||||
" case 1:\n",
|
||||
" result = rxb ^ operand\n",
|
||||
" if debug: print(f\"bxl: {rxb} XOR {operand} = {result}\")\n",
|
||||
" rxb = result\n",
|
||||
" ip += 2\n",
|
||||
" case 2:\n",
|
||||
" operand = decombo(operand)\n",
|
||||
" result = operand % 8\n",
|
||||
" if debug: print(f\"bst: {operand} % 8 = {result}\")\n",
|
||||
" rxb = result\n",
|
||||
" ip += 2\n",
|
||||
" case 3:\n",
|
||||
" result = rxa!=0\n",
|
||||
" if debug: print(f\"jnz: {rxa} != 0 is {result}\")\n",
|
||||
" ip = operand if result else ip+2\n",
|
||||
" case 4:\n",
|
||||
" result = rxb ^ rxc\n",
|
||||
" if debug: print(f\"bxc: {rxb} XOR {rxc} = {result}\")\n",
|
||||
" rxb=result \n",
|
||||
" ip += 2\n",
|
||||
" case 5:\n",
|
||||
" operand = decombo(operand)\n",
|
||||
" result = operand % 8\n",
|
||||
" if debug: print(f\"out: {operand} % 8 = {operand % 8}\")\n",
|
||||
" output.append(f\"{result}\")\n",
|
||||
" ip += 2\n",
|
||||
" case 6:\n",
|
||||
" operand = decombo(operand)\n",
|
||||
" result = int(rxa/(2**operand))\n",
|
||||
" if debug: print(f\"bdv: {rxa} / 2^{operand} = {result}\")\n",
|
||||
" rxb = result\n",
|
||||
" ip += 2\n",
|
||||
" case 7:\n",
|
||||
" operand = decombo(operand)\n",
|
||||
" result = int(rxa/(2**operand))\n",
|
||||
" if debug: print(f\"cdv: {rxa} / 2^{operand} = {result}\")\n",
|
||||
" rxc = result\n",
|
||||
" ip += 2\n",
|
||||
" def decombo(self, operand):\n",
|
||||
" match operand:\n",
|
||||
" case x if 0 <= x <= 3:\n",
|
||||
" return operand\n",
|
||||
" case 4:\n",
|
||||
" return self.rxa\n",
|
||||
" case 5:\n",
|
||||
" return self.rxb\n",
|
||||
" case 6:\n",
|
||||
" return self.rxc\n",
|
||||
" case _:\n",
|
||||
" raise ValueError(\"Invalid operand\")\n",
|
||||
"\n",
|
||||
" def operation(self, opcode, operand, debug=False):\n",
|
||||
" match opcode:\n",
|
||||
" case 0:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = int(self.rxa / (2 ** operand))\n",
|
||||
" if debug:\n",
|
||||
" print(f\"adv: {self.rxa} / 2^{operand} = {result}\")\n",
|
||||
" self.rxa = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 1:\n",
|
||||
" result = self.rxb ^ operand\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bxl: {self.rxb} XOR {operand} = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 2:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = operand % 8\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bst: {operand} % 8 = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 3:\n",
|
||||
" result = self.rxa != 0\n",
|
||||
" if debug:\n",
|
||||
" print(f\"jnz: {self.rxa} != 0 is {result}\")\n",
|
||||
" self.ip = operand if result else self.ip + 2\n",
|
||||
" case 4:\n",
|
||||
" result = self.rxb ^ self.rxc\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bxc: {self.rxb} XOR {self.rxc} = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 5:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = operand % 8\n",
|
||||
" if debug:\n",
|
||||
" print(f\"out: {operand} % 8 = {result}\")\n",
|
||||
" self.output = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 6:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = int(self.rxa / (2 ** operand))\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bdv: {self.rxa} / 2^{operand} = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 7:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = int(self.rxa / (2 ** operand))\n",
|
||||
" if debug:\n",
|
||||
" print(f\"cdv: {self.rxa} / 2^{operand} = {result}\")\n",
|
||||
" self.rxc = result\n",
|
||||
" self.ip += 2\n",
|
||||
"\n",
|
||||
"def get_instruction(index=None):\n",
|
||||
" if index is None:\n",
|
||||
" index = ip\n",
|
||||
" if index < 0 or (index+1) > len(prog):\n",
|
||||
" return None\n",
|
||||
" return prog[index],prog[index+1]\n",
|
||||
" def get_instruction(self, index=None):\n",
|
||||
" if index is None:\n",
|
||||
" index = self.ip\n",
|
||||
" if index < 0 or (index + 1) > len(self.prog):\n",
|
||||
" return None\n",
|
||||
" return self.prog[index], self.prog[index + 1]\n",
|
||||
"\n",
|
||||
"def state():\n",
|
||||
" print(f\"rxa: {rxa}\")\n",
|
||||
" print(f\"rxb: {rxb}\")\n",
|
||||
" print(f\"rxc: {rxc}\")\n",
|
||||
" print(f\"output: {','.join(output)}\")\n",
|
||||
" print(f\"instruction pointer : {[ip]}\")\n"
|
||||
" def __str__(self):\n",
|
||||
" return f\"{self.state()}\"\n",
|
||||
"\n",
|
||||
" def state(self):\n",
|
||||
" return {\n",
|
||||
" \"rxa\": self.rxa,\n",
|
||||
" \"rxb\": self.rxb,\n",
|
||||
" \"rxc\": self.rxc,\n",
|
||||
" \"instruction_pointer\": self.ip\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" def outiterator(self, debug=False):\n",
|
||||
" while instruction := self.get_instruction():\n",
|
||||
" opcode, operand = instruction\n",
|
||||
" self.operation(opcode, operand, debug)\n",
|
||||
" if self.output is not None:\n",
|
||||
" yield self.output\n",
|
||||
" self.output=None\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 221,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"rxa: 729\n",
|
||||
"rxb: 0\n",
|
||||
"rxc: 0\n",
|
||||
"output: \n",
|
||||
"instruction pointer : [0]\n"
|
||||
"{'rxa': 729, 'rxb': 0, 'rxc': 0, 'instruction_pointer': 0}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -106,63 +129,159 @@
|
||||
"rxc = 0\n",
|
||||
"ip = 0\n",
|
||||
"prog = [0,1,5,4,3,0]\n",
|
||||
"output = []\n",
|
||||
"state()"
|
||||
"testcpu = ElfberryPiCPU(rxa,rxb,rxc,prog)\n",
|
||||
"print(testcpu)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 222,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"rxa: 0\n",
|
||||
"rxb: 0\n",
|
||||
"rxc: 0\n",
|
||||
"output: 4,6,3,5,6,3,5,2,1,0\n",
|
||||
"instruction pointer : [6]\n"
|
||||
]
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'4,6,3,5,6,3,5,2,1,0'"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"while instruction:=get_instruction():\n",
|
||||
" operation(*instruction)\n",
|
||||
"state()\n"
|
||||
"\",\".join([str(x) for x in testcpu.outiterator()])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 223,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"rxa: 51342988\n",
|
||||
"rxb: 0\n",
|
||||
"rxc: 0\n",
|
||||
"output: \n",
|
||||
"instruction pointer : [0]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"rxa = 51342988\n",
|
||||
"rxb = 0\n",
|
||||
"rxc = 0\n",
|
||||
"ip = 0\n",
|
||||
"prog = [2,4,1,3,7,5,4,0,1,3,0,3,5,5,3,0]\n",
|
||||
"output = []\n",
|
||||
"state()"
|
||||
"class ElfberryPiCPU:\n",
|
||||
" def __init__(self, rxa, rxb, rxc, prog):\n",
|
||||
" self.rxa = rxa\n",
|
||||
" self.rxb = rxb\n",
|
||||
" self.rxc = rxc\n",
|
||||
" self.ip = 0\n",
|
||||
" self.prog = prog\n",
|
||||
" self.output = None\n",
|
||||
"\n",
|
||||
" def decombo(self, operand):\n",
|
||||
" match operand:\n",
|
||||
" case x if 0 <= x <= 3:\n",
|
||||
" return operand\n",
|
||||
" case 4:\n",
|
||||
" return self.rxa\n",
|
||||
" case 5:\n",
|
||||
" return self.rxb\n",
|
||||
" case 6:\n",
|
||||
" return self.rxc\n",
|
||||
" case _:\n",
|
||||
" raise ValueError(\"Invalid operand\")\n",
|
||||
"\n",
|
||||
" def operation(self, opcode, operand, debug=False):\n",
|
||||
" match opcode:\n",
|
||||
" case 0:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = int(self.rxa / (2 ** operand))\n",
|
||||
" if debug:\n",
|
||||
" print(f\"adv: {self.rxa} / 2^{operand} = {result}\")\n",
|
||||
" self.rxa = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 1:\n",
|
||||
" result = self.rxb ^ operand\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bxl: {self.rxb} XOR {operand} = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 2:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = operand % 8\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bst: {operand} % 8 = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 3:\n",
|
||||
" result = self.rxa != 0\n",
|
||||
" if debug:\n",
|
||||
" print(f\"jnz: {self.rxa} != 0 is {result}\")\n",
|
||||
" self.ip = operand if result else self.ip + 2\n",
|
||||
" case 4:\n",
|
||||
" result = self.rxb ^ self.rxc\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bxc: {self.rxb} XOR {self.rxc} = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 5:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = operand % 8\n",
|
||||
" if debug:\n",
|
||||
" print(f\"out: {operand} % 8 = {result}\")\n",
|
||||
" self.output = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 6:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = int(self.rxa / (2 ** operand))\n",
|
||||
" if debug:\n",
|
||||
" print(f\"bdv: {self.rxa} / 2^{operand} = {result}\")\n",
|
||||
" self.rxb = result\n",
|
||||
" self.ip += 2\n",
|
||||
" case 7:\n",
|
||||
" operand = self.decombo(operand)\n",
|
||||
" result = int(self.rxa / (2 ** operand))\n",
|
||||
" if debug:\n",
|
||||
" print(f\"cdv: {self.rxa} / 2^{operand} = {result}\")\n",
|
||||
" self.rxc = result\n",
|
||||
" self.ip += 2\n",
|
||||
"\n",
|
||||
" def get_instruction(self, index=None):\n",
|
||||
" if index is None:\n",
|
||||
" index = self.ip\n",
|
||||
" if index < 0 or (index + 1) > len(self.prog):\n",
|
||||
" return None\n",
|
||||
" return self.prog[index], self.prog[index + 1]\n",
|
||||
"\n",
|
||||
" def __str__(self):\n",
|
||||
" return f\"{self.state()}\"\n",
|
||||
"\n",
|
||||
" def state(self):\n",
|
||||
" return {\n",
|
||||
" \"rxa\": self.rxa,\n",
|
||||
" \"rxb\": self.rxb,\n",
|
||||
" \"rxc\": self.rxc,\n",
|
||||
" \"instruction_pointer\": self.ip\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" def outiterator(self, debug=False):\n",
|
||||
" while instruction := self.get_instruction():\n",
|
||||
" opcode, operand = instruction\n",
|
||||
" self.operation(opcode, operand, debug)\n",
|
||||
" if self.output is not None:\n",
|
||||
" yield self.output\n",
|
||||
" self.output = None\n",
|
||||
"\n",
|
||||
" def test_program_output(self):\n",
|
||||
" generator = self.outiterator()\n",
|
||||
" for expected in self.prog:\n",
|
||||
" try:\n",
|
||||
" output = next(generator)\n",
|
||||
" if output != expected:\n",
|
||||
" return False # Mismatch found, terminate early\n",
|
||||
" except StopIteration:\n",
|
||||
" return False # Generator exhausted prematurely\n",
|
||||
"\n",
|
||||
" try:\n",
|
||||
" next(generator) # Check if generator produces extra output\n",
|
||||
" return False\n",
|
||||
" except StopIteration:\n",
|
||||
" return True # Generator matches program exactly\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 224,
|
||||
"execution_count": 264,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -185,17 +304,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for x in range(51342988):\n",
|
||||
" rxa = x\n",
|
||||
" rxb = 0\n",
|
||||
" rxc = 0\n",
|
||||
" ip = 0\n",
|
||||
"x = 118000000\n",
|
||||
"while True:\n",
|
||||
" x+=1\n",
|
||||
" prog = [2,4,1,3,7,5,4,0,1,3,0,3,5,5,3,0]\n",
|
||||
" output = []\n",
|
||||
" mycomp = ElfberryPiCPU(x,0,0,prog)\n",
|
||||
" if mycomp.test_program_output():\n",
|
||||
" print(f\"Solution : {x}\")\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
" "
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user