summaryrefslogtreecommitdiff
path: root/11/src/stuff.rs
blob: 82540ef951702bf6e3bdfe904beeb54239b417f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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() {
    }

}