File size: 2,714 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::{Grid, GridLabel};
use std::collections::HashSet;

pub trait GridToLabel {
    fn to_grid_labels(&self) -> HashSet<GridLabel>;
}

impl GridToLabel for Grid {
    fn to_grid_labels(&self) -> HashSet<GridLabel> {
        let mut result = HashSet::<GridLabel>::new();

        if self.grid_found() {
            {
                let label = GridLabel::GridWithSomeColor;
                result.insert(label);
            }
            {
                let label = GridLabel::GridColor { color: self.grid_color() };
                result.insert(label);
            }
            {
                let label = GridLabel::GridWithMismatchesAndColor { color: self.grid_color() };
                result.insert(label);
            }
            {
                let label = GridLabel::GridWithMismatchesAndSomeColor;
                result.insert(label);
            }
        }

        if self.grid_with_mismatches_found() {
            {
                let label = GridLabel::GridWithMismatchesAndSomeColor;
                result.insert(label);
            }
            for pattern in self.patterns_full() {
                let color: u8 = pattern.color;
                {
                    let label = GridLabel::GridWithMismatchesAndColor { color };
                    result.insert(label);
                }
            }
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::arc::{Image, ImageTryCreate};

    #[test]
    fn test_10000_grid_yes() {
        // Arrange
        let pixels: Vec<u8> = vec![
            0, 5, 0,
            5, 5, 5,
            0, 5, 0,
        ];
        let input: Image = Image::try_create(3, 3, pixels).expect("image");
        let grid: Grid = Grid::analyze(&input).expect("ok");
        
        // Act
        let actual: HashSet<GridLabel> = grid.to_grid_labels();

        // Assert
        assert_eq!(actual.contains(&GridLabel::GridWithSomeColor), true);
        assert_eq!(actual.contains(&GridLabel::GridColor { color: 5 }), true);
        assert_eq!(actual.contains(&GridLabel::GridWithMismatchesAndSomeColor), true);
    }

    #[test]
    fn test_10001_grid_no() {
        // Arrange
        let pixels: Vec<u8> = vec![
            0, 1, 2,
            3, 4, 5,
            6, 7, 8,
        ];
        let input: Image = Image::try_create(3, 3, pixels).expect("image");
        let grid: Grid = Grid::analyze(&input).expect("ok");
        
        // Act
        let actual: HashSet<GridLabel> = grid.to_grid_labels();

        // Assert
        assert_eq!(actual.contains(&GridLabel::GridWithSomeColor), false);
        assert_eq!(actual.contains(&GridLabel::GridWithMismatchesAndSomeColor), false);
    }
}