From 996924be9d25c855e33c2e33e94f7f9ae0842eb4 Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Fri, 3 May 2024 14:17:05 -0400 Subject: [PATCH] Perms --- wasm/src/permutation.rs | 74 ++++++++++++++++++++++++++++++++++++----- wasm/src/puzzle.rs | 56 +++++++++++++++++++++++++++---- 2 files changed, 116 insertions(+), 14 deletions(-) diff --git a/wasm/src/permutation.rs b/wasm/src/permutation.rs index b0b034c..037e90f 100644 --- a/wasm/src/permutation.rs +++ b/wasm/src/permutation.rs @@ -1,18 +1,18 @@ use std::collections::HashMap; #[derive(Clone)] -pub struct Permutation(HashMap); +pub struct PermHashMap(HashMap); -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() @@ -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([u8; N as usize]) +where + [u8; N as usize]: Sized; +//Assert<{ N as usize < 256 }>: IsTrue; +impl PermArray +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 {} +pub trait IsTrue {} +impl IsTrue for Assert {} diff --git a/wasm/src/puzzle.rs b/wasm/src/puzzle.rs index a347e72..bd2ea90 100644 --- a/wasm/src/puzzle.rs +++ b/wasm/src/puzzle.rs @@ -1,3 +1,5 @@ +use crate::permutation::{PermArray, PermHashMap}; + enum Hex { White, Pink, @@ -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, + squares: Vec, + triangles: Vec, + edge_permutation: PermArray<24>, + square_permutation: PermArray<6>, + triangle_permutation: PermArray<8>, +} -struct PerspectiveShift { - hexes: Vec, - squares: Vec, +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, - squares: Vec, +/* +fn facets() -> Vec { + triangle_facets() + .map(|t| Facet::Triangle(facet)) + .concat(square_facets()) + .concat(edge_facets()) +} +*/ + +fn square_facets() -> Vec { + 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 {