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

Compiler report

input/code.c: In function 'main':
input/code.c:106:17: error: expected expression before ')' token
  106 |         else if()
      |                 ^
input/code.c:51:5: warning: ignoring return value of 'read' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     read(STDIN_FILENO, rooms, amount);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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;
        }
    }
    return 999999;
}


int distance_right(char* array, int robot_index, int array_len)
{
    for(int i = robot_index; i < array_len; i++)
    {
        if(array[i] == '*')
        {
            return i - robot_index;
        }
    }
    return 999999;
}

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

    while(read(STDIN_FILENO, &ch, 1))
    {
        if(ch == '\n')
            break;
        amount_str[number_index] = ch;
        number_index++;
    }

    amount_str[number_index] = 0;
    amount = atoi(amount_str);

    char* rooms = malloc(amount+1);
    read(STDIN_FILENO, rooms, amount);
    rooms[amount] = '\0';

    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);
    int last_move = -1;

    while(1)
    {
        switch(last_move)
        {
            case 0:
                d_left = distance_left(rooms, robot_index);
                break;
            case 1:
                d_right = distance_right(rooms, robot_index, amount);
                break;
        }
        //printf("%s, %d:%d\n", rooms, d_left, d_right);

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

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

            steps += d_right;
            coins++;
            last_move = 1;
            d_left += d_left < 999999 ? d_right : 0;
        }
        else if()
        {
            break;
        }
    }
 
    printf("%d %d", steps, coins);
    return 0;
}