blob: 26e1e308af39163659ec1f6761116206d2ea9710 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() -> std::io::Result<()> {
let file = File::open("input.txt")?;
let mut reader = BufReader::new(file);
let mut buffer = String::new();
reader.read_line(&mut buffer)?;
let mut inputs: Vec<_> = buffer
.trim()
.split(',')
.map(|x| x.parse::<i16>().unwrap())
.collect::<Vec<i16>>();
inputs.sort();
let half: usize = inputs.len() / 2;
let middle = inputs[half];
let mut sum: u32 = 0;
for input in inputs {
sum += (input - middle).abs() as u32;
}
println!("{}", sum);
Ok(())
}
|