summaryrefslogtreecommitdiff
path: root/day3/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-12-07 22:31:33 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-12-07 22:31:33 +0100
commit467181d7b73b9747d8ea88041187c06d12cc0adf (patch)
tree46c1a875568619f231d008e9551dc4c33b9e13bd /day3/src
parentc7e2cc9b02d040300d6516a177c9b6ed0ecaf58a (diff)
Add day3
Diffstat (limited to 'day3/src')
-rw-r--r--day3/src/main.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/day3/src/main.rs b/day3/src/main.rs
new file mode 100644
index 0000000..aa7eda7
--- /dev/null
+++ b/day3/src/main.rs
@@ -0,0 +1,36 @@
+use std::fs::File;
+use std::io::prelude::*;
+use std::io::BufReader;
+
+fn main() -> Result<(), std::io::Error> {
+ let file = File::open("input.txt")?;
+ let reader = BufReader::new(&file);
+
+ let lines: Vec<_> = reader.lines().map(|x| x.unwrap()).collect::<Vec<String>>();
+ let mut bits: Vec<u32> = vec![0; lines[0].len()];
+ let half: u32 = (lines.len() / 2) as u32;
+ let mut gamma_string = String::new();
+ let mut epsilon_string = String::new();
+
+ for line in lines {
+ for (index, char) in line.chars().enumerate() {
+ bits[index] += char.to_digit(10).unwrap();
+ }
+ }
+
+ for bit in bits {
+ if bit >= half {
+ gamma_string.push('1');
+ epsilon_string.push('0');
+ } else {
+ gamma_string.push('0');
+ epsilon_string.push('1');
+ }
+ }
+ let gamma_rate = isize::from_str_radix(&gamma_string, 2).unwrap();
+ let epsilon_rate = isize::from_str_radix(&epsilon_string, 2).unwrap();
+
+ println!("{}", gamma_rate * epsilon_rate);
+
+ Ok(())
+}