Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // use std::i32::consts::PI;
- use std::fmt;
- const PI: i32 = 2;
- const PI_MOD: i32 = 2 * PI;
- #[derive(Debug)]
- enum Direction {
- Up,
- Left,
- Down,
- Right,
- Custom(i32),
- }
- impl Direction {
- fn from_i32(v: i32) -> Direction {
- match v {
- x if x == 0 => Direction::Right,
- x if x == PI => Direction::Left,
- x if x == PI / 2 => Direction::Up,
- x if x == 3 * PI / 2 => Direction::Down,
- _ => Direction::Custom(v)
- }
- }
- }
- #[derive(Copy, Clone, PartialEq)]
- struct Torch(i32);
- impl Torch {
- fn rotate(self, amount: i32) -> Self {
- Torch((self.0 + amount) % PI_MOD)
- }
- }
- impl fmt::Debug for Torch {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{:?}", Direction::from_i32(self.0))
- }
- }
- #[derive(Copy, Clone, Debug)]
- struct Puzzle(Torch, Torch, Torch);
- impl Puzzle {
- fn new(left: i32, center: i32, right: i32) -> Self {
- Puzzle(Torch(left), Torch(center), Torch(right))
- }
- fn rotate_left(self, amount: i32) -> Self {
- Puzzle(
- self.0.rotate(amount),
- self.1.rotate(amount / 2),
- self.2.rotate(amount + 1),
- )
- }
- fn rotate_center(self, amount: i32) -> Self {
- Puzzle(
- self.0.rotate(amount),
- self.1.rotate(amount),
- self.2.rotate(amount),
- )
- }
- fn rotate_right(self, amount: i32) -> Self {
- Puzzle(
- self.0.rotate(amount + (PI / 2)),
- self.1.rotate(amount + (PI / 2)),
- self.2.rotate(amount),
- )
- }
- fn solved(&self) -> bool {
- let up = Torch(PI / 2);
- self.0 == up && self.1 == up && self.2 == up
- }
- }
- fn main() {
- let mut puzzle = Puzzle::new(PI / 2, 3 * PI / 2, PI / 2);
- let mut solution = Vec::new();
- println!("{:?}",
- puzzle.rotate_center(PI)
- .rotate_right(PI)
- .rotate_left(PI));
- for i in 1..11 {
- puzzle = match i % 3 {
- 0 => { puzzle.rotate_left(PI); solution.push("left"); puzzle },
- 1 => { puzzle.rotate_center(PI); solution.push("center"); puzzle },
- 2 => { puzzle.rotate_right(PI); solution.push("right"); puzzle },
- _ => unreachable!(),
- };
- if puzzle.solved() {
- break
- }
- }
- println!("[{:?}] {:?}", puzzle, solution);
- }
Add Comment
Please, Sign In to add comment