AfanasevGad4

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

commit 403ef6dce2e470283c59851f444a181685bb2a65
parent 86f340e6bcb4e749ca0e7e9b49243f30bbdc73f7
Author: Plat <plat@stellar-nexus.ru>
Date:   Fri, 10 Oct 2025 23:19:29 +0000

Added the guesser program

Diffstat:
Aguesser_2.c | 119+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 119 insertions(+), 0 deletions(-)

diff --git a/guesser_2.c b/guesser_2.c @@ -0,0 +1,119 @@ +//dogwater code written at night. This sucks, please don't use it +#include <stdio.h> +#include <stdlib.h> + +#define NUMBER_AMOUNT 4 +#define MAX_GUESS_3 9999 //this +#define MAX_GUESS_2 999 //shit +#define MAX_GUESS_1 99 //sucks + +static int +uniq(unsigned int n) +{ + int seen = 0, digits = 0; + + if (n <= MAX_GUESS_2 && n > MAX_GUESS_1) { + seen |= 1 << 0; + ++digits; + } + + do { + int d = n % 10; + int bit = 1 << d; + if (seen & bit) + return 0; + seen |= bit; + n /= 10; + ++digits; + } while (n > 0); + + return digits == NUMBER_AMOUNT; +} + +static unsigned int +bulls(unsigned int guess, unsigned int their_guess) +{ + unsigned int count = 0; + + unsigned int guess_digits[NUMBER_AMOUNT] = {0}; + unsigned int their_digits[NUMBER_AMOUNT] = {0}; + + for (int i = NUMBER_AMOUNT - 1; i >= 0; --i) { + guess_digits[i] = guess % 10; + their_digits[i] = their_guess % 10; + guess /= 10; + their_guess /= 10; + } + + for (int i = 0; i < NUMBER_AMOUNT; i++) { + if (guess_digits[i] == their_digits[i]) + ++count; + } + + return count; +} + +static unsigned int +cows(unsigned int guess, unsigned int their_guess) +{ + unsigned int mask_guess = 0; + unsigned int mask_their = 0; + + if (guess == 0) + mask_guess |= (1U << 0); + if (their_guess == 0) + mask_their |= (1U << 0); + + if (guess < MAX_GUESS_2 + 1) + mask_guess |= (1U << 0); + if (their_guess < MAX_GUESS_2 + 1) + mask_their |= (1U << 0); + + unsigned int tmp = guess; + while (tmp > 0) { + mask_guess |= (1U << (tmp % 10)); + tmp /= 10; + } + + tmp = their_guess; + while (tmp > 0) { + mask_their |= (1U << (tmp % 10)); + tmp /= 10; + } + + unsigned int intersection = mask_guess & mask_their; + + + unsigned int count = 0; + while (intersection) { + intersection &= (intersection - 1); + ++count; + } + + return count - bulls(guess, their_guess); +} + + +int +main(int argc, char *argv[]) +{ + char tmp[NUMBER_AMOUNT + 1]; + unsigned int their_guess, guess = atoi(argv[1]); + do { + for (int i = 0; i < NUMBER_AMOUNT; ++i) + tmp[i] = getchar(); + tmp[NUMBER_AMOUNT] = '\0'; + + int c = getchar(); + if (c != '\n' && c != EOF) + ungetc(c, stdin); + + their_guess = atoi(tmp); + if (their_guess <= MAX_GUESS_1 || uniq(their_guess) == 0) + exit(1); + //die("retarded guessing"); + + printf("%u", bulls(guess, their_guess)); + printf("%u", cows(guess, their_guess)); + } while (bulls(guess, their_guess) != NUMBER_AMOUNT); +}