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
//! Rusty Santa
//! 
//! A small Rust library (and command-line tool) for resolving [Secret
//! Santa](https://en.wikipedia.org/wiki/Secret_Santa) assignments with additional
//! constraints.
//!
//! It is possible to add the following constraints to the random assignments:
//!
//! - Person A and B should not draw each other (e.g. for couples)
//! - Person A should not draw person B (e.g. if person B already received a gift
//!   from person A the previous year, or if person A dislikes person B)
//!
//! While this is an interesting mathematical problem and can be solved with
//! bipartite graphs and the hungarian algorithm, this library sticks to a simpler
//! approach and tries to emulate the real name drawing from a basket. Up to 1000
//! attempts are made at resolving the name assignments without conflict until the
//! algorithm fails.
#![doc(html_logo_url = "https://github.com/dbrgn/rusty-santa/raw/master/logo.png")]

#[macro_use]
extern crate log;
extern crate rand;

use std::collections::{HashMap, HashSet};

use rand::{thread_rng, Rng};

#[derive(Clone)]
struct Matrix {
    keys: Vec<String>,
    indexes: HashMap<String, usize>,
    data: Vec<Vec<bool>>,
}

impl Matrix {
    pub fn new(keys: Vec<String>) -> Self {
        // Get size of matrix
        let size = keys.len();

        // Initialize indexes lookup map
        let mut indexes = HashMap::new();
        for (i, key) in keys.iter().enumerate() {
            indexes.insert(key.clone(), i);
        }

        // Initialize data vectors
        let mut data = vec![vec![true; size]; size];

        // Disallow giving gifts to oneself
        for i in 0..size {
            data[i][i] = false;
        }

        Matrix {
            keys: keys,
            indexes: indexes,
            data: data,
        }
    }

    /// Get the matrix value at the specified coordinates.
    ///
    /// Panics if the x or y keys are invalid.
    pub fn get(&self, x: &str, y: &str) -> bool {
        let ix = self.indexes.get(x).unwrap();
        let iy = self.indexes.get(y).unwrap();
        self.data[*ix][*iy]
    }

    /// Get the matrix row at the specified key.
    ///
    /// Panics if the key is invalid.
    pub fn get_row(&self, x: &str) -> Vec<bool> {
        let ix = self.indexes.get(x).unwrap();
        self.data[*ix].clone()
    }

    /// Set the field at coordinates x/y.
    ///
    /// Panics if the x or y keys are invalid.
    pub fn set(&mut self, x: &str, y: &str, val: bool) {
        let ix = self.indexes.get(x).unwrap();
        let iy = self.indexes.get(y).unwrap();
        self.data[*ix][*iy] = val;
    }

    /// Set every value at the specified column.
    ///
    /// Panics if the key is invalid.
    pub fn set_col(&mut self, y: &str, val: bool) {
        let iy = self.indexes.get(y).unwrap();
        for row in self.data.iter_mut() {
            row[*iy] = val;
        }
    }

    /// Return whether the key is contained in the matrix.
    pub fn contains(&mut self, key: &str) -> bool {
        self.indexes.contains_key(key)
    }

    /// Return the matrix size.
    pub fn size(&self) -> usize {
        self.keys.len()
    }

