-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparison.js
More file actions
147 lines (124 loc) · 3.76 KB
/
comparison.js
File metadata and controls
147 lines (124 loc) · 3.76 KB
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
function objectify (cards) {
/*
Utility object maker.
TODO: Just store stuff like this to begin with?
*/
const cardsObj = {}
for (var dot of cards) {
if (cardsObj[dot]) {
cardsObj[dot] += 1
} else {
cardsObj[dot] = 1
}
}
return cardsObj
}
function validate (proposedSet, cardsObj) {
for (var val in cardsObj) {
if (cardsObj[val] % 2 !== 0 || new Set(proposedSet).size !== proposedSet.length) {
return false
}
}
return true
}
function threeCardSet (deck) {
const actualSets = []
for (var first in deck) {
for (var second in deck) {
for (var third in deck) {
let valid = true
const proposedSet = [first, second, third].sort()
const proposedSetString = proposedSet.join('')
const cardsObj = objectify([...deck[first], ...deck[second], ...deck[third]])
valid = validate(proposedSet, cardsObj)
if (valid && !actualSets.includes(proposedSetString)) {
actualSets.push(proposedSetString)
}
}
}
}
return actualSets
}
function fourCardSet (deck) {
const actualSets = []
for (var first in deck) {
for (var second in deck) {
for (var third in deck) {
for (var fourth in deck) {
let valid = true
const proposedSet = [first, second, third, fourth].sort()
const proposedSetString = proposedSet.join('')
const cardsObj = objectify([...deck[first], ...deck[second], ...deck[third], ...deck[fourth]])
valid = validate(proposedSet, cardsObj)
if (valid && !actualSets.includes(proposedSetString)) {
actualSets.push(proposedSetString)
}
}
}
}
}
return actualSets
}
function fiveCardSet (deck) {
const actualSets = []
for (var first in deck) {
for (var second in deck) {
for (var third in deck) {
for (var fourth in deck) {
for (var fifth in deck) {
let valid = true
const proposedSet = [first, second, third, fourth, fifth].sort()
const proposedSetString = proposedSet.join('')
const cardsObj = objectify([...deck[first], ...deck[second], ...deck[third], ...deck[fourth], ...deck[fifth]])
valid = validate(proposedSet, cardsObj)
if (valid && !actualSets.includes(proposedSetString)) {
actualSets.push(proposedSetString)
}
}
}
}
}
}
return actualSets
}
function sixCardSet (deck) {
const actualSets = []
for (var first in deck) {
for (var second in deck) {
for (var third in deck) {
for (var fourth in deck) {
for (var fifth in deck) {
for (var sixth in deck) {
let valid = true
const proposedSet = [first, second, third, fourth, fifth, sixth].sort()
const proposedSetString = proposedSet.join('')
const cardsObj = objectify([...deck[first], ...deck[second], ...deck[third], ...deck[fourth], ...deck[fifth], ...deck[sixth]])
valid = validate(proposedSet, cardsObj)
if (valid && !actualSets.includes(proposedSetString)) {
actualSets.push(proposedSetString)
}
}
}
}
}
}
}
return actualSets
}
function sevenCardSet (deck) {
const actualSets = []
let cardsArr = []
const proposedSet = [1, 2, 3, 4, 5, 6, 7]
const proposedSetString = proposedSet.join('')
let valid = true
for (var card in deck) {
cardsArr = [...cardsArr + deck[card]]
}
const cardsObj = objectify(cardsArr)
valid = validate(proposedSet, cardsObj)
if (valid) {
actualSets.push(proposedSetString)
}
return actualSets
}
module.exports = { threeCardSet, fourCardSet, fiveCardSet, sixCardSet, sevenCardSet, objectify }