$this->explore($state->minus()); } } public static function clearCache() { self::$cache = array(); } } class UglyNumbers { private static $counter = NULL; public static function solve($digits) { self::initCounter(); StateExplorer::clearCache(); $solver = new StateExplorer(self::$counter); $solver->explore(new State($digits, strlen($digits))); return self::$counter->count; } private static function initCounter() { if (is_null(self::$counter)) { self::$counter = new UglyCounter(); } else { self::$counter->count = 0; } } } foreach (file($argv[1]) as $digits) { echo UglyNumbers::solve(trim($digits)) . "\n"; }
/** * @dataProvider known_solutions */ public function test_uglyNumbers($input, $expectedOutput) { $this->assertEquals($expectedOutput, UglyNumbers::solve($input)); }