repl.it
@Bibliofile/

TulipColors

Rust

No description

fork
loading
Files
  • main.rs
  • main.4s37gsrti678ik8u.rcgu.o
  • main.main.7rcbfp3g-cgu.0.rcgu.o
  • main.main.7rcbfp3g-cgu.1.rcgu.o
  • main.main.7rcbfp3g-cgu.10.rcgu.o
  • main.main.7rcbfp3g-cgu.15.rcgu.o
  • main.main.7rcbfp3g-cgu.2.rcgu.o
  • main.main.7rcbfp3g-cgu.3.rcgu.o
  • main.main.7rcbfp3g-cgu.4.rcgu.o
  • main.main.7rcbfp3g-cgu.5.rcgu.o
  • main.main.7rcbfp3g-cgu.6.rcgu.o
  • main.main.7rcbfp3g-cgu.7.rcgu.o
  • main.main.7rcbfp3g-cgu.8.rcgu.o
  • main.main.7rcbfp3g-cgu.9.rcgu.o
main.rs
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
use std::collections::HashSet;
use std::hash::{Hash, Hasher};

#[derive(Eq)]
struct TulipColor(u8, u8, u8);

impl PartialEq for TulipColor {
  fn eq(&self, other: &Self) -> bool {
    // Same colors, same mix = same
    if self.0 == other.0 && self.1 == other.1 && self.2 == other.2 {
      return true;
    }

    // Same colors across the board = same
    // (red red) & (red red) - mix doesn't matter
    if self.0 == other.0 && self.1 == other.1 && self.0 == other.1 {
      return true;
    }

    // Check for the colors mixed in opposite directions
    // (red blue) mix 0 vs (blue red) mix 7
    if self.0 == other.1 && self.1 == other.0 {
      return self.2 + other.2 == 7;
    }

    // Otherwise it is actually different
    false
  }
}

impl Hash for TulipColor {
  // Always fall back on the eq method for testing for equality... slower, but this isn't perf critical
  fn hash<H: Hasher>(&self, h: &mut H) {
    1.hash(h)
  }
}

fn main() {
  let mut set = HashSet::new();

  for a in 0..8 {
    for b in 0..8 {
      for mix in 0..8 {
        set.insert(TulipColor(a, b, mix));
      }
    }
  }

  println!("There are {} total colors", set.len() * set.len());
}