diff options
Diffstat (limited to '11/src/stuff.rs')
-rw-r--r-- | 11/src/stuff.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/11/src/stuff.rs b/11/src/stuff.rs new file mode 100644 index 0000000..82540ef --- /dev/null +++ b/11/src/stuff.rs @@ -0,0 +1,48 @@ +use core::cmp::{max, min}; +use std::io::{BufRead}; + +pub fn read_helper<'a, 'b, R: BufRead>(r: &'a mut R, buf: &'b mut Vec<u8>, token: u8) -> std::io::Result<usize> { + buf.clear(); + let num_bytes = r.read_until(token, buf)?; + if buf.last() == Some(& token) { buf.pop(); } + Ok(num_bytes) +} + +pub fn is_galaxy(c: u8) -> bool { + c == b'#' +} + +pub type Position = [usize; 2]; + +pub fn cumulative_sum_expansion(doubling: & [bool], factor: usize) -> Vec<usize> { + let mut ret: Vec<usize> = Vec::with_capacity(doubling.len()); + + let mut sum: usize = 0; + for &x in doubling.into_iter() { + if x { + sum += factor; + } + else { + sum += 1; + } + ret.push(sum); + } + + ret +} + +pub fn distance(cumul_rows: & [usize], cumul_cols: & [usize], p: & Position, q: & Position) -> usize { + (cumul_rows[max(p[0], q[0])] - cumul_rows[min(p[0], q[0])]) + + (cumul_cols[max(p[1], q[1])] - cumul_cols[min(p[1], q[1])]) +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test() { + } + +} |