Advertisement
authorblues

Routing Mario's Time Machine

Dec 17th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. # optimize Mario's Time Machine
  2. from collections import namedtuple
  3. from itertools import permutations
  4.  
  5. LOCATION_WHEEL = [
  6.     'Menlo Park',
  7.     'Orleans',
  8.     'London',
  9.     'Alexandria',
  10.     'Calcutta',
  11.     'Mainz',
  12.     'Philadelphia',
  13.     'Vienna',
  14.     'The Pacific',
  15.     'Gobi Desert',
  16.     'Florence',
  17.     'Cambridge',
  18.     'Athens',
  19.     'Stratford',
  20. ]
  21.  
  22. class Destination(namedtuple('Destination', ['obj', 'pos', 'location', 'year', 'ad'])):
  23.     __slots__ = ()
  24.     def __str__(self):
  25.         return '{} @ position {} ({}, {} {})'.format(self.obj, self.pos,
  26.             self.location, self.year, 'AD' if self.ad else 'BC')
  27.  
  28. INITIAL_POSITION = Destination(obj=None, pos=None, location='Menlo Park', year='1993', ad=True)
  29. FLOORS = [
  30.     [
  31.         Destination(obj='apple', pos=1, location='Cambridge', year='1867', ad=True),
  32.         Destination(obj='shield', pos=2, location='Orleans', year='1429', ad=True),
  33.         Destination(obj='papers', pos=3, location='Florence', year='1505', ad=True),
  34.         Destination(obj='music', pos=4, location='Vienna', year='1824', ad=True),
  35.         Destination(obj='declaration', pos=5, location='Philadelphia', year='1776', ad=True),
  36.     ],
  37.     [
  38.         Destination(obj='filament', pos=1, location='Menlo Park', year='1879', ad=True),
  39.         Destination(obj='block', pos=2, location='Gobi Desert', year='1292', ad=True),
  40.         Destination(obj='book', pos=3, location='Athens', year='0369', ad=False),
  41.         Destination(obj='crown', pos=4, location='London', year='1595', ad=True),
  42.         Destination(obj='flag', pos=5, location='Calcutta', year='1947', ad=True),
  43.     ],
  44.     [
  45.         Destination(obj='printer', pos=1, location='Mainz', year='1455', ad=True),
  46.         Destination(obj='globe', pos=2, location='The Pacific', year='1521', ad=True),
  47.         Destination(obj='chisel', pos=3, location='Florence', year='1503', ad=True),
  48.         Destination(obj='skull', pos=4, location='Stratford', year='1601', ad=True),
  49.         Destination(obj='staff', pos=5, location='Alexandria', year='0047', ad=False),
  50.     ],
  51. ]
  52.  
  53. LOCATION_WHEEL_TIME = 64
  54. YEAR_WHEEL_TIME = 68
  55. AD_SLIDER_TIME = 16
  56.  
  57. def time_between(a, b):
  58.     destdiff = abs(LOCATION_WHEEL.index(a.location) - LOCATION_WHEEL.index(b.location))
  59.     destdiff = min(destdiff, len(LOCATION_WHEEL) - destdiff)
  60.    
  61.     yeardiff = 0
  62.     for x,y in zip(a.year, b.year):
  63.         xi, yi = int(x), int(y)
  64.         yeardiff += min(10 - abs(xi-yi), abs(xi-yi))
  65.    
  66.     adslider = AD_SLIDER_TIME if a.ad != b.ad else 0
  67.     return destdiff * LOCATION_WHEEL_TIME + yeardiff * YEAR_WHEEL_TIME + adslider
  68.  
  69. best, bcost = None, 1000000000000
  70. for floor1 in permutations(FLOORS[0]):
  71.     for floor2 in permutations(FLOORS[1]):
  72.         for floor3 in permutations(FLOORS[2]):
  73.             order = floor1 + floor2 + floor3
  74.             cost = time_between(INITIAL_POSITION, order[0])
  75.             for x,y in zip(order[:-1], order[1:]):
  76.                 cost += time_between(x,y)
  77.             if cost < bcost:
  78.                 best, bcost = order, cost
  79.                
  80. print("\n".join(str(dest) for dest in best))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement