rustle-game 0.1.0

Play wordle and nerdle in your terminal
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use std::cmp;
use rand::Rng;
use rand::seq::SliceRandom; // 0.7.2


// Terms
// nod: num of digits


// Notes
// Generate equation by end to start


#[derive(Debug, PartialEq, Copy, Clone)]
struct Bounds {
    min: i32,
    max: i32,
}

impl Bounds {

    fn from_nod(nod: i32) -> Bounds {
        Bounds {
            min: (10 as i32).pow((nod - 1) as u32) - (if nod == 1 {1} else {0}),
            max: (10 as i32).pow(nod as u32) - 1,
        }
    }

    fn to_borrowed(&self) -> Bounds {
        Bounds {
            min: self.min,
            max: self.max
        }
    }

    fn print(&self) {
        println!("Min: {}, Max: {}", self.min, self.max);
    }

    /// Returns bounds that the target and source bounds share
    fn intersect(&self, b: &Bounds) -> Bounds {
        Bounds {
            min: cmp::max(self.min, b.min),
            max: cmp::min(self.max, b.max),
        }
    }

    fn union_many(bounds: &Vec<&Bounds>) -> Bounds {
        let min = bounds.into_iter().map(|b| {b.min}).min().unwrap();
        let max = bounds.into_iter().map(|b| {b.max}).max().unwrap();
        Bounds {
            min,
            max
        }
    }

    fn union(&self, b: &Bounds) -> Bounds {
        Bounds {
            min: cmp::min(self.min, b.min),
            max: cmp::max(self.max, b.max),
        }
    }

    fn flip_non_valid(&self) -> Bounds {
        Bounds {
            min: cmp::min(self.min, self.max),
            max: cmp::max(self.min, self.max),
        }
    }

    fn make_positive(&self) -> Bounds {
        Bounds {
            min: cmp::max(self.min, 0),
            max: cmp::max(self.max, 0),
        }
    }

    fn is_valid(&self) -> bool {
        self.min <= self.max
    }

    fn random_value(&self) -> i32 {
        rand::thread_rng().gen_range(self.min..self.max + 1)
    }
}

#[derive(Clone)]
struct Expression {
    nods: Vec<i32>,
}

#[derive(Clone, Copy)]
enum Op {
    Add,
    Subtract,
    Multiply,
    Divide
}

pub trait Operation {
    fn get_restricted_bounds(answer_bounds: &Bounds, rest_bounds: &Bounds) -> Bounds;
    fn undo_op();
}

// pub struct AddOperation { }
// impl Operation for AddOperation {
//     fn get_restricted_bounds(answer_bounds: &Bounds, rest_bounds: &Bounds) -> Bounds {
//             let restricted_add_bounds_1 = Bounds {
//                 min: answer_bounds.min - rest_bounds.min,
//                 max: answer_bounds.max - rest_bounds.min,
//             }.flip_non_valid();

//             let restricted_add_bounds_2 = Bounds {
//                 min: answer_bounds.min - rest_bounds.max,
//                 max: answer_bounds.max - rest_bounds.max,
//             }.flip_non_valid();

//             Bounds::union_many(&vec![&restricted_add_bounds_1, &restricted_add_bounds_2])
//     }
// }

// pub struct SubtractOperation { }
// impl Operation for SubtractOperation {
//     fn get_restricted_bounds(answer_bounds: &Bounds, rest_bounds: &Bounds) -> Bounds {
//         let restricted_sub_bounds_1 = Bounds {
//             min: rest_bounds.min - answer_bounds.min,
//             max: rest_bounds.min - answer_bounds.max,
//         }.flip_non_valid();

//         let restricted_sub_bounds_2 = Bounds {
//             min: rest_bounds.max - answer_bounds.min,
//             max: rest_bounds.max - answer_bounds.max,
//         }.flip_non_valid();

//         Bounds::union_many(&vec![&restricted_sub_bounds_1, &restricted_sub_bounds_2])
//     }
// }


impl Expression {

    fn to_borrowed(&self) -> Expression {
        Expression {
            nods: self.nods.to_owned(),
        }
    }

    fn gen_all_of_length(len: i32) -> Vec<Expression> {
        let mut exps = vec![];

        let mut queue = vec![];

        for n in 1..len-1 {
            let ex = Expression {
                nods: vec![n]
            };
            queue.push(ex);
        }

        // Len 6
        // First Gen [ [1], [2], [3], [4] ]
        // Second Gen [ [1,1], [1,2], [1,3]  ]

        loop {
            let mut next_gen_queue = vec![];
            for ex in queue.to_owned() {
                let current_len = ex.char_length();
                let left = len - current_len;
                for nod in 1..left {
                    let new_exp = ex.append(nod);
                    let new_exp_len = new_exp.char_length();

                    if  new_exp_len == len {
                        exps.push(new_exp.clone());
                    }
                    if new_exp_len < len {
                        next_gen_queue.push(new_exp);
                    }
                }
            }
            if next_gen_queue.len() == 0 {
                break;
            }
            queue = next_gen_queue
        }
        exps.to_owned()
    }

    // Generate random structure of size len
    fn from_length(len: i32) -> Option<Expression> {
        let all_exp = Expression::gen_all_of_length(len);
        let t = all_exp.choose(&mut rand::thread_rng());
        match t {
            Some(i) => Some(i.to_owned()),
            None => None
        }
    }

    fn append(&self, nod: i32) -> Expression {
        let mut new = Expression {
            nods: self.nods.to_owned()
        };
        new.nods.push(nod);
        new
    }

    fn get_str(&self) -> String {
        let mut output = String::new();
        for nod in self.nods.to_owned() {
            output.push_str(&"x".repeat(nod as usize));
            output.push_str("*");
        }
        output.pop();
        output
    }

    fn char_length(&self) -> i32 {
        let mut sum = 0;
        for nod in self.nods.to_owned() {
            sum += nod
        }
        sum + (self.nods.len() as i32) - 1
    }

    fn output_bounds(&self) -> Bounds {
        if self.nods.len() == 0 {
            print!("Malformed Equality");
        }

        if self.nods.len() == 1 {
            return Bounds::from_nod(self.nods[0]);
        }

        fn op_output_bounds(bounds1: Bounds, bounds2: Bounds) -> Bounds {
            let add_bounds = Bounds {
                min: bounds1.min + bounds2.min,
                max: bounds1.max + bounds2.max,
            };
            let sub_bounds = Bounds {
                min: bounds1.min - bounds2.max,
                max: bounds1.max - bounds2.min,
            };
            let mul_bounds = Bounds {
                min: bounds1.min * bounds2.min,
                max: bounds1.max * bounds2.max,
            };
            Bounds::union_many(&vec![&add_bounds, &sub_bounds, &mul_bounds])
        }

        let mut b1 = Bounds::from_nod(self.nods[0]);
        for i in 1..self.nods.len() {
            b1 = op_output_bounds(b1, Bounds::from_nod(self.nods[i]));
        }
        b1
    }

    // Remove the first {amount} of elements from self
    fn slice(&self, amount: usize) -> Expression {
        Expression {
            nods: self.nods[0..amount].to_vec()
        }
    }

    fn gen_rand(&self, answer_bounds: Bounds) -> Option<Equation> {
        // Generate a number in the chosen_value_bounds
        let mut rng = rand::thread_rng();

        let mut filled_eq = Equation {
            numbers: vec![],
            ops: vec![],
        };

        let mut cum_answer_bounds = answer_bounds.to_borrowed();

        for i in 0..self.nods.len()-1 {

            // Get the nod from the end of the array
            let nod = self.nods[self.nods.len()-(i+1)];

            let rest = self.slice((self.nods.len() - (i+1)) as usize);

            let rest_bounds = rest.output_bounds();

            // Generate a number and op that when applied to rest will yeld a number in the answer range

            let output_bounds = Bounds::from_nod(nod);

            let restricted_sub_bounds_1 = Bounds {
                min: rest_bounds.min - cum_answer_bounds.min,
                max: rest_bounds.min - cum_answer_bounds.max,
            }.flip_non_valid();

            let restricted_sub_bounds_2 = Bounds {
                min: rest_bounds.max - cum_answer_bounds.min,
                max: rest_bounds.max - cum_answer_bounds.max,
            }.flip_non_valid();

            let restricted_add_bounds_1 = Bounds {
                min: cum_answer_bounds.min - rest_bounds.min,
                max: cum_answer_bounds.max - rest_bounds.min,
            }.flip_non_valid();

            let restricted_add_bounds_2 = Bounds {
                min: cum_answer_bounds.min - rest_bounds.max,
                max: cum_answer_bounds.max - rest_bounds.max,
            }.flip_non_valid();

            let restricted_mul_bounds_1 = Bounds {
                min: if rest_bounds.min != 0 {
                    cum_answer_bounds.min / rest_bounds.min
                } else {
                    // Doesn't matter since we are multiplying by 0
                    i32::MIN
                },
                max: if rest_bounds.max != 0 {
                    cum_answer_bounds.max / rest_bounds.max
                } else {
                    i32::MAX
                }
            }.flip_non_valid();

            let restricted_mul_bounds_2 = Bounds {
                min: if rest_bounds.max != 0 {
                    cum_answer_bounds.min / rest_bounds.max
                } else {
                    // Doesn't matter since we are multiplying by 0
                    i32::MIN
                },
                max: if rest_bounds.min != 0 {
                    cum_answer_bounds.max / rest_bounds.min
                } else {
                    i32::MAX
                }
            }.flip_non_valid();

            let restricted_add_bounds = Bounds::union_many(&vec![&restricted_add_bounds_1, &restricted_add_bounds_2]).intersect(&output_bounds);
            let restricted_sub_bounds = Bounds::union_many(&vec![&restricted_sub_bounds_1, &restricted_sub_bounds_2]).intersect(&output_bounds);
            let restricted_mul_bounds = Bounds::union_many(&vec![&restricted_mul_bounds_1, &restricted_mul_bounds_2]).intersect(&output_bounds);

            let mut possible_op_bounds = vec![];

            if restricted_add_bounds.is_valid() {
               possible_op_bounds.push((restricted_add_bounds, Op::Add));
            }

            if restricted_sub_bounds.is_valid() {
                possible_op_bounds.push((restricted_sub_bounds, Op::Subtract));
            }

            if restricted_mul_bounds.is_valid() {
                possible_op_bounds.push((restricted_mul_bounds, Op::Multiply));
            }

            if possible_op_bounds.len() == 0 {
                return None;
            }

            // Choose random bounds from possible
            let (chosen_bounds, op) = possible_op_bounds.choose(&mut rng).unwrap();

            let num = chosen_bounds.random_value();

            // Undo the operation to the answer bounds
            cum_answer_bounds = match op {
                Op::Add => Bounds {
                    min: cum_answer_bounds.min - num,
                    max: cum_answer_bounds.max - num,
                },
                Op::Subtract => Bounds {
                    min: cum_answer_bounds.min + num,
                    max: cum_answer_bounds.max + num,
                },
                Op::Multiply => Bounds {
                    min: if num != 0 {cum_answer_bounds.min / num} else {0},
                    max: if num !=0 {cum_answer_bounds.max / num} else {0},
                },
                _ => panic!("Invalid op")
            };

            // Ensure the answer bounds are positive
            cum_answer_bounds = cum_answer_bounds.make_positive();


            filled_eq.numbers.push(num);

            filled_eq.ops.push(*op);
        }

        let nod = self.nods[0];
        let end_bounds = cum_answer_bounds.intersect(&Bounds::from_nod(nod));

        if !end_bounds.is_valid() {
            return None;
        }

        let num = end_bounds.random_value();
        filled_eq.numbers.push(num);

        // We filled the array backwards so it is flipped
        filled_eq.numbers.reverse();

        Some(filled_eq)
    }

}

struct Equation {
    numbers: Vec<i32>,
    ops: Vec<Op>
}

impl Equation {
    fn get_str(&self) -> String {
        let mut eq_str = String::new();
        for i in 0..self.numbers.len() {
            let num = self.numbers[i];
            let op = self.ops.get(i);
            eq_str.push_str(&num.to_string());
            match op {
                Some(Op::Add) => eq_str.push_str("+"),
                Some(Op::Subtract) => eq_str.push_str("-"),
                Some(Op::Multiply) => eq_str.push_str("*"),
                None => eq_str.push_str("?"),
                _ => (),
            };
        }
        eq_str.pop();
        eq_str.push_str("=");
        eq_str.push_str(&self.calc_value().to_string());
        eq_str
    }

    fn calc_value(&self) -> i32 {
        let mut val = self.numbers[0];
        for i in 1..self.numbers.len() {
            let num = self.numbers[i];
            let op = self.ops.get(i-1);
            match op {
                Some(Op::Add) => val += num,
                Some(Op::Subtract) => val -= num,
                Some(Op::Multiply) => val *= num,
                None => (),
                _ => (),
            };
        }
        val
    }

    fn try_make_random(len: u8) -> Option<Equation> {
        let mut rng = rand::thread_rng();

        let answer_max_nod: u8 = 3;

        let answer_nod = rng.gen_range(1..answer_max_nod+1);
        // let answer_nod = 1;

        let answer_bounds = Bounds::from_nod(answer_nod as i32);

        let nod_left = len - (answer_nod+1);

        let expression = Expression::from_length(nod_left as i32);

        // let expression = Some(Expression {
        //     nods: vec![1,1,1]
        // });

        match expression {
            Some(exp) => {
                println!("{}={}", exp.get_str(), "x".to_string().repeat(answer_nod as usize));
                exp.gen_rand(answer_bounds)
            }
            None => None
        }
    }

    fn make_random(len: u8) -> Equation {
        loop {
            match Equation::try_make_random(len) {
                Some(eq) => return eq,
                None => (),
            }
        }
    }
}

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

    #[test]
    fn nod_bounds() {
        let b1 = Bounds::from_nod(1);
        assert_eq!(b1, Bounds {
            min: 0,
            max: 9
        });
        let b2 = Bounds::from_nod(2);
        assert_eq!(b2, Bounds {
            min: 10,
            max: 99
        });
    }

    #[test]
    fn bounds_intersect() {
        let b1 = Bounds { min: 4, max: 6};
        let b2 = Bounds { min: 3, max: 5};
        let b3 = Bounds { min: 6, max: 9};

        let b4 = b1.intersect(&b2);
        let b5 = b2.intersect(&b3);

        assert_eq!(b4.min, 4);
        assert_eq!(b4.max, 5);
        assert_eq!(b5.is_valid(), false);
    }

    #[test]
    fn bounds_union() {
        let b1 = Bounds { min: 4, max: 6};
        let b2 = Bounds { min: 3, max: 5};

        let b4 = b1.union(&b2);

        assert_eq!(b4.min, 3);
        assert_eq!(b4.max, 6);
    }
}

pub fn play(n: u8) -> String {
    let eq = Equation::make_random(n);
    eq.get_str()
}