Pagini recente » Cod sursa (job #2788222) | Cod sursa (job #977575) | Cod sursa (job #1411621) | Cod sursa (job #1453061) | Cod sursa (job #3135724)
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, Error, Write};
#[derive(Debug)]
pub struct Input {
a: u64,
b: u64,
}
#[derive(Debug)]
pub struct Output {
sum: u64,
}
pub fn read_input(filepath: &str) -> Input {
let reader = match File::open(filepath) {
Ok(f) => BufReader::new(f),
Err(e) => panic!("Failed opening input file: {}", e.to_string()),
};
let input = reader
.lines()
.into_iter()
.map(|r| r.map(|s| s.parse().unwrap()))
.collect::<Vec<Result<u64, Error>>>();
match input.as_slice() {
[Ok(a), Ok(b)] => Input { a: *a, b: *b },
_ => panic!("Ill formatted input file"),
}
}
pub fn write_output(filepath: &str, output: &Output) {
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(filepath)
.expect("Failed opening output file");
file
.write(&output.sum.to_string().as_bytes())
.expect("Failed writing to output file");
}
pub fn solve(input: Input) -> Output {
Output { sum: input.a + input.b }
}
fn main() {
let input = read_input("adunare.in");
write_output("adunare.out", &solve(input));
}