diff --git a/day16/Cargo.lock b/day16/Cargo.lock new file mode 100644 index 0000000..16dc480 --- /dev/null +++ b/day16/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day16" +version = "0.1.0" +dependencies = [ + "itertools", +] + +[[package]] +name = "either" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" + +[[package]] +name = "itertools" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc" +dependencies = [ + "either", +] diff --git a/day16/Cargo.toml b/day16/Cargo.toml new file mode 100644 index 0000000..e6a15e1 --- /dev/null +++ b/day16/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day16" +version = "0.1.0" +edition = "2024" + +[dependencies] +itertools = "*" diff --git a/day16/src/main.rs b/day16/src/main.rs new file mode 100644 index 0000000..a663391 --- /dev/null +++ b/day16/src/main.rs @@ -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}"); +}