CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:osmukka
Submission time:2024-11-06 10:40:07 +0200
Language:C
Status:COMPILE ERROR

Compiler report

input/code.c: In function 'main':
input/code.c:66:19: warning: implicit declaration of function 'distance_left' [-Wimplicit-function-declaration]
   66 |     int d_left  = distance_left(rooms, robot_index);
      |                   ^~~~~~~~~~~~~
input/code.c:67:19: warning: implicit declaration of function 'distance_right' [-Wimplicit-function-declaration]
   67 |     int d_right = distance_right(rooms, robot_index, amount);
      |                   ^~~~~~~~~~~~~~
input/code.c:51:5: warning: ignoring return value of 'read' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     read(STDIN_FILENO, &rooms, 1);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: output/cc4y45qn.o: in function `main':
code.c:(.text.startup+0xe3): undefined reference to `distance_left'
/usr/bin/ld: code.c:(.text.startup+0xf6): undefined reference to `distance_right'
/usr/bin/ld: code.c:(.text.startup+0x139): undefined reference to `distance_left'
/usr/bin/ld: code.c:(.text.startup+0x...

Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/*int distance_left(char* array, int robot_index)
{

    for(int i = robot_index-1; i >= 0; i--)
    {
        if(array[i] == '*')
        {
            return robot_index - i;
        }
    }
    // If no coins exist to the left. Distance between two indices can't be -1.
    return -1;
}


int distance_right(char* array, int robot_index, const int array_len)
{
    for(int i = array_len-robot_index; i <= array_len; i--)
    {
        if(array[i] == '*')
        {
            return i - robot_index;
        }
    }
    // Distance cant be negative.
    return -1;
}*/

int main()
{
    printf("hhh");
    char ch;
    int number_index = 0;
    char amount_str[7];
    int amount = 0;

    while(read(STDIN_FILENO, &ch, 1) > 0)
    {
        amount_str[number_index] = ch;
        number_index++;
    }
    amount_str[number_index] = 0;
    amount = atoi(amount_str);

    char* rooms = malloc(sizeof(char)*amount);
    read(STDIN_FILENO, &rooms, 1);

    int robot_index = 0;
    int steps = 0;
    int coins = 0;

    for(int i = 0; i < amount; i++)
    {
        if(rooms[i] == 'R')
        {
            robot_index = i;
            break;
        }
    }

    int d_left  = distance_left(rooms, robot_index);
    int d_right = distance_right(rooms, robot_index, amount);

    while(1)
    {
        if(d_left == 0)
            d_left = distance_left(rooms, robot_index);
        else if(d_right == 0)
            d_right = distance_right(rooms, robot_index, amount);

        if(d_left < d_right)
        {
            rooms[robot_index] = '.';
            robot_index--;
            rooms[robot_index] = 'R';
            steps += d_left;

            d_right += d_left;
            coins++;
            d_left = 0;
        }
        else if(d_right < d_left)
        {
            rooms[robot_index] = '.';
            robot_index++;
            rooms[robot_index] = 'R';

            steps += d_right;
            coins++;
            d_right = 0;
        }
        else
        {
            break;
        }
    }
 
    printf("%d %d", steps, coins);
    return 0;
}