solution: day 16

This commit is contained in:
YK 2024-11-27 00:05:07 +03:00
parent afba7f543c
commit c8a2ebffc4
2 changed files with 45 additions and 1 deletions

View File

@ -13,7 +13,7 @@ pub mod d12;
pub mod d13;
pub mod d14;
pub mod d15;
// pub mod d16;
pub mod d16;
// pub mod d17;
// pub mod d18;
// pub mod d19;

44
src/days/d16.rs Normal file
View File

@ -0,0 +1,44 @@
#![allow(non_upper_case_globals)]
#![allow(unused_imports)]
#![allow(dead_code)]
use crate::prelude::*;
pub type I = &'static str;
pub type O = String;
fn checksum (data: &str) -> String {
data.chars().array_chunks().map(|[a, b]| if a == b { '1' } else { '0' }).collect()
}
fn _solve (data: I, disk: usize) -> O {
let mut s = data.to_owned();
while s.len() < disk {
s = format!("{}0{}", &s, s.chars().rev().map(|e| if e == '1' { '0' } else { '1' }).collect::<String>());
}
s = (&s[..disk]).to_owned();
let mut chk = checksum(&s);
while chk.len() % 2 == 0 {
chk = checksum(&chk);
}
chk
}
#[cfg(test)]
mod test {
use super::*;
const input: &'static str = "01110110101001000";
#[test]
fn silver () {
let ans = _solve(input, 272);
assert_eq!(ans, s!("11100111011101111"))
}
#[test]
fn gold () {
let ans = _solve(input, 35651584);
assert_eq!(ans, s!("10001110010000110"))
}
}