CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:Ez
Submission time:2024-11-05 13:12:14 +0200
Language:Rust (2021)
Status:COMPILE ERROR

Compiler report

error[E0384]: cannot assign twice to immutable variable `oikea`
  --> input/code.rs:32:34
   |
28 |     let oikea:usize = if O{10000000}else{t.iter().skip(r).position(|c| *c=='*').unwrap_or(10000000)};
   |         -----
   |         |
   |         first assignment to `oikea`
   |         help: consider making this binding mutable: `mut oikea`
...
32 |     if oikea>1000000{O=true}else{oikea = r + oikea}
   |                                  ^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0384`.

Code

use std::{process::exit, io::{BufRead, BufReader}};
static mut COINS: i32 = 0;
static mut STEPS: usize = 0;
static mut V: bool = false;
static mut O: bool = false;
static mut VI: usize = 0;
static mut OI: usize = 0;

fn main() {
    let mut input = BufReader::new(std::io::stdin());
    let mut line = "".to_string();
    input.read_line(&mut line).unwrap();
    let mut split = line.split_whitespace();
    let n: usize = split.next().unwrap().parse().unwrap();
    let mut robot: usize = 0;
    line = "".to_string();    input.read_line(&mut line).unwrap();
    let mut t: Vec<char> = line.chars().filter(|a| *a=='*'||*a=='.'||*a=='R').collect();
    for i in t.iter().enumerate(){if *i.1=='R'{robot = i.0;}}
    unsafe{
        OI = t.iter().skip(robot).position(|c| *c=='*').unwrap_or(10000000);
        VI = t.iter().rev().skip(n-robot-1).position(|c| *c=='*').unwrap_or(10000000)
    }
    loop{
    unsafe{robot = step(n, &mut t,robot)}}
}
unsafe fn step(n: usize, t: &mut Vec<char>, r: usize) -> usize{
    t[r]='.';
    let oikea:usize = if O{10000000}else{t.iter().skip(r).position(|c| *c=='*').unwrap_or(10000000)};
    let mut vasen:usize = if V{10000000}else{t.iter().rev().skip(n-r-1).position(|c| *c=='*').unwrap_or(10000000)};
    if vasen == oikea ||( vasen>1000000 && oikea>1000000){print!("{} {}",STEPS, COINS); exit(0)}
    if vasen>1000000{V=true}else{vasen = r - vasen}
    if oikea>1000000{O=true}else{oikea = r + oikea}
    if vasen < oikea{ 
        t[vasen]='R';
        STEPS+=r - vasen;
        COINS +=1 ;
        vasen
    }else{
        t[oikea]='R';
        STEPS+=oikea-r;
        COINS +=1 ;
        oikea
    }
}