initial commit

This commit is contained in:
Daniel Yrovas 2022-11-10 16:58:11 +11:00
commit 658f3bfca5
Signed by: danielyrovas
GPG key ID: C181BAC70BDE7C00
31 changed files with 7570 additions and 0 deletions

View file

@ -0,0 +1,62 @@
#![allow(dead_code, unused_imports)]
use std::fmt::Binary;
use std::fs::File;
use std::path::Path;
use std::io::{self, BufRead};
fn main() {
let mut data: Vec<Vec<u32>> = Vec::new();
if let Ok(lines) = read_lines("diagnostics.txt") {
let mut vecinit = false;
for line in lines {
if let Ok(l) = line {
if !vecinit {
for _ in l.chars() {
data.push(Vec::new());
}
vecinit = true;
}
//println!("{}", l);
let mut i = 0;
for c in l.chars() {
data[i].push(c as u32 - 48);
i += 1;
}
}
}
}
let mut gama = Vec::new();
for line in &data {
gama.push(
if line.iter().sum::<u32>() < data[0].len() as u32 / 2 {
0
} else {
1
}
);
}
let lifesupport = if data[0].iter().sum::<u32>() < data[0].len() as u32 / 2 {
0
} else {
1
};
let co2scrub = if data[0].iter().sum::<u32>() > data[0].len() as u32 / 2 {
0
} else {
1
};
println!("{}:{}", lifesupport,co2scrub);
let mut gamarate = 0;
let mut epsilonrate = 0;
for i in 0..gama.len() {
gamarate += (2_u32.pow(i.try_into().unwrap())) * gama[gama.len() - i - 1];
epsilonrate += (2_u32.pow(i.try_into().unwrap())) * if gama[gama.len() - i - 1] == 1 { 0 } else { 1 };
}
println!("power consumption: {}", gamarate * epsilonrate);
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path> {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}