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
use std::{collections::HashSet, fmt::Display};

use super::{direction::Direction, path::Path, point::Point};

pub struct SandProducer {
    entry: Point,
    current: Point,
    //TODO: optimize using BTreeSet
    blocked_points: HashSet<Point>,
    sand: Vec<Point>,
    abyss: usize,
    floor: Option<usize>,
}

impl SandProducer {
    pub fn new<P: Into<Point>>(point: P, paths: &Vec<Path>) -> Self {
        let mut blocked_points = HashSet::new();

        for p in paths.iter() {
            let k = p.filler();
            blocked_points.extend(k.0)
        }

        let max_val = paths
            .iter()
            .flat_map(|p| p.0.iter())
            .max_by(|p1, p2| p1.y.cmp(&p2.y))
            .map(|p| p.y)
            .unwrap_or(0);
        let point = point.into();
        SandProducer {
            entry: point,
            current: point,
            blocked_points,
            sand: Vec::new(),
            abyss: max_val + 3,
            floor: None,
        }
    }

    pub fn new_with_floor<P: Into<Point>>(point: P, paths: &Vec<Path>) -> Self {
        let mut blocked_points = HashSet::new();

        for p in paths.iter() {
            let k = p.filler();
            blocked_points.extend(k.0)
        }

        let max_val = paths
            .iter()
            .flat_map(|p| p.0.iter())
            .max_by(|p1, p2| p1.y.cmp(&p2.y))
            .map(|p| p.y)
            .unwrap_or(0);
        let point = point.into();
        SandProducer {
            entry: point,
            current: point,
            blocked_points,
            sand: Vec::new(),
            abyss: max_val + 3,       // if max is 9 then this is 12
            floor: Some(max_val + 2), // if max is 9 then this 11
        }
    }

    pub fn display<'a>(&'a self, widths: (usize, usize)) -> DisplayBox<'a> {
        DisplayBox {
            producer: self,
            widths,
        }
    }

    fn is_blocked_by_path(&self, p: &Point) -> bool {
        if let Some(floor) = self.floor {
            if p.y >= floor {
                return true;
            }
        }
        self.blocked_points.contains(p)
    }

    fn is_point_blocked(&self, p: &Point) -> bool {
        self.sand.binary_search(p).is_ok() || self.is_blocked_by_path(p)
    }

    fn has_reached_abyss(&self, p: &Point) -> bool {
        self.abyss < p.y
    }
}

impl Iterator for SandProducer {
    type Item = Point;

    fn next(&mut self) -> Option<Self::Item> {
        if self.has_reached_abyss(&self.current) {
            return None;
        }
        // sand can't produce anymore because entry is blocked
        if self.is_point_blocked(&self.entry) {
            return None;
        }

        let mut new_current = self.current.clone();
        while !self.is_point_blocked(&new_current) && !self.has_reached_abyss(&new_current) {
            let mut tmp = Direction::Down.move_point(&new_current);
            if self.is_point_blocked(&tmp) {
                tmp = Direction::DownLeft.move_point(&new_current);
            }
            if self.is_point_blocked(&tmp) {
                tmp = Direction::DownRight.move_point(&new_current);
            }
            if self.is_point_blocked(&tmp) {
                break;
            }
            new_current = tmp;
        }
        if self.has_reached_abyss(&new_current) {
            self.current = new_current;
            return None;
        }

        let i = self.sand.binary_search(&new_current).unwrap_or_else(|e| e);
        self.sand.insert(i, new_current);

        self.current = self.entry.clone();
        Some(new_current)
    }
}

//TODO: with numbers i.e. rows and columns numbers
pub struct DisplayBox<'a> {
    producer: &'a SandProducer,
    widths: (usize, usize),
}

impl<'a> Display for DisplayBox<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let start_x = self
            .producer
            .entry
            .x
            .checked_sub(self.widths.0)
            .unwrap_or(0);
        let end_x = self.producer.entry.x + (self.widths.1);
        for y in self.producer.entry.y..=self.producer.entry.y + self.producer.abyss + 2 {
            for x in start_x..=end_x {
                let p: Point = (x, y).into();
                let c = match p {
                    p if self.producer.sand.contains(&p) => 'o',
                    p if p == self.producer.entry => '+',
                    p if self.producer.is_blocked_by_path(&p) => '#',
                    _ => '.',
                };
                write!(f, "{}", c)?;
            }
            writeln!(f, "")?;
        }

        Ok(())
    }
}