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
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Card(u32);
impl Card {
pub fn new(mut index: usize) -> Card {
let mut value = index % 3;
for _ in 0..3 {
value <<= 8;
index /= 3;
value |= index % 3;
}
Card(value as u32)
}
pub fn index(&self) -> usize {
let mut value = self.0;
let mut result = value & 0xff;
for _ in 0..3 {
value >>= 8;
result *= 3;
result += value & 0xff;
}
result as usize
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Feature { Count, Shape, Color, Shading }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Shape { Oval, Squiggle, Diamond }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color { A, B, C }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Shading { Solid, Striped, Outlined }
impl Card {
fn feature(&self, feature: Feature) -> u8 {
(self.0 >> (feature as u32 * 8) & 0xff) as u8
}
pub fn count(&self) -> u8 {
self.feature(Feature::Count) + 1
}
pub fn shape(&self) -> Shape {
match self.feature(Feature::Shape) {
0 => Shape::Oval,
1 => Shape::Squiggle,
_ => Shape::Diamond,
}
}
pub fn color(&self) -> Color {
match self.feature(Feature::Color) {
0 => Color::A,
1 => Color::B,
_ => Color::C,
}
}
pub fn shading(&self) -> Shading {
match self.feature(Feature::Shading) {
0 => Shading::Solid,
1 => Shading::Striped,
_ => Shading::Outlined,
}
}
}
use std::fmt;
impl fmt::Debug for Card {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Card({})", self.index())
}
}
type Triple = (Card, Card, Card);
pub struct Set { cards: Triple }
impl Set {
pub fn cards(&self) -> Triple {
self.cards
}
}
pub trait ToSet {
fn to_set(self) -> Option<Set>;
}
impl ToSet for Triple {
fn to_set(self) -> Option<Set> {
let (a, b, c) = self;
let mut sum = a.0 + b.0 + c.0;
let mut result = true;
while sum != 0 {
result &= (sum & 0xff) % 3 == 0;
sum >>= 8;
}
if result {
let set = Set { cards: self };
Some(set)
} else {
None
}
}
}
type Pair = (Card, Card);
pub struct SuperSet { left: Pair, right: Pair }
impl SuperSet {
pub fn left(&self) -> Pair {
self.left
}
pub fn right(&self) -> Pair {
self.right
}
}
pub trait ToSuperSet {
fn to_superset(self) -> Option<SuperSet>;
}
impl ToSuperSet for (Card, Card, Card, Card) {
fn to_superset(self) -> Option<SuperSet> {
let (a, b, c, d) = self;
let pair_combos = &[((a, b), (c, d)),
((a, c), (b, d)),
((a, d), (b, c))];
for &(left, right) in pair_combos {
if left.complete_set() == right.complete_set() {
let superset = SuperSet { left: left, right: right };
return Some(superset);
}
}
None
}
}
pub trait CompleteSet {
fn complete_set(self) -> Card;
}
impl CompleteSet for Pair {
fn complete_set(self) -> Card {
const MATCHES: [u32; 5] = [0, 2, 1, 0, 2];
let (a, b) = self;
let mut sum = (a.0 + b.0) as usize;
let mut value = MATCHES[sum & 0xff];
for _ in 0..3 {
value <<= 8;
sum >>= 8;
value |= MATCHES[sum & 0xff];
}
let reversed = value.swap_bytes();
Card(reversed)
}
}
impl<'a> CompleteSet for (&'a Card, &'a Card) {
fn complete_set(self) -> Card {
let (&a, &b) = self;
(a, b).complete_set()
}
}
#[cfg(test)]
mod tests {
use super::*;
use deck::cards;
use pair_iter::PairIter;
#[test]
fn check_card_conversions() {
for i in 0..81 {
let card = Card::new(i);
assert_eq!(i, card.index());
}
}
#[test]
fn check_set_completion() {
let cards = cards();
let mut set_count = 0;
for (&a, &b) in cards.pairs() {
let c = (a, b).complete_set();
let triple = (a, b, c);
assert!(triple.to_set().is_some());
set_count += 1;
}
assert_eq!(set_count, 1080 * 3)
}
}