/** * @param integer $rowKey * @param integer $fieldKey * @param integer $value * @return string */ public function setField($rowKey, $fieldKey, $value) { $successReturn = json_encode(['status' => 1, 'message' => 'success']); $errorReturn = json_encode(['status' => 0, 'message' => 'not possible']); if (!$this->isValidInput($value)) { return $errorReturn; } if (!$this->hasInputChanged($rowKey, $fieldKey, $value)) { return $errorReturn; } $solver = new Solver(); if ($solver->checkIfPossible($rowKey, $fieldKey, $value)) { $_SESSION['sudokuGrid'][$rowKey][$fieldKey] = $value; return $successReturn; } $_SESSION['sudokuGrid'][$rowKey][$fieldKey] = null; return $errorReturn; }
public function testMass() { // 702 characters from 'A' to 'ZZ' $participants = []; for ($n = 'A'; $n !== 'AAA'; $n++) { $participants[] = $n; } Solver::one($participants); }
function main($argc, $argv) { if ($argc < 2 || $argc > 3) { usage($argv[0]); } // Als '-v' is meegegeven tijdens het starten, ga in verbose mode if ($argv[1] == '-v') { verbose(true); $argc--; array_shift($argv); } else { verbose(false); } // Reader voor de XML-bestanden $reader = new KnowledgeBaseReader(); // Parse een xml-bestand (het eerste argument) tot knowledge base $state = $reader->parse($argv[1]); // Start de solver, dat ding dat kan infereren $solver = new Solver(); // leid alle goals in de knowledge base af. $goals = $state->goals; // Begin met de doelen die we hebben op de goal stack te zetten foreach ($goals as $goal) { $state->goalStack->push($goal->name); } // Zo lang we nog vragen kunnen stellen, stel ze while (($question = $solver->solveAll($state)) instanceof AskedQuestion) { $answer = cli_ask($question); if ($answer instanceof Option) { $state->apply($answer->consequences, Yes::because("User answered '{$answer->description}' to '{$question->description}'")); } } // Geen vragen meer, print de gevonden oplossingen. foreach ($goals as $goal) { printf("%s: %s\n", $goal->description, $goal->answer($state)->description); } }
private function breakRow() { foreach ($this->braille_str_lines as $lines) { $this->exploded_lines[] = str_replace(["\n"], [""], explode(" ", $lines)); } } private function flattenBrailleChars() { $flattenedChar = []; for ($i = 0; $i <= count($this->exploded_lines[0]) - 1; $i++) { for ($j = 0; $j <= 2; $j++) { $flattenedChar[] = trim($this->exploded_lines[$j][$i]); } $this->chars[] = implode("", $flattenedChar); $flattenedChar = []; } } public function solve() { $this->breakToThreeLines(); $this->breakRow(); $this->flattenBrailleChars(); $word = ''; foreach ($this->chars as $value) { $word .= $this->braille_table[$value]; } echo $word; } } $s = new Solver($braille_str, $braille_table); echo $s->solve();
<?php require '../util.php'; require '../solver.php'; $test_description = "Testing a variable in the fact value side in the knowledege state facts dict."; $rule = new Rule(); $rule->condition = new FactCondition('a', 'c'); $rule->consequences = ['test' => 'passed']; $rule->inferred_facts->push('test'); $state = new KnowledgeState(); $state->facts = ['a' => '$b', 'b' => 'c']; $state->rules->push($rule); $state->goalStack->push('test'); $solver = new Solver(); $solver->solveAll($state); // The fact that c is true should have been recorded assert('$state->value("test") == "passed"');
<?php $input = file_get_contents('day17.txt'); $lines = explode("\r\n", $input); $lines = array_filter($lines); $containers = []; foreach ($lines as $line) { $containers[] = intval($line); } $solver = new Solver(); $ans = $solver->solve($containers, 150); printf('ans#17.1: %u' . PHP_EOL, $ans[1]); printf('ans#17.2: %u' . PHP_EOL, $ans[2]); class Solver { public $used = []; public $found = 0; public $foundMin = 0; public $min; public $max; public function solve($values, $max) { $this->used = []; $this->min = count($values); $this->max = $max; while ($values) { end($values); $key = key($values); $node = new Node($key, array_pop($values)); $this->doSolve($values, $node, 150); }
} function printBoard(&$s) { $indent = false; $i = 0; while ($i < strlen($s)) { if ($indent) { print " "; } $j = 0; while ($j < BOARD_COLS) { print $s[$i]; print " "; $j++; $i++; } print "\n"; $indent = !$indent; } print "\n"; } function printSolutions() { printf("%d solutions found\n\n", $this->n); $this->printBoard($this->first); $this->printBoard($this->last); } } $s = new Solver($argv[1]); $s->findSolutions(); $s->printSolutions();
<?php require '../util.php'; require '../solver.php'; $a_b = new Rule(); $a_b->condition = new FactCondition('a', 'true'); $a_b->consequences = ['b' => 'true']; $a_b->inferred_facts->push('b'); $b_c = new Rule(); $b_c->condition = new FactCondition('b', 'true'); $b_c->consequences = ['c' => 'true']; $b_c->inferred_facts->push('c'); // This rule will evaluate to 'no' and should also be removed $a_d = new Rule(); $a_d->condition = new FactCondition('a', 'false'); $a_d->consequences = ['d' => 'true']; $a_d->inferred_facts->push('d'); $state = new KnowledgeState(); $state->facts = ['a' => 'true']; $state->rules->pushAll([$a_b, $a_d, $b_c]); $solver = new Solver(); $solver->forwardChain($state); // The fact that c is true should have been recorded assert('$state->facts["c"] == "true"'); // The fact that intermediate b is also true should be recorded assert('$state->facts["b"] == "true"'); // There should be no knowledge about d since the rule was false. assert('!isset($state->facts["d"])'); // All rules should have been applied and deleted assert('$state->rules->isEmpty()');
// Receiving input frequency $response['frequency'] = Encoder::getFrequency($request->data); // Setting success status $response['status'] = 'success'; // Setting solution attempt $response['solution'] = Solver::tryGuess($request->data); } else { if ($request->action === 'decode') { // Receiving decoded text $response['data'] = Decoder::decode($request->data, (int) $request->rot); // Setting success status $response['frequency'] = Decoder::getFrequency($request->data); // Setting success status $response['status'] = 'success'; // Setting solution attempt $response['solution'] = Solver::tryGuess($request->data); } else { // Default for external POST requests //Setting error message $response['error'] = 'Wrong params for Caesar code'; // Setting fail status $response['status'] = 'fail'; } } } // Server json output echo json_encode($response); } /* * Returns standard frequency to a certain user */
<? require 'Solver.php'; try{ // You probably want to change this $db = new PDO('mysql:host=localhost;dbname=anagram', 'root', 'root'); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); // Throw exceptions }catch(PDOException $e){ die('Database on fire. ('. $e->getMessage() . ')'); } $slvr = new Solver($db); $slvr->solve($_GET['q'], $_GET['lang']);
private function _whenSolvingFor($puzzle) { $this->_solution = $this->_solver->solve($this->_grid, $puzzle); }