design.rs

2.3 kB · rust · 79 lines

1use mrlycore::Tensor;2use std::collections::BTreeSet;34pub const BASE: usize = 3;56pub fn plane(code: u128, base: usize, level: usize) -> Tensor {7    mrlymath::two::create(code, base, level, 0, base)8        .expect("a plane design renders")9        .types()10        .clone()11}1213pub fn floats(grid: &Tensor) -> Vec<f32> {14    grid.bytes().iter().map(|&b| b as f32).collect()15}1617pub fn bit_cells() -> Vec<(usize, usize)> {18    (0..9)19        .map(|bit| {20            let grid = plane(1u128 << bit, BASE, 1);21            let flat = grid22                .bytes()23                .iter()24                .position(|cell| *cell != 0)25                .expect("one filled cell");26            (flat / BASE, flat % BASE)27        })28        .collect()29}3031pub fn square_group() -> Vec<Vec<usize>> {32    let mut out: Vec<Vec<usize>> = Vec::new();33    for flip in 0..2 {34        for turn in 0..4 {35            let map: Vec<usize> = (0..9)36                .map(|flat| {37                    let (mut r, mut c) = (flat / BASE, flat % BASE);38                    if flip == 1 {39                        std::mem::swap(&mut r, &mut c);40                    }41                    for _ in 0..turn {42                        let next = (c, BASE - 1 - r);43                        r = next.0;44                        c = next.1;45                    }46                    r * BASE + c47                })48                .collect();49            if !out.contains(&map) {50                out.push(map);51            }52        }53    }54    out55}5657pub fn carry(map: &[usize], code: u128, table: &[(usize, usize)]) -> u128 {58    let index: Vec<usize> = table.iter().map(|(r, c)| r * BASE + c).collect();59    let mut out = 0u128;60    for bit in 0..9 {61        if code >> bit & 1 == 1 {62            let image = map[index[bit]];63            let target = index64                .iter()65                .position(|flat| *flat == image)66                .expect("a cell");67            out |= 1u128 << target;68        }69    }70    out71}7273pub fn orbit(group: &[Vec<usize>], code: u128, table: &[(usize, usize)]) -> BTreeSet<u128> {74    group.iter().map(|map| carry(map, code, table)).collect()75}7677pub fn canonical(group: &[Vec<usize>], code: u128, table: &[(usize, usize)]) -> u128 {78    *orbit(group, code, table).iter().next().expect("an orbit")79}