<?php
$input = "123 -> x
456 -> y
x AND y -> d
x OR y -> e
x LSHIFT 2 -> f
y RSHIFT 2 -> g
NOT x -> h
NOT y -> i";
$inputs = explode("\n", $input);
function operation($s){
$matchesand = null;
$and = preg_match("/(.{1,2})\sAND\s(.{1,2})\s->\s(.{1,2})/",$s,$matchesand);
if(count($matchesand)>0){
return [1,$matchesand[1] , $matchesand[2], $matchesand[3]];
}
$matchesor= null;
$or = preg_match("/(.{1,2})\sOR\s(.{1,2})\s->\s(.{1,2})/",$s,$matchesor);
if(count($matchesor)>0){
return [2,$matchesor[1] , $matchesor[2], $matchesor[3]];
}
$matchesnot = null;
$not = preg_match("/NOT\s(.{1,2})\s->\s(.{1,2})/",$s,$matchesnot);
if(count($matchesnot)>0){
return [3,$matchesnot[1], $matchesnot[2]];
}
$matchesleft = null;
$left = preg_match("/(.{1,2})\sLSHIFT\s(.{1,2})\s->\s(.{1,2})/",$s,$matchesleft);
if(count($matchesleft)>0){
return [4,$matchesleft[1] , $matchesleft[2], $matchesleft[3]];
}
$matchesright = null;
$right = preg_match("/(.{1,2})\sRSHIFT\s(.{1,2})\s->\s(.{1,2})/",$s,$matchesright);
if(count($matchesright)>0){
return [5,$matchesright[1] , $matchesright[2], $matchesright[3]];
}
$matches = null;
$simple = preg_match("/(.{1,5})\s->\s(.{1,2})/",$s,$matches);
if(count($matches)>0){
return [6,$matches[1], $matches[2]];
}
}
$array=[];
for($i = 'a'; $i<'z'; $i++){
$array[$i]=0;
}
for($i = 'a'; $i<'z'; $i++){
for($j = 'a'; $j<'z'; $j++){
$array[$i.$j]=0;
}
}
for($i = 0; $i<count($inputs); $i++){
$values = operation($inputs[$i]);
//var_dump($values);
if($values[0] === 1){
if(intval($values[1])>0)
$array[$values[3]] = (int)$values[1] & $array[$values[2]];
else
$array[$values[3]] = $array[$values[1]] & $array[$values[2]];
}
if($values[0] === 2){
$array[$values[3]] = $array[$values[1]] | $array[$values[2]];
}
if($values[0] === 3){
$array[$values[2]] = ~$array[$values[1]];
}
if($values[0] === 4){
$array[$values[3]] = $array[$values[1]]<<intval($values[2]);
}
if($values[0] === 5){
$array[$values[3]] = $array[$values[1]]>>intval($values[2]);
}
if($values[0] === 6){
$array[$values[2]] = intval($values[1]);
}
//$array[$var] = $val;
}
var_dump($array);
?>
preferences:
44.39 ms | 402 KiB | 5 Q