    /// Return the key at the specified index.
    pub fn key_at(&self, index: usize) -> &str {
        &self.keys[index]
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Constraint {
    ExcludePair {
        a: String,
        b: String,
    },
    Exclude {
        from: String,
        to: String,
    },
}

/// A group of people that wants to draw names.
#[derive(Debug, Clone)]
pub struct Group {
    people_set: HashSet<String>,
    constraints: Vec<Constraint>,

    /// When trying to resolve group assignments, try up to `max_attempts`
    /// times until giving up.
    max_attempts: u32,
}

impl Group {
    /// Create a new `Group`.
    pub fn new() -> Self {
        Group {
            people_set: HashSet::new(),
            constraints: vec![],
            max_attempts: 1000,
        }
    }

    /// Add a name to the group.
    pub fn add(&mut self, name: String) {
        self.people_set.insert(name);
    }

    fn add_constraint(&mut self, constraint: Constraint) {
        self.constraints.push(constraint);
    }

    /// Make sure that person A does not have to give person B a gift.
    pub fn exclude(&mut self, from: String, to: String) {
        let constraint = Constraint::Exclude { from: from, to: to };
        self.add_constraint(constraint);
    }

    /// Make sure that person A and B don't have to give each other gifts.
    pub fn exclude_pair(&mut self, a: String, b: String) {
        let constraint = Constraint::ExcludePair { a: a, b: b };
        self.add_constraint(constraint);
    }

    /// Return whether the specified name is alread in the group.
    pub fn contains_name(&self, name: &str) -> bool {
        self.people_set.contains(name)
    }

    /// Run the name assignment!
    pub fn assign(&self) -> Result<Vec<(String, String)>, AssignError> {
        // Initialize the random number generator
        let mut rng = thread_rng();

        // Shuffle the people
        let mut people: Vec<String> = self.people_set.iter().cloned().collect();
        rng.shuffle(&mut people);

        'attempt: for _ in 0..self.max_attempts {

            // Initialize the gift possibility matrix
            let mut matrix = Matrix::new(people.clone());

            // Iterate over constraints, apply them to the matrix
            for constraint in self.constraints.iter() {
                match constraint {
                    &Constraint::ExcludePair{ ref a, ref b } => {
                        if !matrix.contains(a) {
                            return Err(AssignError::BadConstraint(format!("Unknown person \"{}\"", a)));
                        }
                        if !matrix.contains(b) {
                            return Err(AssignError::BadConstraint(format!("Unknown person \"{}\"", b)));
                        }
                        matrix.set(a, b, false);
                        matrix.set(b, a, false);
                    },
                    &Constraint::Exclude { ref from, ref to } => {
                        if !matrix.contains(from) {
                            return Err(AssignError::BadConstraint(format!("Unknown person \"{}\"", from)));
                        }
                        if !matrix.contains(to) {
                            return Err(AssignError::BadConstraint(format!("Unknown person \"{}\"", to)));
                        }
                        matrix.set(from, to, false);
                    }
                }
            };

            let mut assignments = vec![];
            for person in people.iter() {
                trace!("Drawing recipient for {}", person);

                // Get the possible names
                let mut basket = vec![];
                {
                    let row = matrix.get_row(&person);
                    for i in 0..row.len() {
                        if row[i] {
                            basket.push(matrix.key_at(i).to_owned());
                        }
                    }
                }
                trace!("Options: {:?}", basket);

                // Draw a random name
                if basket.is_empty() {
                    trace!("Attempt failed. Retrying...");
                    continue 'attempt;
                }
                let choice = rng.choose(&basket).unwrap();
                trace!("Picked {}!", choice);

                // Clear that person as a receiver from all rows
                matrix.set_col(choice, false);

                // Register assignment
                assignments.push((person.clone(), choice.clone()));
            }

            return Ok(assignments);
        }
        return Err(AssignError::GivingUp)
    }
}

/// Errors that can happen while assigning names.
#[derive(Debug)]
pub enum AssignError {
    BadConstraint(String),
    GivingUp,
}

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

    #[test]
    fn matrix_init() {
        let keys = vec!["a".into(), "b".into(), "c".into()];
        let matrix = Matrix::new(keys);

        assert!(!matrix.get("a", "a"));
        assert!(!matrix.get("b", "b"));
        assert!(!matrix.get("c", "c"));

        assert!(matrix.get("a", "b"));
        assert!(matrix.get("a", "c"));
        assert!(matrix.get("b", "a"));
        assert!(matrix.get("b", "c"));
        assert!(matrix.get("c", "a"));
        assert!(matrix.get("c", "b"));
    }

    #[test]
    fn matrix_get_row() {
        let keys = vec!["a".into(), "b".into(), "c".into()];
        let mut matrix = Matrix::new(keys);
        assert_eq!(matrix.get_row("a"), vec![false, true, true]);
        assert_eq!(matrix.get_row("b"), vec![true, false, true]);
        assert_eq!(matrix.get_row("c"), vec![true, true, false]);
    }

    #[test]
    fn matrix_set() {
        let keys = vec!["a".into(), "b".into(), "c".into()];
        let mut matrix = Matrix::new(keys);

        assert!(matrix.get("a", "b"));
        matrix.set("a", "b", false);
        assert!(!matrix.get("a", "b"));
        matrix.set("a", "b", true);
        assert!(matrix.get("a", "b"));
    }

    #[test]
    fn matrix_contains() {
        let keys = vec!["a".into(), "b".into(), "c".into()];
        let mut matrix = Matrix::new(keys);

        assert!(matrix.contains("a"));
        assert!(matrix.contains("b"));
        assert!(matrix.contains("c"));
        assert!(!matrix.contains("d"));
        assert!(!matrix.contains("aa"));
    }

    #[test]
    fn matrix_size() {
        let keys = vec!["a".into(), "b".into(), "c".into()];
        let matrix = Matrix::new(keys);
        assert_eq!(3, matrix.size());

        let keys = vec!["a".into()];
        let matrix = Matrix::new(keys);
        assert_eq!(1, matrix.size());
    }

    /// Test a simple group assignment.
    #[test]
    fn group_simple() {
        let mut group = Group::new();

        group.add("a".into());
        group.add("b".into());
        group.add("c".into());

        let assignments = group.assign().unwrap();
        assert_eq!(assignments.len(), 3);

        for (from, to) in assignments {
            match from.as_ref() {
                "a" => assert!(to == "b" || to == "c"),
                "b" => assert!(to == "a" || to == "c"),
                "c" => assert!(to == "a" || to == "b"),
                _ => panic!(),
            }
        }
    }

    /// Test a group constellation that may fail.
    #[test]
    fn group_may_fail() {
        let mut group = Group::new();

        group.add("Sheldon".into());
        group.add("Amy".into());
        group.add("Leonard".into());
        group.add("Penny".into());
        group.add("Rajesh".into());

        group.exclude_pair("Sheldon".into(), "Amy".into());
        group.exclude_pair("Sheldon".into(), "Leonard".into());
        group.exclude_pair("Leonard".into(), "Penny".into());

        for i in 0..1000 {
            group.assign();
        }
    }
}