Christoph Stelz 1 rok temu
rodzic
commit
e9c0e5ab00
3 zmienionych plików z 55 dodań i 45 usunięć
  1. BIN
      src/.lib.rs.swp
  2. 45 35
      src/lib.rs
  3. 10 10
      src/main.rs

BIN
src/.lib.rs.swp


+ 45 - 35
src/lib.rs

@@ -18,6 +18,15 @@ impl Coord {
         }
     }
 
+    fn from_ab(a : i8, b : i8, upright: bool) -> Coord {
+        Coord {
+            a: a,
+            b: b,
+            c: if upright { 2 - a - b} else { 1 - a - b },
+        }
+
+    }
+
     /**
      * Rotates coordinate around the origin by 120°
      */
@@ -30,8 +39,6 @@ impl Coord {
     }
 
 
-
-
     /**
      * Flip coordinate at the y-axis
      */
@@ -51,30 +58,25 @@ impl Coord {
         }
     }
 
-    fn from_cartesian(x: i8, y: i8) -> Coord {
-        assert!(x >= 0 && y >= 0 && x < 63 && y < 63);
-        let upright : bool = (x + y) % 2 == 0;
-
-        let b : i8 = 1 - y;
-        let x_offset : i8 = (b - 1).abs() / 2;
-        let a : i8 = x_offset + x / 2;
-        let c : i8 = if upright { 2 - a - b } else {1 - a - b};
-
-        Coord::from(a,b,c)
-    }
-
     fn to_cartesian(&self) -> (i8, i8) {
-        let y : i8 = 1 - self.b;
-        let x_offset : i8 = (self.b - 1).abs() / 2;
-        let x : i8 = 2 * (self.a - x_offset) + if self.upright() { y % 2 }  else { 1 - (y % 2) };
+        (self.a - self.c + 1, 1 - self.b)
+        //let y : i8 = 1 - self.b;
+        //let x_offset : i8 = (self.b - 1).abs() / 2;
+        //let x : i8 = 2 * (self.a - x_offset) + if self.upright() { y % 2 }  else { 1 - (y % 2) };
 
 
 
-        return (x, y);
+        //return (x, y);
+    }
 
+    fn translate_x(&self, x: i8) -> Coord {
+        Coord::from_ab(self.a + x, self.b, self.upright())
     }
-}
 
