CSES - Datatähti 2024 alku - Results
Submission details
Task:Säähavainnot
Sender:Bliz
Submission time:2023-11-07 14:24:30 +0200
Language:Rust
Status:READY
Result:60
Feedback
groupverdictscore
#1ACCEPTED59.5
Test results
testverdicttimescore
#1ACCEPTED0.01 s7.5details
#2ACCEPTED0.01 s7.75details
#3ACCEPTED0.01 s7.75details
#4ACCEPTED0.01 s7.38details
#5ACCEPTED0.01 s7.63details
#6ACCEPTED0.01 s7.25details
#7ACCEPTED0.01 s7details
#8ACCEPTED0.01 s7.25details

Code

use std::{
    io::{self, BufRead},
    iter,
};

const DAY_COUNT: usize = 6;
const PREDICTION_STRENGTH: f64 = 0.6;

fn main() {
    let reader = io::stdin().lock();
    let predictions = predict(reader);
    print_predictions(predictions);
}

fn predict<T>(mut reader: T) -> Vec<Vec<Option<f64>>>
where
    T: BufRead,
{
    let mut input = String::new();
    reader.read_line(&mut input).unwrap();

    let _n: i32 = input
        .trim()
        .parse()
        .expect("first line of input should be the number of days");

    let days = get_temperatures(reader);
    let diffs = calculate_average_diffs(&days);
    dbg!(&diffs);

    days.iter().map(|day| predict_day(day, &diffs)).collect()
}

fn get_temperatures<T>(reader: T) -> Vec<Vec<f64>>
where
    T: BufRead,
{
    reader
        .lines()
        .map(|line| {
            line.unwrap()
                .split_whitespace()
                .map(|x| x.parse::<f64>().unwrap())
                .collect()
        })
        .collect()
}

fn calculate_average_diffs(days: &Vec<Vec<f64>>) -> Vec<f64> {
    let n = days.len();
    let mut diffs = vec![0.0; 12];
    for day in days {
        diffs
            .iter_mut()
            .zip(day.iter())
            .for_each(|(diff, temp)| *diff += temp);
    }
    let avg: f64 = days
        .iter()
        .map(|day| day.iter().skip(23).next().unwrap())
        .sum();
    diffs
        .iter_mut()
        .for_each(|diff| *diff = (*diff - avg) / n as f64);
    diffs
}

fn predict_day(day: &Vec<f64>, diffs: &Vec<f64>) -> Vec<Option<f64>> {
    let base = day.iter().skip(23).next().unwrap();

    diffs
        .iter()
        .map(|diff| Some(base + PREDICTION_STRENGTH * diff))
        .take(DAY_COUNT)
        .chain(iter::repeat(None))
        .take(12)
        .collect()
}

