-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_santa.html
143 lines (120 loc) · 3.84 KB
/
secret_santa.html
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
<html><head><title>Secret Santa</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var siblings = ["Lisa", "Adam", "John", "Elizabeth", "Lynn", "Paul", "Allison", "Lauren"];
var def_gg = [
// Li Ad J El Ly P Al La < givee v giver
[ 1, 1, 0, 0, 0, 2017, 2016, 2015], // Lisa
[ 1, 1, 2017, 0, 2015, 2016, 0, 0], // Adam
[2017, 0, 1, 1, 2016, 0, 2015, 0], // John
[ 0, 0, 1, 1, 0, 0, 0, 0], // Elizabeth
[ 0, 2016, 2015, 0, 1, 0, 0, 2017], // Lynn
[ 0, 2015, 0, 0, 2017, 1, 1, 2016], // Paul
[2015, 2017, 2016, 0, 0, 1, 1, 0], // Allison
[2016, 0, 0, 0, 0, 2015, 2017, 1]]; // Lauren
function any(a) {
return a.some(function(x){return x});
}
function sum(a) {
return a.reduce(function(s,x){return x?s+1:s;}, 0);
}
function first(a) {
for (var i=0; i<a.length; i++) {
if (a[i]) return i;
}
return -1;
}
$(document).ready(function() {
var givergiveetable = "<table>";
givergiveetable += `<tr><th /><th
colspan=${siblings.length}>Recipient</th></tr>\n`;
givergiveetable += "<tr><th />";
for (sib in siblings) {
givergiveetable += `<th>${siblings[sib]}</th>`;
}
givergiveetable += "</tr>";
for (i in siblings) {
givergiveetable += `<tr><th>${siblings[i]}</th>`;
for (j in siblings) {
givergiveetable += `<td><input type="checkbox" name="gg_${i}_${j}"
value="1" ${def_gg[i][j]?"checked":""}></td>`;
}
givergiveetable += "</tr>\n";
}
givergiveetable += "</table>";
$('#givergivee').html(givergiveetable);
});
function draw() {
var undrawn = [];
for (i = 0; i < siblings.length; i++) {
undrawn.push(true);
}
var cycle = [];
var gg = [];
for (var i=0; i<siblings.length; i++) {
gg.push([]);
for (var j=0; j<siblings.length; j++) {
gg[i].push(!$(`input[name=gg_${i}_${j}]`).is(":checked"));
}
}
var restarts = 0;
while (any(undrawn)) {
if (cycle.length == 0) {
var n = Math.floor(Math.random()*undrawn.length);
cycle.push(n);
undrawn[n] = false;
}
var recipients = gg[cycle[cycle.length-1]].slice();
for (var i=0; i<cycle.length; i++) {
recipients[cycle[i]] = false;
}
var draw_pool = [];
for (var i=0; i<recipients.length; i++) {
if (recipients[i]) draw_pool.push(i);
}
if ((draw_pool.length == 0) || ((sum(undrawn) == 1) && !gg[first(undrawn)][cycle[0]])) {
cycle = [];
undrawn = [];
for (i = 0; i < siblings.length; i++) {
undrawn.push(true);
}
restarts += 1;
console.log('Dead end.');
$('#draw').html("Try again.");
if (restarts > 1000) {
return;
}
continue;
//return;
}
var n = Math.floor(Math.random()*draw_pool.length);
cycle.push(draw_pool[n]);
undrawn[draw_pool[n]] = false;
}
console.log("SUCCESS!");
console.log(cycle);
var cycle_string = "";
for (var i=0; i < cycle.length; i++) {
cycle_string += siblings[cycle[i]] + " -> ";
}
cycle_string += siblings[cycle[0]];
$('#draw').html(cycle_string);
}
</script>
</head>
<body>
<h2>Sibling Secret Santa</h2>
<h4>Gift restrictions</h4>
Use the grid below to eliminate giver-recipient pairings. The top is the
recipient, and the left is the giver. If the box is checked, then the giver
specified cannot give to the recipient specified.
<form id='secretsanta'>
<div id="givergivee">
</div>
<h4>Draw</h4>
<button onclick="draw(); return false;">Draw!</button>
<div id="draw">
</div>
</form>
</body>
</html>