summaryrefslogtreecommitdiff
path: root/day7/src/main.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-12-09 21:27:45 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-12-09 21:36:02 +0100
commit41ebee263d44442c4b304b9417bedf878f752981 (patch)
treeb68fe6682390984c48f1b68e8c2c5472a8037046 /day7/src/main.rs
parent772a3db1831d4ccfed172e659b92db47cba7ee70 (diff)
Add day7
Diffstat (limited to 'day7/src/main.rs')
-rw-r--r--day7/src/main.rs29
1 files changed, 29 insertions, 0 deletions
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::<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(())
+}