summaryrefslogtreecommitdiff
path: root/2021/day7/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-12-05 19:46:06 +0100
committerSanto Cariotti <santo@dcariotti.me>2022-12-05 19:46:06 +0100
commitcf6303a5bc1558ebdb7b467da38f74cd3ac3a9b1 (patch)
tree6292cb239a8cf114179c6e7c8b3015840dfbae6e /2021/day7/src
parent6e10cc2773fcaff64902b13f44443da014f38be7 (diff)
Add 2022
Diffstat (limited to '2021/day7/src')
-rw-r--r--2021/day7/src/main.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/2021/day7/src/main.rs b/2021/day7/src/main.rs
new file mode 100644
index 0000000..26e1e30
--- /dev/null
+++ b/2021/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(())
+}