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
use std::fmt::Display;

#[derive(Debug, Clone, Copy)]
pub enum HeightCell {
    Start,
    Elevation(char),
    End,
}

impl HeightCell {
    pub fn elevation(&self) -> u32 {
        match self {
            Self::Start => 0,
            Self::End => 'z' as u32 - 'a' as u32,
            // a starts at 1
            Self::Elevation(c) => *c as u32 - 'a' as u32,
        }
    }
}

impl Display for HeightCell {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Start => 'S',
                Self::End => 'E',
                Self::Elevation(c) => *c,
            }
        )
    }
}

impl From<char> for HeightCell {
    fn from(c: char) -> Self {
        match c {
            'S' => Self::Start,
            'E' => Self::End,
            c => Self::Elevation(c),
        }
    }
}