3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $recipes = [ 'cc' => [ 'in' => ['c' => 1], 'out' => ['cc' => 2], 'time' => 0.5, 'terminal' => true, ], 'gc' => [ 'in' => ['cc' => 1.5, 'i' => 1,], 'out' => ['gc' => 1], 'time' => 0.5, 'terminal' => false, ], 'rc' => [ 'in' => ['gc' => 2, 'p' => 2, 'cc' => 2], 'out' => ['rc' => 1], 'time' => 6, 'terminal' => false, ], 'bc' => [ 'in' => ['gc' => 20, 'rc' => 2], 'out' => ['bc' => 1], 'time' => 10, 'terminal' => false, ], ]; function solveFor($recipes, $type, $numAssemblers, $requirements = []) { if ( $recipes[$type]['terminal']) { foreach($recipes[$type]['in'] as $k => $v) { if (! array_key_exists($type, $requirements)) { $requirements[$type] = 0; } $perMinute = 60 / $recipes[$type]['time']; $requirements[$k] = $requirements[$k] + ($numAssemblers * $v * $perMinute); return $requirements; } } else { foreach($recipes[$type]['in'] as $k => $v) { $depReq = solveFor($recipes, $k, 1, $requirements); foreach ($depReq as $k2 => $v2) { $requirements[$k2] += $v2; } } return $requirements; } } //print_r( solveFor($recipes, 'cc', 4, []) ); print_r( solveFor($recipes, 'gc', 4, []) );
Output for 7.2.11
Notice: Undefined index: c in /in/drfRL on line 39 Notice: Undefined index: cc in /in/drfRL on line 46 Notice: Undefined index: c in /in/drfRL on line 46 Notice: Undefined index: i in /in/drfRL on line 34 Notice: Undefined index: i in /in/drfRL on line 43 Warning: Invalid argument supplied for foreach() in /in/drfRL on line 43 Array ( [cc] => 0 [c] => 240 )