+// 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));
     }
   }
 
 
 //
 
+pub use std::fmt::{self, Display};
 pub use std::io;
 pub use std::io::Write as _;
 pub use std::iter;
   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)
 }