From fbe28b5325a8ae2a53dede00d191fcf438fa72b3 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 29 Oct 2022 22:26:02 +0100 Subject: [PATCH] wip print-opt-orient Signed-off-by: Ian Jackson --- src/bin/print-opt-orient.rs | 21 +++++++++++++++++---- src/lib.rs | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/bin/print-opt-orient.rs b/src/bin/print-opt-orient.rs index 70cc60c..8b578a4 100644 --- a/src/bin/print-opt-orient.rs +++ b/src/bin/print-opt-orient.rs @@ -1,19 +1,32 @@ +// Tries rotating it about and sees which one has smallest +// total Z coordinate for in-Z-layer edges. +// That is least support when printing. use z3_treefoil::*; fn main() -> io::Result<()> { for shape1 in read_vertices() { - let rots = shape_all_rotations(&shape1) + let mut rots = shape_all_rotations(&shape1) .enumerate() .map(|(i, shape)| { - let weight = 0; + let weight: i32 = shape_edges(&shape) + .filter_map(|edge| { + let z = edge + .iter() + .map(|p| p[DIM-1]) + .dedup() + .exactly_one().ok()?; + Some(i32::from(z)) + }) + .sum(); (weight, i, shape) }) .collect_vec(); + rots.sort(); + for (weight, _, shape) in rots { - print!("{} ", weight); - print_shape(&shape)?; + println!("{} {}", weight, Xyzzy(&shape)); } } diff --git a/src/lib.rs b/src/lib.rs index f911cd9..5cf9d5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ // +pub use std::fmt::{self, Display}; pub use std::io; pub use std::io::Write as _; pub use std::iter; @@ -93,6 +94,26 @@ pub fn print_shape(shape: &Shape) -> io::Result<()> { Ok(()) } +pub struct Xyzzy<'s>(pub &'s Shape); + +impl Display for Xyzzy<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + for edge in shape_edges(self.0) { + let mut done = None; + for (i, c) in izip!(0..DIM, "xyz".chars()) { + let d = edge[1][i] - edge[0][i]; + if d == 0 { continue } + assert_eq!(done, None); + let c = if d > 0 { c.to_ascii_uppercase() } else { c }; + write!(f, "{}", c)?; + done = Some(c); + } + assert!(done.is_some()); + } + Ok(()) + } +} + pub fn point_middle(point: &Point) -> bool { point.iter().all(|&c| c == 1 || c == 2) } -- 2.30.2