Table of content

What is this :grey_question:

The rules of the game are explained in my original post.

33rd Challenge

Challenge

Today I’m solving today’s AoC day 3 2021. So far only part one is done. Part two coming soon.. :sweat:.

Solution :white_check_mark:

	fn main() {

	    let x:Vec<Vec<_>> = DIAGNOSTIC_REPORT.lines()
	        .map(|l| {
	            l.as_bytes()
	                .into_iter()
	                .map(|x|*x == b'1')
	                .collect()
	        })
	        .collect();

	    let length = x.len() as u32;

	    let bit_count = x
	        .iter()
	        .fold(vec![0; length as usize], |acc:Vec<u32>, inst|{
	        acc
	            .iter().zip(inst.iter())
	            .map(|x| {match x.1
	                {
	                    true =>{ *x.0 + 1},
	                    false => *x.0
	            }})
	            .collect()
	    });

	    println!("{:?}",bit_count);

	    let gamma= bit_count.iter()
	        .map(|x|{ (*x > length/2) as u32})
	        .rev()
	        .enumerate()
	        .fold(0,|acc, inst|{
	            acc | (inst.1 << (inst.0 as u32))
	            }
	        );

	    let epsilon = (!gamma) & 0b111111111111;
	    println!("{}*{} = {}",gamma,epsilon,gamma*epsilon);

	}

	const DIAGNOSTIC_REPORT:&str ="00100
	11110
	10110
	10111
	10101
	01111
	00111
	11100
	10000
	11001
	00010
	01010";
	

To see the full source see github and see the live demo in playground.

Saying part two was a hassle is an under statement. The solution outputs to binary vector which need to be multiplied. I did that manually as I lost motivation in part two :star: unlocked! Using transpose helps, starting to feel like this AOC stuff would be easier in Matlab / octave.

	fn main() {

	    let mut input: Vec<Vec<u32>> = DIAGNOSTIC_REPORT.lines()
	        .map(|l| {
	            l.as_bytes()
	                .into_iter()
	                .map(|x| (*x == b'1') as u32)
	                .collect()
	        })
	        .collect();

	    let mut a:usize = 0;

	    let mut x = input.clone();

	    while (*x).len()>1 {
	        let bit_count = find_most_common_bits_per_row(x.clone());
	        x.retain(|x|{x[a] == bit_count[a]});
	        a+= 1;
	    }

	    println!("{:?}",x);

	    let mut a:usize = 0;
	    while (*input).len()>1 {
	        let bit_count = find_most_common_bits_per_row(input.clone());
	        input.retain(|x|{x[a] != bit_count[a]});
	        a+= 1;
	    }

	    println!("{:?}",input);

	}


	fn find_most_common_bits_per_row(x: Vec<Vec<u32>>) -> Vec<u32> {
	    let length = x.len() as u32;
	    let x = transpose(x);
	    let bit_count: Vec<u32> = x.iter()
	        .map(|x| {
	            x.iter().fold(0, |acc, inst| { acc + inst })
	        })
	        .map(|x| { (x as f32 >= (length as f32 / 2.0)) as u32 })
	        .collect();
	    bit_count
	}

	/// Source : https://stackoverflow.com/questions/64498617/how-to-transpose-a-vector-of-vectors-in-rust
	fn transpose<T>(v: Vec<Vec<T>>) -> Vec<Vec<T>> {
	    assert!(!v.is_empty());
	    let len = v[0].len();
	    let mut iters: Vec<_> = v.into_iter().map(|n| n.into_iter()).collect();
	    (0..len)
	        .map(|_| {
	            iters
	                .iter_mut()
	                .map(|n| n.next().unwrap())
	                .collect::<Vec<T>>()
	        })
	        .collect()
	}


	const DIAGNOSTIC_REPORT:&str ="00100
	11110
	10110
	10111
	10101
	01111
	00111
	11100
	10000
	11001
	00010
	01010";
	

github part 2 playground part 2