Skip to content

Instantly share code, notes, and snippets.

@josephg
Created October 16, 2022 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephg/9c7fa8bc48849419bb96013ea4c7ad75 to your computer and use it in GitHub Desktop.
Save josephg/9c7fa8bc48849419bb96013ea4c7ad75 to your computer and use it in GitHub Desktop.
rotation puzzle
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::hash::Hash;
// Wrapper around u16. Using the lowest significant 9 bits to store a 3x3 state.
#[derive(Copy, Clone, Eq, PartialEq, Default, Hash)]
struct Bits(u16);
fn main() {
let mut map = HashMap::new();
let mut case = 0;
for i in 0..512 {
let b = Bits(i);
if map.contains_key(&b) { continue; }
println!("{case}: {:?}", b);
let b = b.rotate();
map.insert(b, case);
let b = b.rotate();
map.insert(b, case);
let b = b.rotate();
map.insert(b, case);
case += 1;
}
println!("{case}");
}
impl Debug for Bits {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut formatter = f.debug_list();
for i in 0..9 {
formatter.entry(&(if self.bit(i) { 1 } else { 0 }));
}
formatter.finish()
}
}
impl Bits {
fn bit(self, n: u8) -> bool {
assert!(n < 9);
(1u16 << n) & self.0 != 0
}
fn from_bits(bits: [bool; 9]) -> Self {
Bits(
(bits[0] as u16) | (bits[1] as u16) << 1 | (bits[2] as u16) << 2
| (bits[3] as u16) << 3 | (bits[4] as u16) << 4 | (bits[5] as u16) << 5
| (bits[6] as u16) << 6 | (bits[7] as u16) << 7 | (bits[8] as u16) << 8
)
}
fn rotate(self) -> Self {
Self::from_bits([
self.bit(6), self.bit(3), self.bit(0),
self.bit(7), self.bit(4), self.bit(1),
self.bit(8), self.bit(5), self.bit(2),
])
}
}
This is the list of all the distinct bit patterns, excluding duplicates due to rotation
0: [0, 0, 0, 0, 0, 0, 0, 0, 0]
1: [1, 0, 0, 0, 0, 0, 0, 0, 0]
2: [0, 1, 0, 0, 0, 0, 0, 0, 0]
3: [1, 1, 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, 1]
Number of representable states: 140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment