CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:villsukka
Submission time:2024-10-29 14:23:21 +0200
Language:Rust (2021)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED30
#2ACCEPTED70
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2details
#2ACCEPTED0.00 s1, 2details
#3ACCEPTED0.00 s1, 2details
#4ACCEPTED0.00 s1, 2details
#5ACCEPTED0.00 s1, 2details
#6ACCEPTED0.00 s1, 2details
#7ACCEPTED0.00 s1, 2details
#8ACCEPTED0.00 s1, 2details
#9ACCEPTED0.00 s1, 2details
#10ACCEPTED0.00 s1, 2details
#11ACCEPTED0.00 s1, 2details
#12ACCEPTED0.00 s2details
#13ACCEPTED0.00 s2details
#14ACCEPTED0.00 s2details
#15ACCEPTED0.00 s2details
#16ACCEPTED0.00 s2details
#17ACCEPTED0.00 s2details
#18ACCEPTED0.01 s2details
#19ACCEPTED0.01 s2details
#20ACCEPTED0.00 s2details
#21ACCEPTED0.01 s2details
#22ACCEPTED0.01 s2details
#23ACCEPTED0.01 s2details
#24ACCEPTED0.00 s2details

Code

#[cfg(test)]
mod tests;

use std::{io::stdin, ops::Add};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum RoomType {
    Robot,
    Coin,
    Empty,
}

#[derive(Debug, Clone, Copy)]
enum Ordering {
    Less,
    Greater,
    Equal,
    BothInvalid,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Direction {
    Left,
    Right,
}

struct Pointer {
    pos: usize,
    max_len: usize,
    direction: Direction,
}

impl Pointer {
    ///True Means Incremented
    fn try_increment(&mut self) -> bool {
        let num = match self.direction {
            Direction::Left => self.pos.checked_sub(1),
            Direction::Right => {
                if self.pos + 1 <= self.max_len {
                    Some(self.pos.add(1))
                } else {
                    None
                }
            }
        };

        match num {
            Some(x) => {
                self.pos = x;
                true
            }
            None => false,
        }
    }

    fn cmp_with_start(&self, other: &Self, start_pos: usize) -> Ordering {
        let dist_to_self = self.dist_from_start(start_pos);
        let dist_to_other = other.dist_from_start(start_pos);

        match (self.is_edged(), other.is_edged()) {
            (true, true) => Ordering::BothInvalid,
            (true, false) => Ordering::Greater,
            (false, true) => Ordering::Less,
            (false, false) => match dist_to_self.cmp(&dist_to_other) {
                std::cmp::Ordering::Less => Ordering::Less,
                std::cmp::Ordering::Equal => Ordering::Equal,
                std::cmp::Ordering::Greater => Ordering::Greater,
            },
        }
        //Add comparison which fails or sum when is edged
    }

    fn dist_from_start(&self, start_pos: usize) -> usize {
        match self.direction {
            Direction::Left => start_pos - self.pos,
            Direction::Right => self.pos - start_pos,
        }
    }

