summaryrefslogtreecommitdiff
path: root/08/src/stuff.rs
blob: 835c65eccb79e74472f4430f40f47d5c2af3ef60 (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 std::io::{BufRead};

pub fn ascii_to_u64(s: & [u8]) -> u64 {
    #[cfg(debug_assertions)]
    {
        assert!(s.into_iter().all(|& c| c.is_ascii_digit()), "Not an ASCII base-10 number");
    }

    // SAFETY: AoC is ASCII-only.
    unsafe { std::str::from_utf8_unchecked(s) }.parse().unwrap()
}

pub fn is_all_ascii_ws<'a, I: IntoIterator<Item = &'a u8>>(s: I) -> bool {
    s.into_iter().all(|& c| c.is_ascii_whitespace())
}

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)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Direction {
    L, R
}

impl TryFrom<u8> for Direction {
    type Error = &'static str;

    fn try_from(c: u8) -> Result<Self, Self::Error> {
        match c {
            b'L' => Ok(Self::L),
            b'R' => Ok(Self::R),
            _    => Err("ASCII character not a valid direction")
        }
    }
}

pub type Node = [u8; 3];


#[cfg(test)]
mod tests {
    use super::*;

}