Compare commits
No commits in common. "b2c281e19cf2937f0a3b37cbe0e340810d7a7335" and "faf02e34fa61942d3c3f4e59f6425c179bb99a0f" have entirely different histories.
b2c281e19c
...
faf02e34fa
|
@ -1,55 +1,9 @@
|
|||
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_part1(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)
|
||||
fn solve_part2(input: &str) -> i32 {
|
||||
todo!();
|
||||
}
|
||||
|
||||
pub fn run(input: String) {
|
||||
|
@ -62,16 +16,15 @@ pub fn run(input: String) {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
const EXAMPLE_INPUT: &str =
|
||||
"00100\n11110\n10110\n10111\n10101\n01111\n00111\n11100\n10000\n11001\n00010\n01010";
|
||||
const EXAMPLE_INPUT: &str = todo!();
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
assert_eq!(super::solve_part1(EXAMPLE_INPUT), 198);
|
||||
todo!();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
assert_eq!(super::solve_part2(EXAMPLE_INPUT), 230);
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
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!();
|
||||
}
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
mod day_01;
|
||||
mod day_02;
|
||||
mod day_03;
|
||||
mod day_04;
|
||||
|
||||
pub const INPUT_PATH: &str = "./input/2021/day4.txt";
|
||||
pub const INPUT_PATH: &str = "./input/2021/day3.txt";
|
||||
|
||||
pub fn run(input: String) {
|
||||
day_04::run(input);
|
||||
day_03::run(input);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue