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
166
167
168
169
170
171
172
173
174
175
use rand::Rng;
use std::time::{SystemTime, UNIX_EPOCH};

const DIRECTION_DIST: phf::Map<&'static str, [i32; 2]> = phf::phf_map! {
    "U" => [-1, 0],
    "D" => [1, 0],
    "L" => [0, -1],
    "R" => [0, 1]
};

const DIRECTION_LIST: [&str; 4] = ["U", "D", "L", "R"];


pub struct Puzzle {
    pub cmds_str: String,
    pub mode: usize,
    pub puzzle: Vec<Vec<i32>>,
    correct_puzzle: Vec<Vec<i32>>,
    pub start_time: u128,
    pub end_time: u128,
}

impl Puzzle {
    pub fn new(mode: usize) -> Self {
        let mut puzzle = vec![vec![0; mode]; mode];
        let mut correct_puzzle = vec![vec![0; mode]; mode];
        let mut num = 1;

        for i in 0..mode {
            for j in 0..mode {
                puzzle[i][j] = num;
                correct_puzzle[i][j] = num;
                num += 1;
            }
        }

        puzzle[mode - 1][mode - 1] = 0;
        correct_puzzle[mode - 1][mode - 1] = 0;

        let start_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards")
            .as_millis();

        let mut instance = Puzzle {
            cmds_str: String::new(),
            mode,
            puzzle,
            correct_puzzle,
            start_time,
            end_time: 0,
        };

        instance.shuffle();
        instance
    }

    fn find_0(&self) -> Option<(usize, usize)> {
        for (i, row) in self.puzzle.iter().enumerate() {
            for (j, &value) in row.iter().enumerate() {
                if value == 0 {
                    return Some((i, j));
                }
            }
        }
        None
    }

    pub fn move_tile<'a>(&'a mut self, direction: &'a str) -> &str {
        self.cmds_str.push_str(direction);
        if let Some((r, c)) = self.find_0() {
            if (r == 0 && direction == "U")
                || (r == self.mode - 1 && direction == "D")
                || (c == 0 && direction == "L")
                || (c == self.mode - 1 && direction == "R")
            {
                return "";
            }

            if let Some(&[dr, dc]) = DIRECTION_DIST.get(direction) {
                let rr = (r as i32 + dr) as usize;
                let cc = (c as i32 + dc) as usize;

                let num1 = self.puzzle[rr][cc];
                self.puzzle[r][c] = num1;
                self.puzzle[rr][cc] = 0;
                return direction;
            }
        }
        ""
    }

    pub fn check(&mut self) -> bool {
        for i in 0..self.mode {
            for j in 0..self.mode {
                if self.puzzle[i][j] != self.correct_puzzle[i][j] {
                    return false;
                }
            }
        }
        self.end_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards")
            .as_millis();
        true
    }

    pub fn move_sequence(&mut self, sequence: &str) -> bool {
        let uppercase = sequence.to_uppercase();
        self.cmds_str.clear();
        for command in uppercase.chars() {
            let command_str = command.to_string();
            let _ = self.move_tile(&command_str);

            if self.check() {
                return true;
            }
        }
        false
    }

    pub fn shuffle(&mut self) {
        let mut rng = rand::thread_rng();
        for _ in 0..1000 {
            let random_direction = DIRECTION_LIST[rng.gen_range(0..4)];
            self.move_tile(random_direction);
        }
    }

    pub fn duration(&self) -> String {
        let time_now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards")
            .as_millis();
        let duration = time_now - self.start_time;
        self.format_duration(duration)
    }

    fn format_duration(&self, duration: u128) -> String {
        let hours = duration / 3600000;
        let minutes = (duration % 3600000) / 60000;
        let seconds = (duration % 60000) / 1000;
        format!("{}:{}:{}", hours, minutes, seconds)
    }
}

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

    #[test]
    fn it_works() {
        let puzzle = Puzzle::new(4);
        for i in 0..4{
            println!("{:?}", puzzle.puzzle[i]);
        }
        println!();
        assert_eq!(puzzle.puzzle.len(), 4);
    }

    #[test]
    fn it_works_mode3() {
        let mut puzzle = Puzzle::new(3);
        for i in 0..3{
            println!("{:?}", puzzle.puzzle[i]);
        }
        println!();
        puzzle.move_tile("U");
        for i in 0..3{
            println!("{:?}", puzzle.puzzle[i]);
        }
        println!();
        assert_eq!(puzzle.puzzle.len(), 3);
    }
}