Second attempt on 17

This commit is contained in:
tabledevil
2024-12-27 22:39:44 +01:00
parent cb2e62d3d7
commit b4155378bd
2 changed files with 388 additions and 122 deletions

145
17/17.go Normal file
View File

@@ -0,0 +1,145 @@
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
// QuickCheckRxa validates if a given rxa produces the program as output
func QuickCheckRxa(rxa int, prog []int) bool {
rxb := 0
rxc := 0
ip := 0
output := []int{}
for ip < len(prog) {
opcode := prog[ip]
operand := prog[ip+1]
switch opcode {
case 0: // adv: rxa / 2^operand
if operand >= 0 && operand <= 3 {
rxa = rxa / (1 << operand)
} else {
return false
}
ip += 2
case 1: // bxl: rxb XOR operand
rxb ^= operand
ip += 2
case 2: // bst: operand % 8
switch operand {
case 4:
operand = rxa
case 5:
operand = rxb
case 6:
operand = rxc
}
rxb = operand % 8
ip += 2
case 3: // jnz: jump if rxa != 0
if rxa != 0 {
ip = operand
} else {
ip += 2
}
case 4: // bxc: rxb XOR rxc
rxb ^= rxc
ip += 2
case 5: // out: operand % 8
switch operand {
case 4:
operand = rxa
case 5:
operand = rxb
case 6:
operand = rxc
}
output = append(output, operand%8)
ip += 2
default:
return false
}
// Check if output mismatches the program so far
if len(output) > len(prog) {
return false
}
for i, val := range output {
if val != prog[i] {
return false
}
}
}
return len(output) == len(prog)
}
func workerDynamic(taskQueue <-chan int, prog []int, result chan int, done <-chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case <-done:
return
case rxa, ok := <-taskQueue:
if !ok {
return
}
if QuickCheckRxa(rxa, prog) {
select {
case result <- rxa:
default:
}
return
}
}
}
}
func main() {
prog := []int{2, 4, 1, 3, 7, 5, 4, 0, 1, 3, 0, 3, 5, 5, 3, 0}
start := time.Now()
taskQueue := make(chan int, 1000) // Buffered queue for tasks
result := make(chan int, 1)
done := make(chan struct{})
var wg sync.WaitGroup
numWorkers := runtime.NumCPU()
// Status updater
go func() {
for {
time.Sleep(1 * time.Second)
fmt.Printf("Queue size: %d\n", len(taskQueue))
}
}()
// Launch workers
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go workerDynamic(taskQueue, prog, result, done, &wg)
}
// Generate tasks dynamically
go func() {
for rxa := 0; ; rxa++ {
select {
case <-done:
close(taskQueue)
return
default:
taskQueue <- rxa
}
}
}()
// Wait for result
foundRxa := <-result
close(done) // Signal workers to stop
wg.Wait() // Ensure all workers exit cleanly
duration := time.Since(start)
fmt.Printf("Found rxa: %d\n", foundRxa)
fmt.Printf("Execution time: %s\n", duration)
}

View File

@@ -2,101 +2,124 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 220, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def decombo(operand):\n", "class ElfberryPiCPU:\n",
" match operand:\n", " def __init__(self, rxa, rxb, rxc, prog):\n",
" case x if x >=0 and x<=3:\n", " self.rxa = rxa\n",
" return operand\n", " self.rxb = rxb\n",
" case 4:\n", " self.rxc = rxc\n",
" return rxa\n", " self.ip = 0\n",
" case 5:\n", " self.prog = prog\n",
" return rxb\n", " self.output = None\n",
" case 6:\n",
" return rxc\n",
" case _:\n",
" raise ValueError\n",
"\n", "\n",
"def operation(opcode,operand, debug=False):\n", " def decombo(self, operand):\n",
" global rxa,rxb,rxc,ip,output\n", " match operand:\n",
" match(opcode):\n", " case x if 0 <= x <= 3:\n",
" case 0:\n", " return operand\n",
" operand = decombo(operand)\n", " case 4:\n",
" result = int(rxa/(2**operand))\n", " return self.rxa\n",
" if debug: print(f\"adv: {rxa} / 2^{operand} = {result}\")\n", " case 5:\n",
" rxa = result\n", " return self.rxb\n",
" ip += 2\n", " case 6:\n",
" case 1:\n", " return self.rxc\n",
" result = rxb ^ operand\n", " case _:\n",
" if debug: print(f\"bxl: {rxb} XOR {operand} = {result}\")\n", " raise ValueError(\"Invalid operand\")\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",
"\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", "\n",
"def get_instruction(index=None):\n", " def get_instruction(self, index=None):\n",
" if index is None:\n", " if index is None:\n",
" index = ip\n", " index = self.ip\n",
" if index < 0 or (index+1) > len(prog):\n", " if index < 0 or (index + 1) > len(self.prog):\n",
" return None\n", " return None\n",
" return prog[index],prog[index+1]\n", " return self.prog[index], self.prog[index + 1]\n",
"\n", "\n",
"def state():\n", " def __str__(self):\n",
" print(f\"rxa: {rxa}\")\n", " return f\"{self.state()}\"\n",
" print(f\"rxb: {rxb}\")\n", "\n",
" print(f\"rxc: {rxc}\")\n", " def state(self):\n",
" print(f\"output: {','.join(output)}\")\n", " return {\n",
" print(f\"instruction pointer : {[ip]}\")\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", "cell_type": "code",
"execution_count": 221, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"rxa: 729\n", "{'rxa': 729, 'rxb': 0, 'rxc': 0, 'instruction_pointer': 0}\n"
"rxb: 0\n",
"rxc: 0\n",
"output: \n",
"instruction pointer : [0]\n"
] ]
} }
], ],
@@ -106,63 +129,159 @@
"rxc = 0\n", "rxc = 0\n",
"ip = 0\n", "ip = 0\n",
"prog = [0,1,5,4,3,0]\n", "prog = [0,1,5,4,3,0]\n",
"output = []\n", "testcpu = ElfberryPiCPU(rxa,rxb,rxc,prog)\n",
"state()" "print(testcpu)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 222, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "data": {
"output_type": "stream", "text/plain": [
"text": [ "'4,6,3,5,6,3,5,2,1,0'"
"rxa: 0\n", ]
"rxb: 0\n", },
"rxc: 0\n", "execution_count": 3,
"output: 4,6,3,5,6,3,5,2,1,0\n", "metadata": {},
"instruction pointer : [6]\n" "output_type": "execute_result"
]
} }
], ],
"source": [ "source": [
"while instruction:=get_instruction():\n", "\",\".join([str(x) for x in testcpu.outiterator()])"
" operation(*instruction)\n",
"state()\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 223, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"rxa: 51342988\n",
"rxb: 0\n",
"rxc: 0\n",
"output: \n",
"instruction pointer : [0]\n"
]
}
],
"source": [ "source": [
"rxa = 51342988\n", "class ElfberryPiCPU:\n",
"rxb = 0\n", " def __init__(self, rxa, rxb, rxc, prog):\n",
"rxc = 0\n", " self.rxa = rxa\n",
"ip = 0\n", " self.rxb = rxb\n",
"prog = [2,4,1,3,7,5,4,0,1,3,0,3,5,5,3,0]\n", " self.rxc = rxc\n",
"output = []\n", " self.ip = 0\n",
"state()" " 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", "cell_type": "code",
"execution_count": 224, "execution_count": 264,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -185,17 +304,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 13,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"for x in range(51342988):\n", "x = 118000000\n",
" rxa = x\n", "while True:\n",
" rxb = 0\n", " x+=1\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", " 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",
" " " "
] ]
} }