+    fn translate_y(&self, y: i8) -> Coord {
+        Coord::from_ab(self.a + y, self.b - 2 * y, self.upright())
+    }
+}
 impl std::ops::Add<Coord> for Coord {
     type Output = Coord;
 
@@ -105,10 +107,10 @@ fn generate_part(arr: &[(i8, i8, i8)]) -> Part {
 pub fn generate_parts() -> HashMap<PartID, Part> {
     HashMap::from([
         (1, generate_part(&[(0,1,1), (0,0,1), (1,0,1), (1,0,0), (2,0,0), (1,-1,1)])),
-        (2, generate_part(&[(0,1,1), (0,1,0), (0,0,1), (1,0,1), (1,0,0)])),
+        (2, generate_part(&[(0,1,1), (0,1,0), (0,0,1), (1,0,1), (1,0,0), (2,0,0)])),
         (3, generate_part(&[(0,1,1), (0,0,1), (1,0,1), (1,0,0), (2,0,0), (2, -1, 0)])),
         (4, generate_part(&[(0,1,1), (0,1,0), (1,1,0), (1,0,0), (2,0,0), (2,-1,0)])),
-        (5, generate_part(&[(0,1,1), (0,0,1), (1,0,1), (1,-1,1), (1,-1,2), (1,-2,3)])),
+        (5, generate_part(&[(0,1,1), (0,1,0), (1,1,0), (1,1,-1), (0,0,1), (0,0,2)])),
         (6, generate_part(&[(0,1,1), (0,0,1), (1,0,1), (1,-1,1), (2,-1,1), (1,-1,2)])),
         (7, generate_part(&[(0,1,1), (0,0,1), (1,0,1), (1,0,0), (1,1,0), (2,0,0)])),
     ])
@@ -187,7 +189,7 @@ where F: FnMut(i8, i8) -> bool
         let mut coord : Coord = if flipped { coordinate.flip() } else { coordinate.clone() };
         coord = coord.rotate(rotations);
 
-        let world_pos = Coord::from_cartesian(x,y) + coord;
+        let world_pos = coord.translate_x(x / 2).translate_y(y / 2);
         let (wx, wy) = world_pos.to_cartesian();
 
         if in_bound(wx, wy) == false {
@@ -291,7 +293,7 @@ pub fn solve(map: Map, parts: &[(PartID, &Part)]) -> Option<()>{
 
             for rotation in [0, 1, 2] {
                 for flipped in [true, false] {
-                    println!("Testing rotation {} and flipped={}", rotation, flipped);
+                    //println!("Testing rotation {} and flipped={}", rotation, flipped);
                     let valid_position = check_part(&map, &part, x, y, rotation, flipped);
 
                     if valid_position {
@@ -383,14 +385,6 @@ pub fn print_map(m: &Map) {
 mod test {
     use super::*;
 
-    #[test]
-    fn test_from_cartesian_coords() {
-        assert_eq!(Coord::from(0, 1, 1), Coord::from_cartesian(0,0));
-        assert_eq!(Coord::from(5, -1, -2), Coord::from_cartesian(8, 2));
-        assert_eq!(Coord::from(2, -1, 0), Coord::from_cartesian(3, 2));
-        assert_eq!(Coord::from(3,0,-2), Coord::from_cartesian(6, 1));
-    }
-
     #[test]
     fn test_to_cartesian_coords() {
         assert_eq!((0,0), Coord::from(0,1,1).to_cartesian());
@@ -400,11 +394,27 @@ mod test {
 
         assert_eq!((0,-1), Coord::from(-1, 2, 0).to_cartesian());
 
-        for x in 0..63 {
-            for y in 0..63 {
-                assert_eq!((x,y), Coord::from_cartesian(x,y).to_cartesian());
+        assert_eq!((2,1), Coord::from(1,0,0).to_cartesian());
+        assert_eq!((3,1), Coord::from(2,0,0).to_cartesian());
 
-            }
+    }
+
+
+
+    #[test]
+    fn test_translation() {
+        assert_eq!(Coord::from(1,1,0), Coord::from(-1,1,2).translate_x(2));
+        assert_eq!(Coord::from(-1,0,3), Coord::from(2,0,0).translate_x(-3));
+        assert_eq!(Coord::from(1,-1,2), Coord::from(-1,3,0).translate_y(2));
+        assert_eq!(Coord::from(2,1,-1), Coord::from(3,-1,0).translate_y(-1));
+    }
+
+    #[test]
+    fn test_part1() {
+        let part = &generate_parts()[&1];
+        for p in part {
+            let (x,y) = p.to_cartesian();
+            println!("Part1: {} {}", x, y);
         }
 
     }

+ 10 - 10
src/main.rs

@@ -7,11 +7,11 @@ fn main() {
     let E = Cell::Empty;
 
     let mut map : Map = [
-        [E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E],
-        [E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E],
-        [E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E],
-        [E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E],
-        [E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E],
+        [B, B, E, E, E, B, B, B, E, E, E, B, B, B, B, B],
+        [B, E, E, E, E, E, B, B, B, E, E, E, B, B, B, B],
+        [E, E, E, E, E, E, E, B, E, E, E, E, E, E, B, B],
+        [E, E, E, E, E, E, E, B, E, E, E, E, E, B, B, B],
+        [B, E, E, E, B, E, B, B, B, B, B, E, B, B, B, B],
     ];
     let parts = generate_parts();
 
@@ -20,12 +20,12 @@ fn main() {
         .collect();
 
 
-    let (_, part1) = available_parts[0];
-    place_part(&mut map, 1, &parts[&1], 3, 3, 0, false);
-    print_map(&map);
-    //let result = solve(map, &available_parts[1..]);
-    //assert!(result.is_some());
+    //let (_, part1) = available_parts[0];
+    //place_part(&mut map, 1, &parts[&1], 2, 2, 2, true);
     //print_map(&map);
+    let result = solve(map, &available_parts[1..]);
+    assert!(result.is_some());
+    print_map(&map);
 
 
 }