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:
parent
4f4ab5d900
commit
6aae31f295
@ -5,39 +5,80 @@ fn main() {
|
|||||||
|
|
||||||
let report: Vec<&str> = input.lines().collect();
|
let report: Vec<&str> = input.lines().collect();
|
||||||
let bit_count = report[0].len();
|
let bit_count = report[0].len();
|
||||||
let col_size = report.len();
|
|
||||||
|
|
||||||
let mut gamma = 0;
|
{
|
||||||
let mut epsilon = 0;
|
// Part 1
|
||||||
|
let col_size = report.len();
|
||||||
|
|
||||||
for position in 0..bit_count {
|
let mut gamma = 0;
|
||||||
gamma <<= 1;
|
let mut epsilon = 0;
|
||||||
epsilon <<= 1;
|
|
||||||
|
|
||||||
let count = report
|
for position in 0..bit_count {
|
||||||
.iter()
|
gamma <<= 1;
|
||||||
.filter(|b| b.chars().nth(position).unwrap() == '1')
|
epsilon <<= 1;
|
||||||
.count();
|
|
||||||
|
|
||||||
if count > col_size / 2 {
|
let count = report
|
||||||
gamma += 1;
|
.iter()
|
||||||
} else {
|
.filter(|b| b.chars().nth(position).unwrap() == '1')
|
||||||
epsilon += 1;
|
.count();
|
||||||
|
|
||||||
|
if count > col_size / 2 {
|
||||||
|
gamma += 1;
|
||||||
|
} else {
|
||||||
|
epsilon += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
println!("Part 1: {}", gamma * epsilon);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Part 2
|
||||||
|
let generator: i32 = {
|
||||||
|
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() == '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);
|
||||||
}
|
}
|
||||||
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!();
|
|
||||||
// }
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user