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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::{borrow::Cow, cmp::Ordering, fmt::Display, str::FromStr};

use advent_of_code::errors::Error;

#[derive(PartialEq, Eq, Debug, Clone)]
pub enum PacketData {
    Value(i32),
    List(Vec<PacketData>),
}

impl PacketData {
    /// Creates a clone on write which will clone the underlying data when it is mutably borrowed
    /// or ownership is needed of the vector of packetData.
    ///
    /// This won't do anything if the borrow is immutable which we need for our iterator
    ///
    /// TODO: understand other ways to accomplish `Cow` i.e. `Rc`
    pub fn into_list(&self) -> Cow<Vec<PacketData>> {
        match self {
            Self::Value(v) => Cow::Owned(Vec::from([PacketData::Value(*v)])),
            Self::List(v) => Cow::Borrowed(v),
        }
    }
}

impl FromStr for PacketData {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut chars = s.chars();
        let mut cur_value = String::new();
        let mut cur: Option<PacketData> = None;
        let mut stack: Vec<PacketData> = Vec::new();

        while let Some(c) = chars.next() {
            match c {
                ']' => {
                    if !cur_value.is_empty() {
                        cur = Some(PacketData::Value(cur_value.parse()?));
                        cur_value.clear();
                    }
                    if let Some(PacketData::List(last)) = stack.last_mut() {
                        if let Some(cur) = cur {
                            last.push(cur);
                        }
                    }
                    cur = stack.pop();
                }
                '[' => stack.push(PacketData::List(Vec::new())),
                ' ' => {}
                ',' => {
                    if !cur_value.is_empty() {
                        cur = Some(PacketData::Value(cur_value.parse()?));
                        cur_value.clear();
                    }
                    if let Some(PacketData::List(last)) = stack.last_mut() {
                        if let Some(cur) = cur {
                            last.push(cur);
                        }
                    }
                    cur = None;
                }
                c => cur_value.push(c),
            };
            // println!("({}) | {:?} | {:?} | ({})", c, cur, stack, cur_value);
            if stack.is_empty() {
                break;
            }
        }

        cur.ok_or(Error::InvalidParseError(
            "Couldn't get Packet Data for: ".to_owned() + s,
        ))
    }
}

impl Ord for PacketData {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        //TODO: non-recursive solution
        match (self, other) {
            // left < right means that self > other or left compared to right in the right order
            (Self::Value(left), Self::Value(right)) => left.cmp(right).reverse(),
            (left, right) => {
                let left = left.into_list();
                let right = right.into_list();
                let mut left = left.iter();
                let mut right = right.iter();

                loop {
                    match (left.next(), right.next()) {
                        (Some(left), Some(right)) => match left.cmp(right) {
                            Ordering::Equal => {}
                            o => return o,
                        },
                        (None, None) => return Ordering::Equal,
                        (None, Some(_)) => return Ordering::Greater,
                        (Some(_), None) => return Ordering::Less,
                    }
                }
            }
        }
    }
}

impl PartialOrd for PacketData {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Display for PacketData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Value(v) => write!(f, "{}", v),
            Self::List(l) => {
                let mut s = String::from("[");
                for v in l {
                    s.push_str(&format!("{}", v));
                    s.push(',');
                }
                s.pop();
                s.push(']');
                write!(f, "{}", s)
            }
        }
    }
}

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

    #[test]
    fn invalid_packet() {
        let res = "".parse::<PacketData>();
        assert!(res.is_err())
    }

    #[test]
    fn empty_list_packet() {
        let res = "[]".parse::<PacketData>().unwrap();
        assert!(matches!(res, PacketData::List(_)))
    }

    #[test]
    fn simple_packet() {
        let res = "[1, 2]".parse::<PacketData>().unwrap();
        match res {
            PacketData::List(l) => {
                assert_eq!(l, Vec::from([PacketData::Value(1), PacketData::Value(2)]))
            }
            _ => assert!(false),
        };
    }

    #[test]
    fn more_complex_packet() {
        let res = "[[0, 1], 2]".parse::<PacketData>().unwrap();
        let expected = PacketData::List(Vec::from([
            PacketData::List(Vec::from([PacketData::Value(0), PacketData::Value(1)])),
            PacketData::Value(2),
        ]));
        assert_eq!(res, expected);
    }
}