add day 16
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const INPUT: usize = 10000;
|
||||
#[cfg(debug_assertions)]
|
||||
const LENGTH: usize = 20;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const INPUT: usize = 10011111011011001;
|
||||
#[cfg(not(debug_assertions))]
|
||||
const LENGTH: usize = 272;
|
||||
const LENGTH2: usize = 35651584;
|
||||
|
||||
fn main() {
|
||||
calculate_checksum(LENGTH);
|
||||
calculate_checksum(LENGTH2);
|
||||
}
|
||||
|
||||
fn calculate_checksum(length: usize) {
|
||||
let mut a = INPUT.to_string();
|
||||
while a.len() < length {
|
||||
let mut b = a.clone();
|
||||
b = b
|
||||
.chars()
|
||||
.rev()
|
||||
.map(|c| if c == '0' { '1' } else { '0' })
|
||||
.collect();
|
||||
|
||||
let mut result = a.clone();
|
||||
result.push('0');
|
||||
result.push_str(&b);
|
||||
a = result;
|
||||
}
|
||||
a.truncate(length);
|
||||
|
||||
loop {
|
||||
let possible_checksum: String = a
|
||||
.chars()
|
||||
.tuples()
|
||||
.map(|(c0, c1)| c0 == c1)
|
||||
.map(|b| if b { '1' } else { '0' })
|
||||
.collect();
|
||||
if possible_checksum.len() % 2 == 1 {
|
||||
a = possible_checksum;
|
||||
break;
|
||||
}
|
||||
a = possible_checksum;
|
||||
}
|
||||
println!("{a}");
|
||||
}
|
||||
Reference in New Issue
Block a user