initial commit
This commit is contained in:
commit
658f3bfca5
31 changed files with 7570 additions and 0 deletions
1
03-binary-diagnostic/.gitignore
vendored
Normal file
1
03-binary-diagnostic/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
7
03-binary-diagnostic/Cargo.lock
generated
Normal file
7
03-binary-diagnostic/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "binary-diagnostic"
|
||||
version = "0.1.0"
|
8
03-binary-diagnostic/Cargo.toml
Normal file
8
03-binary-diagnostic/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "binary-diagnostic"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
19
03-binary-diagnostic/LICENSE
Normal file
19
03-binary-diagnostic/LICENSE
Normal file
|
@ -0,0 +1,19 @@
|
|||
The MIT License (MIT)
|
||||
Copyright (c) 2021 danielyrovas
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1000
03-binary-diagnostic/diagnostics.txt
Normal file
1000
03-binary-diagnostic/diagnostics.txt
Normal file
File diff suppressed because it is too large
Load diff
62
03-binary-diagnostic/src/main.rs
Normal file
62
03-binary-diagnostic/src/main.rs
Normal 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())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue