Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <ctype.h>
- int hash(char destination[3]) {
- return destination[0]-'a'+1+(destination[1]==0?0:destination[1]-'a'+1)*26;
- }
- int main() {
- uint16_t circuits[2*27+1];
- char operation[20], destination[3];
- for(int i=0;i<27*27+1;i++) {
- circuits[i]=0;
- }
- while(scanf("%[^\n-] -> %[^\n]\n",&operation, &destination) != EOF) {
- //printf("\n%s::%d", destination, hash(destination));
- char * sauce;
- //printf("\n%s::destination::%s\n\t/destination-value::%d", operation, destination, circuits[hash(destination)]);
- sauce = strtok(operation, " ");
- //printf("\n\tfirst::%s", sauce);
- char * source;
- if(strcmp("NOT",sauce)==0) {
- // NOT aa/1 -> bb
- sauce = strtok(NULL, " ");
- // printf("\nNOT:\tfactor::%s,", sauce);
- uint16_t temp = (isdigit(sauce[0]) ? atoi(sauce) : circuits[hash(sauce)]);
- // printf("%d,", temp);
- circuits[hash(destination)] = ~temp;
- // printf("%d,", circuits[hash(destination)]);
- continue;
- } else {
- // aa/1 * -> bb
- source = sauce;
- }
- sauce = strtok(NULL, " ");
- uint16_t val = (isdigit(source[0]) ? atoi(source) : circuits[hash(source)]);
- if(sauce == NULL) {
- // aa -> bb
- circuits[hash(destination)] = val;
- continue;
- }
- char * factor = strtok(NULL, " ");
- uint16_t right = (isdigit(factor[0]) ? atoi(factor) : circuits[hash(factor)]);
- if(strcmp("AND",sauce)==0) {
- circuits[hash(destination)] = val & right;
- } else if(strcmp("OR",sauce)==0) {
- circuits[hash(destination)] = val | right;
- } else if(strcmp("LSHIFT",sauce)==0) {
- circuits[hash(destination)] = val << right;
- } else if(strcmp("RSHIFT",sauce)==0) {
- circuits[hash(destination)] = val >> right;
- }
- }
- printf("\n%d", circuits[hash("a")]);
- }
Advertisement
Add Comment
Please, Sign In to add comment