1
0
mirror of https://gitlab.com/MisterBiggs/aoc2021.git synced 2025-06-16 06:56:48 +00:00

day 2 complete

This commit is contained in:
Anson Biggs 2021-12-04 17:14:51 -07:00
parent 4f4ab5d900
commit 6aae31f295

View File

@ -5,6 +5,9 @@ fn main() {
let report: Vec<&str> = input.lines().collect();
let bit_count = report[0].len();
{
// Part 1
let col_size = report.len();
let mut gamma = 0;
@ -25,19 +28,57 @@ fn main() {
epsilon += 1;
}
}
println!("{}", gamma * epsilon);
// part1(input);
// part2(&commands);
println!("Part 1: {}", gamma * epsilon);
}
// 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);
// }
// }
{
// Part 2
let generator: i32 = {
let mut diagnostic = report.to_vec();
// fn part2(commands: &Vec<(&str, i32)>) {
// todo!();
// }
for position in 0..bit_count {
let col_size = diagnostic.len();
let count = diagnostic
.iter()
.filter(|b| b.chars().nth(position).unwrap() == '1')
.count();
if count >= (col_size - count) {
diagnostic.retain(|b| b.chars().nth(position).unwrap() == '1');
} else {
diagnostic.retain(|b| b.chars().nth(position).unwrap() == '0');
}
if diagnostic.len() == 1 {
break;
}
}
i32::from_str_radix(diagnostic[0], 2).unwrap()
};
let scrubber = {
let mut diagnostic = report.to_vec();
for position in 0..bit_count {
let col_size = diagnostic.len();
let count = diagnostic
.iter()
.filter(|b| b.chars().nth(position).unwrap() == '0')
.count();
if count > (col_size - count) {
diagnostic.retain(|b| b.chars().nth(position).unwrap() == '1');
} else {
diagnostic.retain(|b| b.chars().nth(position).unwrap() == '0');
}
if diagnostic.len() == 1 {
break;
}
}
i32::from_str_radix(diagnostic[0], 2).unwrap()
};
println!("Part 2: {}", generator * scrubber);
}
}