From 41ebee263d44442c4b304b9417bedf878f752981 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 9 Dec 2021 21:27:45 +0100 Subject: Add day7 --- day7/src/main.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day7/src/main.rs (limited to 'day7/src') diff --git a/day7/src/main.rs b/day7/src/main.rs new file mode 100644 index 0000000..26e1e30 --- /dev/null +++ b/day7/src/main.rs @@ -0,0 +1,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::().unwrap()) + .collect::>(); + + 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(()) +} -- cgit v1.2.3-18-g5258