mirror of
https://gitlab.com/MisterBiggs/aoc2021.git
synced 2025-07-27 08:21:22 +00:00
day3 part1
This commit is contained in:
day3
1000
day3/input.txt
Normal file
1000
day3/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
8
day3/rust/Cargo.toml
Normal file
8
day3/rust/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
43
day3/rust/src/main.rs
Normal file
43
day3/rust/src/main.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let input = fs::read_to_string("../input.txt").expect("Could not read file");
|
||||
|
||||
let report: Vec<&str> = input.lines().collect();
|
||||
let bit_count = report[0].len();
|
||||
let col_size = report.len();
|
||||
|
||||
let mut gamma = 0;
|
||||
let mut epsilon = 0;
|
||||
|
||||
for position in 0..bit_count {
|
||||
gamma <<= 1;
|
||||
epsilon <<= 1;
|
||||
|
||||
let count = report
|
||||
.iter()
|
||||
.filter(|b| b.chars().nth(position).unwrap() == '1')
|
||||
.count();
|
||||
|
||||
if count > col_size / 2 {
|
||||
gamma += 1;
|
||||
} else {
|
||||
epsilon += 1;
|
||||
}
|
||||
}
|
||||
println!("{}", gamma * epsilon);
|
||||
// part1(input);
|
||||
// part2(&commands);
|
||||
}
|
||||
|
||||
// fn part1(report: String) {
|
||||
// for i in 1..5 {
|
||||
// let (ones, zeros): (Vec<&str>, Vec<&str>) =
|
||||
// report.lines().partition(|l| l[i..l.len()].starts_with('1'));
|
||||
// println!("{:?}", zeros);
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn part2(commands: &Vec<(&str, i32)>) {
|
||||
// todo!();
|
||||
// }
|
Reference in New Issue
Block a user