fn print_predictions(predictions: Vec<Vec<Option<f64>>>) {
    for prediction in predictions {
        println!(
            "{}",
            prediction
                .iter()
                .map(|x| match x {
                    Some(t) => t.to_string(),
                    None => String::from("?"),
                })
                .collect::<Vec<String>>()
                .join(" ")
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{fs::File, io::BufReader};

    #[test]
    fn example_input() {
        let file =
            File::open("in.txt").expect("there should be an example input file named `in.txt`");
        let reader = BufReader::new(file);
        let predictions = predict(reader);
        print_predictions(predictions);
    }

    #[test]
    fn test_data_set() {
        const DATA_FILE_NAME: &str = "data.txt";

        let file = File::open(DATA_FILE_NAME)
            .unwrap_or_else(|_| panic!("expected to find file `{}`", DATA_FILE_NAME));
        let reader = BufReader::new(file);
        let predictions = predict(reader);

        let file = File::open(DATA_FILE_NAME)
            .unwrap_or_else(|_| panic!("expected to find file `{}`", DATA_FILE_NAME));
        let reader = BufReader::new(file);
        let measurements = get_measurements(reader);

        let score = calculate_score(predictions, measurements);
        println!("Score: {}", score);
    }

    fn get_measurements(reader: BufReader<File>) -> Vec<Vec<f64>> {
        reader
            .lines()
            .map(|line| {
                line.unwrap()
                    .split_whitespace()
                    .map(|x| x.parse().unwrap())
                    .skip(24)
                    .collect()
            })
            .collect()
    }

    fn calculate_score(predictions: Vec<Vec<Option<f64>>>, measurements: Vec<Vec<f64>>) -> f64 {
        let n = predictions.len();
        let a: f64 = predictions
            .iter()
            .flatten()
            .zip(measurements.iter().flatten())
            .map(|x| {
                f64::from(match x {
                    (Some(p), m) => (p - m).abs() < 0.75,
                    (None, _) => false,
                })
            })
            .sum();
        let b: f64 = predictions
            .iter()
            .flatten()
            .zip(measurements.iter().flatten())
            .map(|x| {
                f64::from(match x {
                    (Some(p), m) => (p - m).abs() >= 2.05,
                    (None, _) => false,
                })
            })
            .sum();
        25.0 * (a - b) / n as f64
    }
}

Test details

Test 1

Verdict: ACCEPTED

input
1000
-0.4 -0.1 -0.2 -0.3 -0.4 -0.5 ...

correct output
0.4 0.4 0.5 0.8 0.9 1.1 1.3 1....

user output
0.2679800000000034 0.261619999...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.11330000000000563,
    0.1026999999999989,
    -0.0535...

Test 2

Verdict: ACCEPTED

input
1000
2.9 2.9 2.9 2.1 2.6 2 2 2.2 2....

correct output
2.3 1.6 1.5 1.1 1 0.7 0.6 0.8 ...

user output
2.790920000000004 2.7921800000...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.3182000000000062,
    0.32030000000000564,
    0.11010...

Test 3

Verdict: ACCEPTED

input
1000
6.6 6 6.4 6 4.6 4.6 4.2 4.3 4....

correct output
10 10.9 10.3 10.1 9.1 7.3 5.7 ...

user output
10.171760000000006 10.16024000...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.11960000000001036,
    0.10040000000000691,
    -0.056...

Test 4

Verdict: ACCEPTED

input
1000
19.4 20.2 19.1 18.9 18.3 17.3 ...

correct output
18 18.2 17 17.5 17.2 16.2 12 8...

user output
17.373579999999997 17.34370000...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.28929999999999384,
    0.23950000000000726,
    0.0611...

Test 5

Verdict: ACCEPTED

input
1000
-5.7 -5.8 -5.8 -5.9 -7.1 -6.9 ...

correct output
-4.2 -4.1 -4 -3.8 -3.5 -3.2 -3...

user output
-4.350480000000006 -4.37802000...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.2491999999999889,
    0.20329999999999837,
    0.01739...

Test 6

Verdict: ACCEPTED

input
1000
14.8 14.8 15.4 12.9 11.8 9.7 9...

correct output
11.8 11 11.6 10.8 10.4 10.4 10...

user output
12.642239999999996 12.64524000...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.07039999999999418,
    0.07540000000000237,
    -0.098...

Test 7

Verdict: ACCEPTED

input
1000
0.7 1 2 1.4 0.6 -0.4 -0.9 -0.7...

correct output
-1.3 -0.5 -0.6 -1 -3.2 -7.2 -6...

user output
-1.71546000000001 -1.772759999...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.14089999999998326,
    0.04540000000000236,
    -0.082...

Test 8

Verdict: ACCEPTED

input
1000
15.1 15.3 14.9 14.4 14.4 13.7 ...

correct output
15.6 15.9 16 15.2 14.6 14.4 13...

user output
15.102660000000004 15.08531999...
Truncated

Error:
[input/code.rs:29] &diffs = [
    0.17110000000000672,
    0.14219999999999344,
    -0.052...