CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:Kivvil
Submission time:2024-10-28 19:55:39 +0200
Language:Rust (2021)
Status:READY
Result:30
Feedback
groupverdictscore
#1ACCEPTED30
#20
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.01 s2details
#13ACCEPTED0.01 s2details
#14ACCEPTED0.02 s2details
#15ACCEPTED0.04 s2details
#16ACCEPTED0.04 s2details
#17ACCEPTED0.00 s2details
#18ACCEPTED0.65 s2details
#19--2details
#20ACCEPTED0.00 s2details
#21ACCEPTED0.00 s2details
#22ACCEPTED0.32 s2details
#23--2details
#24--2details

Compiler report

warning: method `tulosta_huoneet` is never used
   --> input/code.rs:120:8
    |
27  | impl Rakennus {
    | ------------- method in this implementation
...
120 |     fn tulosta_huoneet(&self) {
    |        ^^^^^^^^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

Code

use std::io::{self, BufRead};
use std::vec::Vec;
fn main() {
let stdin = io::stdin();
// Luetaan vain kartta huoneista, ei tarvita ensimmäistä riviä, joten skipataan se.
let _ = stdin.lock().lines().next();
let rivi = stdin.lock().lines().next().unwrap().unwrap();
let mut rakennus = Rakennus::new(rivi);
let (askeleet, kolikot) = rakennus.pelaa();
println!("{} {}", askeleet, kolikot);
}
struct Rakennus {
// Kartta rakennuksen huoneista
huoneet: Vec<Huone>,
// Indeksi huoneet-vektoriin, jossa robotti sijaitsee
robotti_indeksi: usize,
keratyt_kolikot: u32,
askeleet: u32,
}
impl Rakennus {
fn new(syote: String) -> Self {
Self {
huoneet: syote
.chars()
.map(|merkki| Huone::from_char(merkki))
.collect(),
robotti_indeksi: syote.find("R").unwrap(),
keratyt_kolikot: 0,
askeleet: 0,
}
}
// Pelaa peliä. Palauttaa tuplen, jossa ensimmäinen askelten määrä ja toinen kerättyjen
// kolikoiden määrä
fn pelaa(&mut self) -> (u32, u32) {
// self.tulosta_huoneet();
self.pelaa_inner();
(self.askeleet, self.keratyt_kolikot)
}
// Funktio, joka pelaa peliä yksi kolikon keräys kerrallaan
fn pelaa_inner(&mut self) {
loop {
let lahin_kolikko = match self.etsi_lahin_kolikko() {
Some(kolikko) => kolikko,
None => {
return;
}
};
// Lahin kolikko vasemmalla
if lahin_kolikko < self.robotti_indeksi {
self.askeleet += (self.robotti_indeksi - lahin_kolikko) as u32;
}
// Lahin kolikko oikealla
if lahin_kolikko > self.robotti_indeksi {
self.askeleet += (lahin_kolikko - self.robotti_indeksi) as u32;
}
self.keratyt_kolikot += 1;
self.robotti_indeksi = lahin_kolikko;
self.huoneet[lahin_kolikko] = Huone::Tyhja;
// self.tulosta_huoneet();
}
}
fn etsi_lahin_kolikko(&self) -> Option<usize> {
let kolikko_vasemmalla = self.etsi_kolikko_vasemmalla();
let kolikko_oikealla = self.etsi_kolikko_oikealla();
if let None = kolikko_vasemmalla {
return kolikko_oikealla;
}
if let None = kolikko_oikealla {
return kolikko_vasemmalla;
}
let kolikko_vasemmalla = kolikko_vasemmalla.unwrap();
let kolikko_oikealla = kolikko_oikealla.unwrap();
let etaisyys_vasemmalle = self.robotti_indeksi - kolikko_vasemmalla;
let etaisyys_oikealle = kolikko_oikealla - self.robotti_indeksi;
if etaisyys_vasemmalle > etaisyys_oikealle {
Some(kolikko_oikealla)
} else if etaisyys_oikealle > etaisyys_vasemmalle {
Some(kolikko_vasemmalla)
} else {
None
}
}
fn etsi_kolikko_oikealla(&self) -> Option<usize> {
match self.huoneet[self.robotti_indeksi..self.huoneet.len()]
.iter()
.position(|huone| *huone == Huone::Kolikko)
{
Some(p) => Some(p + self.robotti_indeksi),
None => None,
}
}
fn etsi_kolikko_vasemmalla(&self) -> Option<usize> {
match self.huoneet[0..=self.robotti_indeksi]
.iter()
.rev()
.position(|huone| *huone == Huone::Kolikko)
{
Some(p) => Some(self.robotti_indeksi - p),
None => None,
}
}
fn tulosta_huoneet(&self) {
let mut string = self
.huoneet
.iter()
.map(|huone| match huone {
Huone::Tyhja => ".",
Huone::Kolikko => "*",
})
.collect::<String>();
string.replace_range(self.robotti_indeksi..self.robotti_indeksi + 1, "R");
println!("Huoneet: {}", string);
}
}
#[derive(PartialEq, Eq)]
enum Huone {
Tyhja,
Kolikko,
}
impl Huone {
fn from_char(character: char) -> Self {
match character {
'*' => Huone::Kolikko,
'.' => Huone::Tyhja,
'R' => Huone::Tyhja,
_ => panic!("Viallinen merkki stdin:ssa"),
}
}
}

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:

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

correct output
299998 100000

user output
(empty)

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:

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

correct output
199999 199999

user output
(empty)

Test 24

Group: 2

Verdict:

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

correct output
199999 199999

user output
(empty)