Compare commits

...

2 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka b2c281e19c chore: prep for day 4 2021-12-03 05:00:54 -04:00
Rekai Nyangadzayi Musuka 4a8c1d460d feat: complete day 3 2021-12-03 04:59:44 -04:00
3 changed files with 87 additions and 9 deletions

View File

@ -1,9 +1,55 @@
fn solve_part1(input: &str) -> i32 {
todo!();
fn solve_part1(input: &str) -> u32 {
let (report, bit_len) = parse_input(input);
let mut gamma: u32 = 0;
for i in 0..bit_len {
let count = report.iter().filter(|v| **v >> i & 0x01 == 0x01).count();
if count > report.len() - count {
gamma |= 1 << i
} else {
gamma &= !(1 << i)
}
}
fn solve_part2(input: &str) -> i32 {
todo!();
gamma * (!gamma & ((1 << bit_len) - 1))
}
fn solve_part2(input: &str) -> u32 {
let (oxy_gen, bit_len) = parse_input(input);
let co2_gen = oxy_gen.clone();
let left = part2_logic(oxy_gen, bit_len, true);
let right = part2_logic(co2_gen, bit_len, false);
left * right
}
fn part2_logic(mut gen: Vec<u16>, bit_len: usize, is_oxy: bool) -> u32 {
for i in 0..bit_len {
if gen.len() < 2 {
break;
}
let count = gen
.iter()
.filter(|v| **v >> (bit_len - 1 - i) & 0x01 == 0x01)
.count();
let common = (count >= gen.len() - count) as u16;
let needle = if is_oxy { 1 - common } else { common };
gen.retain(|v| *v >> (bit_len - 1 - i) & 0x01 == needle);
}
gen[0] as u32
}
fn parse_input(input: &str) -> (Vec<u16>, usize) {
let mut it = input.lines().peekable();
let size = it.peek().expect("1 > &str exists").len();
let report = it.flat_map(|s| u16::from_str_radix(s, 2).ok()).collect();
(report, size)
}
pub fn run(input: String) {
@ -16,15 +62,16 @@ pub fn run(input: String) {
#[cfg(test)]
mod tests {
const EXAMPLE_INPUT: &str = todo!();
const EXAMPLE_INPUT: &str =
"00100\n11110\n10110\n10111\n10101\n01111\n00111\n11100\n10000\n11001\n00010\n01010";
#[test]
fn part1() {
todo!();
assert_eq!(super::solve_part1(EXAMPLE_INPUT), 198);
}
#[test]
fn part2() {
todo!();
assert_eq!(super::solve_part2(EXAMPLE_INPUT), 230);
}
}

30
rust/2021/src/day_04.rs Normal file
View File

@ -0,0 +1,30 @@
fn solve_part1(input: &str) -> i32 {
todo!();
}
fn solve_part2(input: &str) -> i32 {
todo!();
}
pub fn run(input: String) {
println!("--- Part 1 ---");
println!("Answer: {}", solve_part1(&input));
println!();
println!("--- Part 2 ---");
println!("Answer: {}", solve_part2(&input))
}
#[cfg(test)]
mod tests {
const EXAMPLE_INPUT: &str = todo!();
#[test]
fn part1() {
todo!();
}
#[test]
fn part2() {
todo!();
}
}

View File

@ -1,9 +1,10 @@
mod day_01;
mod day_02;
mod day_03;
mod day_04;
pub const INPUT_PATH: &str = "./input/2021/day3.txt";
pub const INPUT_PATH: &str = "./input/2021/day4.txt";
pub fn run(input: String) {
day_03::run(input);
day_04::run(input);
}