    fn is_edged(&self) -> bool {
        match self.direction {
            Direction::Left => self.pos <= 0,
            Direction::Right => self.pos >= self.max_len,
        }
    }
}

impl From<char> for RoomType {
    fn from(value: char) -> Self {
        match value {
            'R' => RoomType::Robot,
            '*' => RoomType::Coin,
            '.' => RoomType::Empty,
            _ => panic!(),
        }
    }
}

fn main() {
    let mut line = String::new();

    stdin().read_line(&mut line).unwrap();
    let len: usize = line.strip_suffix('\n').unwrap_or(&line).parse().unwrap();

    line = "".to_string();

    stdin().read_line(&mut line).unwrap();

    let (steps, coins) = do_challenge(line, len);

    println!("{} {}", steps, coins)
}

fn do_challenge(line: String, len: usize) -> (usize, i32) {
    let mut arr: Vec<RoomType> = line
        .strip_suffix('\n')
        .unwrap()
        .chars()
        .map(|c| c.into())
        .collect();

    let mut robot_pos = arr
        .iter()
        .position(|x| *x == RoomType::Robot)
        .expect("No robot");

    let mut coin_count = 0;
    let mut steps = 0;

    let mut lp = Pointer {
        pos: robot_pos,
        max_len: len - 1,
        direction: Direction::Left,
    };
    let mut rp = Pointer {
        direction: Direction::Right,
        ..lp
    };

    loop {
        match lp.cmp_with_start(&rp, robot_pos) {
            Ordering::Less => {
                lp.try_increment();
            }
            Ordering::Greater => {
                rp.try_increment();
            }
            Ordering::Equal => {
                lp.try_increment();
                rp.try_increment();
            }
            Ordering::BothInvalid => break,
        }

        match (arr[lp.pos] == RoomType::Coin, arr[rp.pos] == RoomType::Coin) {
            (true, true) => break,
            (true, false) => {
                steps += lp.dist_from_start(robot_pos);
                robot_pos = lp.pos;
                coin_count += 1;
                arr[lp.pos] = RoomType::Empty;
            }
            (false, true) => {
                steps += rp.dist_from_start(robot_pos);
                robot_pos = rp.pos;
                coin_count += 1;
                arr[rp.pos] = RoomType::Empty;
            }
            (false, false) => continue,
        }
    }

    (steps, coin_count)
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
1
R

correct output
0 0

user output
0 0

Test 2

Group: 1, 2

Verdict: ACCEPTED

input
10
...R......

correct output
0 0

user output
0 0

Test 3

Group: 1, 2

Verdict: ACCEPTED

input
10
**.R...***

correct output
12 5

user output
12 5

Test 4

Group: 1, 2

Verdict: ACCEPTED

input
10
***R******

correct output
0 0

user output
0 0

Test 5

Group: 1, 2

Verdict: ACCEPTED

input
1000
R................................

correct output
947 9

user output
947 9

Test 6

Group: 1, 2

Verdict: ACCEPTED

input
1000
.................................

correct output
886 9

user output
886 9

Test 7

Group: 1, 2

Verdict: ACCEPTED

input
1000
.....*..*....**..**..*......*....

correct output
1287 400

user output
1287 400

Test 8

Group: 1, 2

Verdict: ACCEPTED

input
1000
************.*****************...

correct output
0 0

user output
0 0

Test 9

Group: 1, 2

Verdict: ACCEPTED

input
1000
******************************...

correct output
0 0

user output
0 0

Test 10

Group: 1, 2

Verdict: ACCEPTED

input
1000
R*****************************...

correct output
999 999

user output
999 999

Test 11

Group: 1, 2

Verdict: ACCEPTED

input
1000
******************************...

correct output
999 999

user output
999 999

Test 12

Group: 2

Verdict: ACCEPTED

input
10000
.......**........*...........*...

correct output
10971 999

user output
10971 999

Test 13

Group: 2

Verdict: ACCEPTED

input
10000
*..*....*......*.....*..*........

correct output
9999 999

user output
9999 999

Test 14

Group: 2

Verdict: ACCEPTED

input
10000
*.*.*...**.*...*....**.**.**.....

correct output
18766 5000

user output
18766 5000

Test 15

Group: 2

Verdict: ACCEPTED

input
10000
R*****************************...

correct output
9999 9999

user output
9999 9999

Test 16

Group: 2

Verdict: ACCEPTED

input
10000
******************************...

correct output
9999 9999

user output
9999 9999

Test 17

Group: 2

Verdict: ACCEPTED

input
200000
.................................

correct output
0 0

user output
0 0

Test 18

Group: 2

Verdict: ACCEPTED

input
200000
.................................

correct output
299934 10000

user output
299934 10000

Test 19

Group: 2

Verdict: ACCEPTED

input
200000
**.***....**..**.....***.*..*....

correct output
299998 100000

user output
299998 100000

Test 20

Group: 2

Verdict: ACCEPTED

input
200000
******************************...

correct output
0 0

user output
0 0

Test 21

Group: 2

Verdict: ACCEPTED

input
200000
R................................

correct output
133765 3

user output
133765 3

Test 22

Group: 2

Verdict: ACCEPTED

input
200000
R................................

correct output
199982 5000

user output
199982 5000

Test 23

Group: 2

Verdict: ACCEPTED

input
200000
R*****************************...

correct output
199999 199999

user output
199999 199999

Test 24

Group: 2

Verdict: ACCEPTED

input
200000
******************************...

correct output
199999 199999

user output
199999 199999