Guest User

Untitled

a guest
Jan 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. // use std::i32::consts::PI;
  2. use std::fmt;
  3.  
  4. const PI: i32 = 2;
  5. const PI_MOD: i32 = 2 * PI;
  6.  
  7. #[derive(Debug)]
  8. enum Direction {
  9. Up,
  10. Left,
  11. Down,
  12. Right,
  13. Custom(i32),
  14. }
  15.  
  16. impl Direction {
  17. fn from_i32(v: i32) -> Direction {
  18. match v {
  19. x if x == 0 => Direction::Right,
  20. x if x == PI => Direction::Left,
  21. x if x == PI / 2 => Direction::Up,
  22. x if x == 3 * PI / 2 => Direction::Down,
  23. _ => Direction::Custom(v)
  24. }
  25. }
  26. }
  27.  
  28. #[derive(Copy, Clone, PartialEq)]
  29. struct Torch(i32);
  30. impl Torch {
  31. fn rotate(self, amount: i32) -> Self {
  32. Torch((self.0 + amount) % PI_MOD)
  33. }
  34. }
  35.  
  36. impl fmt::Debug for Torch {
  37. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  38. write!(f, "{:?}", Direction::from_i32(self.0))
  39. }
  40. }
  41.  
  42. #[derive(Copy, Clone, Debug)]
  43. struct Puzzle(Torch, Torch, Torch);
  44. impl Puzzle {
  45. fn new(left: i32, center: i32, right: i32) -> Self {
  46. Puzzle(Torch(left), Torch(center), Torch(right))
  47. }
  48.  
  49. fn rotate_left(self, amount: i32) -> Self {
  50. Puzzle(
  51. self.0.rotate(amount),
  52. self.1.rotate(amount / 2),
  53. self.2.rotate(amount + 1),
  54. )
  55. }
  56.  
  57. fn rotate_center(self, amount: i32) -> Self {
  58. Puzzle(
  59. self.0.rotate(amount),
  60. self.1.rotate(amount),
  61. self.2.rotate(amount),
  62. )
  63. }
  64.  
  65. fn rotate_right(self, amount: i32) -> Self {
  66. Puzzle(
  67. self.0.rotate(amount + (PI / 2)),
  68. self.1.rotate(amount + (PI / 2)),
  69. self.2.rotate(amount),
  70. )
  71. }
  72.  
  73. fn solved(&self) -> bool {
  74. let up = Torch(PI / 2);
  75.  
  76. self.0 == up && self.1 == up && self.2 == up
  77. }
  78. }
  79.  
  80. fn main() {
  81. let mut puzzle = Puzzle::new(PI / 2, 3 * PI / 2, PI / 2);
  82. let mut solution = Vec::new();
  83.  
  84. println!("{:?}",
  85. puzzle.rotate_center(PI)
  86. .rotate_right(PI)
  87. .rotate_left(PI));
  88.  
  89.  
  90. for i in 1..11 {
  91. puzzle = match i % 3 {
  92. 0 => { puzzle.rotate_left(PI); solution.push("left"); puzzle },
  93. 1 => { puzzle.rotate_center(PI); solution.push("center"); puzzle },
  94. 2 => { puzzle.rotate_right(PI); solution.push("right"); puzzle },
  95. _ => unreachable!(),
  96. };
  97.  
  98. if puzzle.solved() {
  99. break
  100. }
  101. }
  102.  
  103. println!("[{:?}] {:?}", puzzle, solution);
  104. }
Add Comment
Please, Sign In to add comment