Skip to content

Commit

Permalink
Perms
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesTaylor7 committed May 3, 2024
1 parent 01e6360 commit 996924b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 14 deletions.
74 changes: 66 additions & 8 deletions wasm/src/permutation.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::collections::HashMap;

#[derive(Clone)]
pub struct Permutation(HashMap<u8, u8>);
pub struct PermHashMap(HashMap<u8, u8>);

impl Permutation {
fn identity() -> Self {
Permutation(HashMap::default())
impl PermHashMap {
pub fn identity() -> Self {
Self(HashMap::default())
}

fn invert(&self) -> Self {
Permutation(self.0.iter().map(|(k, v)| (*v, *k)).collect())
pub fn invert(&self) -> Self {
Self(self.0.iter().map(|(k, v)| (*v, *k)).collect())
}

fn compose(p: &Self, q: &Self) -> Self {
pub fn compose(p: &Self, q: &Self) -> Self {
let (mut p, q) = p
.0
.iter()
Expand All @@ -30,10 +30,68 @@ impl Permutation {
p.0.insert(key, value);
}
p
}

pub fn permute(&self, k: u8) -> u8 {
self.0.get(&k).map_or(k, |v| *v)
}
}

#[derive(Clone)]
pub struct PermArray<const N: u8>([u8; N as usize])
where
[u8; N as usize]: Sized;
//Assert<{ N as usize < 256 }>: IsTrue;
impl<const N: u8> PermArray<N>
where
[u8; N as usize]: Sized, //Assert<{ N as usize < 256 }>: IsTrue;
{
pub fn identity() -> Self {
let mut array = [0; N as usize];
for i in 1_u8..N {
array[i as usize] = i;
}
Self(array)
}

pub fn invert(&self) -> Self {
let mut array = [0; N as usize];
for (k, v) in self.0.iter().enumerate() {
array[*v as usize] = k as u8;
}
Self(array)
}

pub fn compose(p: &Self, q: &Self) -> Self {
todo!()
/*
let (mut p, q) = p
.0
.iter()
.fold((p.clone(), q.clone()), |(mut p, mut q), (k, v)| {
let qv = q.permute(*v);
if *k == qv {
p.0.remove(k);
} else {
p.0.insert(*k, qv);
}
q.0.remove(v);
(p, q)
});
for (key, value) in q.0 {
p.0.insert(key, value);
}
p
*/
//Self::identity()
}

fn permute(&self, k: u8) -> u8 {
self.0.get(&k).map_or(k, |v| *v)
self.0[k as usize]
}
}

// For const assertions
pub enum Assert<const CHECK: bool> {}
pub trait IsTrue {}
impl IsTrue for Assert<true> {}
56 changes: 50 additions & 6 deletions wasm/src/puzzle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::permutation::{PermArray, PermHashMap};

enum Hex {
White,
Pink,
Expand All @@ -20,15 +22,57 @@ enum Square {

struct TriangleFacet(Hex);
struct EdgeFacet(Square, Hex);
struct SquareFacet(Square);

// all possible facets for center cuts only puzzle.
enum Facet {
Square(SquareFacet),
Triangle(TriangleFacet),
Edge(EdgeFacet),
}

//fn puzzle()
struct Puzzle {
edges: Vec<EdgeFacet>,
squares: Vec<SquareFacet>,
triangles: Vec<TriangleFacet>,
edge_permutation: PermArray<24>,
square_permutation: PermArray<6>,
triangle_permutation: PermArray<8>,
}

struct PerspectiveShift {
hexes: Vec<Hex>,
squares: Vec<Square>,
impl Puzzle {
fn new() -> Self {
Self {
edges: vec![],
squares: vec![],
triangles: vec![],
edge_permutation: PermArray::identity(),
square_permutation: PermArray::identity(),
triangle_permutation: PermArray::identity(),
}
}
}

struct Rotation {
hexes: Vec<Hex>,
squares: Vec<Square>,
/*
fn facets() -> Vec<Facet> {
triangle_facets()
.map(|t| Facet::Triangle(facet))
.concat(square_facets())
.concat(edge_facets())
}
*/

fn square_facets() -> Vec<SquareFacet> {
vec![
SquareFacet(Square::White),
SquareFacet(Square::Yellow),
SquareFacet(Square::Blue),
SquareFacet(Square::Green),
SquareFacet(Square::Yellow),
SquareFacet(Square::Red),
SquareFacet(Square::Orange),
]
}

fn triangle_facets() -> Vec<TriangleFacet> {
Expand Down

0 comments on commit 996924b

Please sign in to comment.