Advertisement
tomdodd4598

Untitled

Oct 5th, 2021
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.56 KB | None | 0 0
  1. import itertools
  2.  
  3. # -------------------------------#
  4. # ========== SETTINGS ========== #
  5. # -------------------------------#
  6.  
  7. # Note: initial positions are counted clockwise from the bottom for the left wheel/console,
  8. # and anti-clockwise from the bottom for the right wheel/console, zero-indexed.
  9.  
  10. # Note: left wheel is numbered 0, right wheel is numbered 1.
  11.  
  12. # Number of positions on the wheels and their respective consoles.
  13. wheel_size_left = 6  # default = 6
  14. wheel_size_right = 6  # default = 6
  15.  
  16. # Starting wheel and position of the crystal ball.
  17. start_wheel = 0  # default = 0
  18. start_position = 0  # default = 0
  19.  
  20. # Target wheel and position the crystal ball must fall through.
  21. target_wheel = 1  # default = 1
  22. target_position = 4  # default = 4
  23.  
  24. # Positions on the wheels which can hold the crystal ball.
  25. holding_positions_left = [0, 2, 4]  # default = [0, 2, 4]
  26. holding_positions_right = [1, 3, 5]  # default = [1, 3, 5]
  27.  
  28. # Jump points and targets from left to right and right to left.
  29. jumps_left_to_right = {0: 0, 3: 3, 4: 4, 5: 5}  # default = {0: 0, 3: 3, 4: 4, 5: 5}
  30. jumps_right_to_left = {0: 0, 3: 3, 4: 4, 5: 5}  # default = {0: 0, 3: 3, 4: 4, 5: 5}
  31.  
  32. # Positions on the console which can not hold a peg
  33. blocked_pegs_left = [0, 2]  # default = [0, 2]
  34. blocked_pegs_right = [0]  # default = [0]
  35.  
  36. # -------------------------------#
  37. # ========== INTERNAL ========== #
  38. # -------------------------------#
  39.  
  40. _holding_positions_left = set(holding_positions_left)
  41. _holding_positions_right = set(holding_positions_right)
  42.  
  43. _blocked_pegs_left = set(blocked_pegs_left)
  44. _blocked_pegs_right = set(blocked_pegs_right)
  45.  
  46. available_pegs_left = set(range(wheel_size_left)) - _blocked_pegs_left
  47. available_pegs_right = set(range(wheel_size_right)) - _blocked_pegs_right
  48.  
  49.  
  50. class Wheel:
  51.     def __init__(self, size, raw_holding_positions: set):
  52.         self.size = size
  53.         self.raw_holding_positions = raw_holding_positions
  54.         self.rotation = 0
  55.  
  56.     def rotate(self):
  57.         self.rotation += 1
  58.         self.rotation %= self.size
  59.  
  60.     def is_holding_position(self, position):
  61.         position += (self.size - self.rotation)
  62.         return position % self.size in self.raw_holding_positions
  63.  
  64.  
  65. class Console:
  66.     def __init__(self, size, pegs: set):
  67.         self.size = size
  68.         self.pegs = pegs
  69.         self.current_position = 0
  70.  
  71.     def increment_position(self):
  72.         self.current_position += 1
  73.         self.current_position %= self.size
  74.  
  75.  
  76. class Puzzle:
  77.     def __init__(self, wheel_left: Wheel, wheel_right: Wheel, console_left: Console, console_right: Console):
  78.         self.wheel_left = wheel_left
  79.         self.wheel_right = wheel_right
  80.         self.console_left = console_left
  81.         self.console_right = console_right
  82.         self.ball_wheel = start_wheel
  83.         self.ball_position = start_position
  84.  
  85.     def current_wheel(self):
  86.         return self.wheel_left if self.ball_wheel == 0 else self.wheel_right
  87.  
  88.     def current_console(self):
  89.         return self.console_left if self.ball_wheel == 0 else self.console_right
  90.  
  91.     def is_fail(self):
  92.         wheel = self.current_wheel()
  93.         if not wheel.is_holding_position(self.ball_position):
  94.             return True
  95.         console = self.current_console()
  96.         if len(console.pegs) == 0 or max(console.pegs) < console.current_position:
  97.             return True
  98.         return False
  99.  
  100.     def move(self):
  101.         wheel = self.current_wheel()
  102.         wheel.rotate()
  103.         self.ball_position += (wheel.size - 1)
  104.         self.ball_position %= wheel.size
  105.         self.current_console().increment_position()
  106.  
  107.     def get_jumps(self) -> dict:
  108.         return jumps_left_to_right if self.ball_wheel == 0 else jumps_right_to_left
  109.  
  110.     def try_jump(self):
  111.         console = self.current_console()
  112.         if console.current_position in console.pegs:
  113.             jumps = self.get_jumps()
  114.             if self.ball_position in jumps:
  115.                 self.ball_position = jumps[self.ball_position]
  116.                 self.ball_wheel = 1 - self.ball_wheel
  117.                 console.pegs.remove(console.current_position)
  118.                 wheel = self.current_wheel()
  119.                 if not wheel.is_holding_position(self.ball_position):
  120.                     if self.ball_wheel == target_wheel and self.ball_position == target_position:
  121.                         return 1
  122.                     return -1
  123.             else:
  124.                 return -1
  125.         return 0
  126.  
  127.     def simulate(self):
  128.         while True:
  129.             if self.is_fail():
  130.                 return False
  131.             self.move()
  132.             jump = self.try_jump()
  133.             if jump == 1:
  134.                 return len(self.console_left.pegs) == 0 and len(self.console_right.pegs) == 0
  135.             if jump == -1:
  136.                 return False
  137.  
  138.  
  139. def subsets(s, n):
  140.     return map(lambda x: {} if not x else set(x), itertools.combinations(s, n))
  141.  
  142.  
  143. def is_solution(pegs_left, pegs_right):
  144.     wheel_left = Wheel(wheel_size_left, _holding_positions_left)
  145.     wheel_right = Wheel(wheel_size_right, _holding_positions_right)
  146.     console_left = Console(wheel_size_left, pegs_left)
  147.     console_right = Console(wheel_size_right, pegs_right)
  148.     return Puzzle(wheel_left, wheel_right, console_left, console_right).simulate()
  149.  
  150.  
  151. for i in range(0, 1 + len(available_pegs_left)):
  152.     for j in range(0, 1 + len(available_pegs_right)):
  153.         for left in subsets(available_pegs_left, i):
  154.             for right in subsets(available_pegs_right, j):
  155.                 if is_solution(left.copy(), right.copy()):
  156.                     print(list(left), list(right))
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement