Day 3
This commit is contained in:
parent
084c896615
commit
f5ec79b1c7
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,87 @@
|
||||||
|
pub fn day3() {
|
||||||
|
// let input = r#"
|
||||||
|
// 00100
|
||||||
|
// 11110
|
||||||
|
// 10110
|
||||||
|
// 10111
|
||||||
|
// 10101
|
||||||
|
// 01111
|
||||||
|
// 00111
|
||||||
|
// 11100
|
||||||
|
// 10000
|
||||||
|
// 11001
|
||||||
|
// 00010
|
||||||
|
// 01010
|
||||||
|
// "#;
|
||||||
|
let input = include_str!("../input/day3.txt");
|
||||||
|
|
||||||
|
let numbers: Vec<u32> = input
|
||||||
|
.trim()
|
||||||
|
.lines()
|
||||||
|
.map(|l| u32::from_str_radix(l.trim(), 2).unwrap())
|
||||||
|
.collect();
|
||||||
|
let nbits = input.trim().lines().next().unwrap().trim().len();
|
||||||
|
|
||||||
|
let gamma = gamma(&numbers);
|
||||||
|
println!("gamma: {}", gamma);
|
||||||
|
println!("epsilon: {}", epsilon(gamma, nbits));
|
||||||
|
|
||||||
|
println!("oxygen: {}", oxygen(&numbers, nbits));
|
||||||
|
println!("co2: {}", co2(&numbers, nbits));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gamma(numbers: &[u32]) -> u32 {
|
||||||
|
let mut gamma: u32 = 0;
|
||||||
|
for i in 0..32 {
|
||||||
|
gamma = gamma | (most_common_bit(numbers, i) << i);
|
||||||
|
}
|
||||||
|
gamma
|
||||||
|
}
|
||||||
|
|
||||||
|
fn most_common_bit(numbers: &[u32], pos: u32) -> u32 {
|
||||||
|
let count = numbers.iter().filter(|&n| (n >> pos) & 1 == 1).count();
|
||||||
|
let frac = (count as f32) / (numbers.len() as f32);
|
||||||
|
frac.round() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn epsilon(gamma: u32, nbits: usize) -> u32 {
|
||||||
|
println!("{:#b}", !gamma);
|
||||||
|
println!("{:#b}", 2_u32.pow(nbits as u32) - 1);
|
||||||
|
!gamma & (2_u32.pow(nbits as u32) - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn oxygen(numbers: &[u32], nbits: usize) -> u32 {
|
||||||
|
let mut vec: Vec<u32> = numbers.into();
|
||||||
|
let mut bit: u32 = nbits as u32;
|
||||||
|
while vec.len() != 1 {
|
||||||
|
bit -= 1;
|
||||||
|
let most_common = most_common_bit(&vec, bit);
|
||||||
|
vec = vec
|
||||||
|
.into_iter()
|
||||||
|
.filter(|n| ((n >> bit) & 1) == most_common)
|
||||||
|
.collect();
|
||||||
|
for n in vec.iter() {
|
||||||
|
print!("{:#b}, ", n);
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
*vec.get(0).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn co2(numbers: &[u32], nbits: usize) -> u32 {
|
||||||
|
let mut vec: Vec<u32> = numbers.into();
|
||||||
|
let mut bit: u32 = nbits as u32;
|
||||||
|
while vec.len() != 1 {
|
||||||
|
bit -= 1;
|
||||||
|
let least_common = 1 - most_common_bit(&vec, bit);
|
||||||
|
vec = vec
|
||||||
|
.into_iter()
|
||||||
|
.filter(|n| ((n >> bit) & 1) == least_common)
|
||||||
|
.collect();
|
||||||
|
// for n in vec.iter() {
|
||||||
|
// print!("{:#b}, ", n);
|
||||||
|
// }
|
||||||
|
// println!();
|
||||||
|
}
|
||||||
|
*vec.get(0).unwrap()
|
||||||
|
}
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
mod day1;
|
mod day1;
|
||||||
mod day2;
|
mod day2;
|
||||||
|
mod day3;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
day2::day2();
|
day3::day3();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue