<?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 [$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 [$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 [~$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 [$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 [$matchesright[1] >> $matchesright[2], $matchesright[3]];
}
$matches = null;
$simple = preg_match("/(.{1,5})\s->\s(.{1,2})/",$s,$matches);
if(count($matches)>0){
return [$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++){
$var = operation($inputs[$i])[1];
$val = operation($inputs[$i])[0];
$array[$var] = $val;
}
var_dump($array);
?>
preferences:
41.56 ms | 402 KiB | 5 Q