use std::{
io::{self, BufRead},
iter,
};
const DAY_COUNT: i32 = 5;
const PREDICTION_STRENGTH: f64 = -0.02;
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");
reader
.lines()
.map(|line| predict_day(&line.unwrap()))
.collect()
}
fn predict_day(line: &str) -> Vec<Option<f64>> {
let mut temperatures = line
.split_whitespace()
.map(|x| x.parse::<f64>().unwrap())
.skip(22);
let (a, b) = (temperatures.next().unwrap(), temperatures.next().unwrap());
let d = b - a;
(1..=DAY_COUNT)
.map(|i| Some(b + PREDICTION_STRENGTH * f64::from(i) * d))
.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
}
}