Advertisement
tomdodd4598

Untitled

Oct 5th, 2021
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.35 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. available_pegs_left = set(range(wheel_size_left)) - blocked_pegs_left
  41. available_pegs_right = set(range(wheel_size_right)) - blocked_pegs_right
  42.  
  43.  
  44. class Wheel:
  45.     def __init__(self, size, raw_holding_positions: set):
  46.         self.size = size
  47.         self.raw_holding_positions = raw_holding_positions
  48.         self.rotation = 0
  49.  
  50.     def rotate(self):
  51.         self.rotation += 1
  52.         self.rotation %= self.size
  53.  
  54.     def is_holding_position(self, position):
  55.         position += (self.size - self.rotation)
  56.         return position % self.size in self.raw_holding_positions
  57.  
  58.  
  59. class Console:
  60.     def __init__(self, size, pegs: set):
  61.         self.size = size
  62.         self.pegs = pegs
  63.         self.current_position = 0
  64.  
  65.     def increment_position(self):
  66.         self.current_position += 1
  67.         self.current_position %= self.size
  68.  
  69.  
  70. class Puzzle:
  71.     def __init__(self, wheel_left: Wheel, wheel_right: Wheel, console_left: Console, console_right: Console):
  72.         self.wheel_left = wheel_left
  73.         self.wheel_right = wheel_right
  74.         self.console_left = console_left
  75.         self.console_right = console_right
  76.         self.ball_wheel = start_wheel
  77.         self.ball_position = start_position
  78.  
  79.     def current_wheel(self):
  80.         return self.wheel_left if self.ball_wheel == 0 else self.wheel_right
  81.  
  82.     def current_console(self):
  83.         return self.console_left if self.ball_wheel == 0 else self.console_right
  84.  
  85.     def is_fail(self):
  86.         wheel = self.current_wheel()
  87.         if not wheel.is_holding_position(self.ball_position):
  88.             return True
  89.         console = self.current_console()
  90.         if len(console.pegs) == 0 or max(console.pegs) < console.current_position:
  91.             return True
  92.         return False
  93.  
  94.     def move(self):
  95.         wheel = self.current_wheel()
  96.         wheel.rotate()
  97.         self.ball_position += (wheel.size - 1)
  98.         self.ball_position %= wheel.size
  99.         self.current_console().increment_position()
  100.  
  101.     def get_jumps(self) -> dict:
  102.         return jumps_left_to_right if self.ball_wheel == 0 else jumps_right_to_left
  103.  
  104.     def try_jump(self):
  105.         console = self.current_console()
  106.         if console.current_position in console.pegs:
  107.             jumps = self.get_jumps()
  108.             if self.ball_position in jumps:
  109.                 self.ball_position = jumps[self.ball_position]
  110.                 self.ball_wheel = 1 - self.ball_wheel
  111.                 console.pegs.remove(console.current_position)
  112.                 wheel = self.current_wheel()
  113.                 if not wheel.is_holding_position(self.ball_position):
  114.                     if self.ball_wheel == target_wheel and self.ball_position == target_position:
  115.                         return 1
  116.                     return -1
  117.             else:
  118.                 return -1
  119.         return 0
  120.  
  121.     def simulate(self):
  122.         while True:
  123.             if self.is_fail():
  124.                 return False
  125.             self.move()
  126.             jump = self.try_jump()
  127.             if jump == 1:
  128.                 return len(self.console_left.pegs) == 0 and len(self.console_right.pegs) == 0
  129.             if jump == -1:
  130.                 return False
  131.  
  132.  
  133. def subsets(s, n):
  134.     return map(lambda x: {} if not x else set(x), itertools.combinations(s, n))
  135.  
  136.  
  137. def is_solution(pegs_left, pegs_right):
  138.     wheel_left = Wheel(wheel_size_left, holding_positions_left)
  139.     wheel_right = Wheel(wheel_size_right, holding_positions_right)
  140.     console_left = Console(wheel_size_left, pegs_left)
  141.     console_right = Console(wheel_size_right, pegs_right)
  142.     return Puzzle(wheel_left, wheel_right, console_left, console_right).simulate()
  143.  
  144.  
  145. for i in range(0, 1 + len(available_pegs_left)):
  146.     for j in range(0, 1 + len(available_pegs_right)):
  147.         for left in subsets(available_pegs_left, i):
  148.             for right in subsets(available_pegs_right, j):
  149.                 if is_solution(left.copy(), right.copy()):
  150.                     print(list(left), list(right))
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement