AfanasevGad4

This is a task for our favourite professor
git clone git://git.stellar-nexus.ru/AfanasevGad4
Log | Files | Refs

guesser_2.c (2329B)


      1 //dogwater code written at night. This sucks, please don't use it
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4  
      5 #define NUMBER_AMOUNT	4
      6 #define MAX_GUESS_3	9999	//this
      7 #define MAX_GUESS_2	999	//shit
      8 #define MAX_GUESS_1	99	//sucks
      9  
     10 static int
     11 uniq(unsigned int n)
     12 {
     13 	int seen = 0, digits = 0;
     14 	
     15 	if (n <= MAX_GUESS_2 && n > MAX_GUESS_1) {
     16 		seen |= 1 << 0;
     17 		++digits;
     18 	}
     19 	
     20 	do {
     21 		int d = n % 10;
     22 		int bit = 1 << d;
     23 		if (seen & bit)
     24 			return 0;
     25 		seen |= bit;
     26 		n /= 10;
     27 		++digits;
     28 	} while (n > 0);
     29 	
     30 	return digits == NUMBER_AMOUNT;
     31 }
     32 
     33 static unsigned int
     34 bulls(unsigned int guess, unsigned int their_guess)
     35 {
     36 	unsigned int count = 0;
     37 
     38 	unsigned int guess_digits[NUMBER_AMOUNT] = {0};
     39 	unsigned int their_digits[NUMBER_AMOUNT] = {0};
     40 
     41 	for (int i = NUMBER_AMOUNT - 1; i >= 0; --i) {
     42 		guess_digits[i]	  = guess % 10;
     43 		their_digits[i] = their_guess % 10;
     44 		guess /= 10;
     45 		their_guess /= 10;
     46 	}
     47 
     48 	for (int i = 0; i < NUMBER_AMOUNT; i++) {
     49 		if (guess_digits[i] == their_digits[i])
     50 			++count;
     51 	}
     52 
     53 	return count;
     54 }
     55 
     56 static unsigned int
     57 cows(unsigned int guess, unsigned int their_guess)
     58 {
     59 	unsigned int mask_guess = 0;
     60 	unsigned int mask_their = 0;
     61 
     62 	if (guess == 0)
     63 		mask_guess |= (1U << 0);
     64 	if (their_guess == 0)
     65 		mask_their |= (1U << 0);
     66 
     67 	if (guess < MAX_GUESS_2 + 1)
     68 		mask_guess |= (1U << 0);
     69 	if (their_guess < MAX_GUESS_2 + 1)
     70 		mask_their |= (1U << 0);
     71 
     72 	unsigned int tmp = guess;
     73 	while (tmp > 0) {
     74 		mask_guess |= (1U << (tmp % 10));
     75 		tmp /= 10;
     76 	}
     77 
     78 	tmp = their_guess;
     79 	while (tmp > 0) {
     80 		mask_their |= (1U << (tmp % 10));
     81 		tmp /= 10;
     82 	}
     83 
     84 	unsigned int intersection = mask_guess & mask_their;
     85 
     86 
     87 	unsigned int count = 0;
     88 	while (intersection) {
     89 		intersection &= (intersection - 1);
     90 		++count;
     91 	}
     92 
     93 	return count - bulls(guess, their_guess);
     94 }
     95 
     96 
     97 int
     98 main(int argc, char *argv[])
     99 {
    100 	char tmp[NUMBER_AMOUNT + 1];
    101 	unsigned int their_guess, guess = atoi(argv[1]);
    102 	do {
    103 		for (int i = 0; i < NUMBER_AMOUNT; ++i)
    104 			tmp[i] = getchar();
    105 		tmp[NUMBER_AMOUNT] = '\0';
    106 		
    107 		int c = getchar();
    108 		if (c != '\n' && c != EOF)
    109 			ungetc(c, stdin);
    110 		
    111 		their_guess = atoi(tmp);
    112 		if (their_guess <= MAX_GUESS_1 || uniq(their_guess) == 0)
    113 			exit(1);
    114 			//die("retarded guessing");
    115 		
    116 		printf("%u", bulls(guess, their_guess));
    117 		printf("%u", cows(guess, their_guess));
    118 	} while (bulls(guess, their_guess) != NUMBER_AMOUNT);
    119 }