Guest User

Untitled

a guest
Dec 8th, 2015
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. int hash(char destination[3]) {
  8.     return destination[0]-'a'+1+(destination[1]==0?0:destination[1]-'a'+1)*26;
  9. }
  10.  
  11. int main() {
  12.     uint16_t circuits[2*27+1];
  13.     char operation[20], destination[3];
  14.  
  15.     for(int i=0;i<27*27+1;i++) {
  16.         circuits[i]=0;
  17.     }
  18.  
  19.     while(scanf("%[^\n-] -> %[^\n]\n",&operation, &destination) != EOF) {
  20.        
  21.         //printf("\n%s::%d", destination, hash(destination));
  22.         char * sauce;
  23.         //printf("\n%s::destination::%s\n\t/destination-value::%d", operation, destination, circuits[hash(destination)]);
  24.         sauce = strtok(operation, " ");
  25.         //printf("\n\tfirst::%s", sauce);
  26.  
  27.         char * source;
  28.         if(strcmp("NOT",sauce)==0) {
  29.             // NOT aa/1 -> bb
  30.             sauce = strtok(NULL, " ");
  31.         //  printf("\nNOT:\tfactor::%s,", sauce);
  32.             uint16_t temp = (isdigit(sauce[0]) ? atoi(sauce) : circuits[hash(sauce)]);
  33.         //  printf("%d,", temp);
  34.             circuits[hash(destination)] = ~temp;
  35.         //  printf("%d,", circuits[hash(destination)]);
  36.             continue;
  37.         } else {
  38.             // aa/1 * -> bb
  39.             source = sauce;
  40.         }
  41.        
  42.         sauce = strtok(NULL, " ");
  43.        
  44.         uint16_t val = (isdigit(source[0]) ? atoi(source) : circuits[hash(source)]);
  45.    
  46.         if(sauce == NULL) {
  47.             // aa -> bb
  48.             circuits[hash(destination)] = val;
  49.             continue;
  50.         }
  51.  
  52.         char * factor = strtok(NULL, " ");
  53.         uint16_t right = (isdigit(factor[0]) ? atoi(factor) : circuits[hash(factor)]);
  54.         if(strcmp("AND",sauce)==0) {
  55.             circuits[hash(destination)] = val & right;
  56.         } else if(strcmp("OR",sauce)==0) {
  57.             circuits[hash(destination)] = val | right;
  58.         } else if(strcmp("LSHIFT",sauce)==0) {
  59.             circuits[hash(destination)] = val << right;    
  60.         } else if(strcmp("RSHIFT",sauce)==0) {
  61.             circuits[hash(destination)] = val >> right;
  62.         }
  63.     }
  64.     printf("\n%d", circuits[hash("a")]);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment