Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2018
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import re
  4.  
  5. def get_bots(values):
  6.     r = re.compile("pos=<([0-9-]+),([0-9-]+),([0-9-]+)>, r=([0-9]+)")
  7.     bots = []
  8.     for cur in values:
  9.         m = r.search(cur)
  10.         if m is None:
  11.             # FIX: Use the right form of print for Python 3
  12.             print(cur)
  13.         bots.append([int(x) for x in m.groups()])
  14.     return bots
  15.  
  16.  
  17. def calc(values):
  18.     bots = get_bots(values)
  19.     best_i = None
  20.     best_val = None
  21.     for i in range(len(bots)):
  22.         if best_i is None or bots[i][3] > best_val:
  23.             best_val = bots[i][3]
  24.             best_i = i
  25.  
  26.     bx, by, bz, bdist = bots[best_i]
  27.  
  28.     ret = 0
  29.  
  30.     for i in range(len(bots)):
  31.         x, y, z, _dist = bots[i]
  32.  
  33.         if abs(x - bx) + abs(y - by) + abs(z - bz) <= bdist:
  34.             ret += 1
  35.  
  36.     return ret
  37.  
  38.  
  39. def calc2(values):
  40.     bots = get_bots(values)
  41.     # FIX: Adding [0] to each range to make sure it's tested for
  42.     xs = [x[0] for x in bots] + [0]
  43.     ys = [x[1] for x in bots] + [0]
  44.     zs = [x[2] for x in bots] + [0]
  45.  
  46.     dist = 1
  47.     while dist < max(xs) - min(xs):
  48.         dist *= 2
  49.  
  50.     while True:
  51.         target_count = 0
  52.         best = None
  53.         best_val = None
  54.         for x in range(min(xs), max(xs) + 1, dist):
  55.             for y in range(min(ys), max(ys) + 1, dist):
  56.                 for z in range(min(zs), max(zs) + 1, dist):
  57.                     count = 0
  58.                     for bx, by, bz, bdist in bots:
  59.                         calc = abs(x - bx) + abs(y - by) + abs(z - bz)
  60.                         # FIX: Python 3 changes how div works, we want integer math here
  61.                         if (calc - bdist) // dist <= 0:
  62.                             count += 1
  63.                     if count > target_count:
  64.                         target_count = count
  65.                         best_val = abs(x) + abs(y) + abs(z)
  66.                         best = (x, y, z)
  67.                     elif count == target_count:
  68.                         if best_val is None or abs(x) + abs(y) + abs(z) < best_val:
  69.                             best_val = abs(x) + abs(y) + abs(z)
  70.                             best = (x, y, z)
  71.  
  72.         if dist == 1:
  73.             print("The max count I found was: " + str(target_count))
  74.             return best_val
  75.         else:
  76.             xs = [best[0] - dist, best[0] + dist]
  77.             ys = [best[1] - dist, best[1] + dist]
  78.             zs = [best[2] - dist, best[2] + dist]
  79.             # FIX: Python 3 changes how div works, we want integer math here
  80.             dist = dist // 2
  81.  
  82.  
  83. def run(values):
  84.     print("Nearest the big bot: " + str(calc(values)))
  85.     print("Best location value: " + str(calc2(values)))
  86.  
  87.  
  88. def load_and_run(filename):
  89.     print("------ %s ------" % (filename,))
  90.     values = []
  91.     with open(filename) as f:
  92.         for cur in f:
  93.             values.append(cur.strip("\r\n"))
  94.     run(values)
  95.  
  96.  
  97. def main():
  98.     load_and_run("day_23_input_from_lordtnt.txt")
  99.     load_and_run("day_23_input_from_halluci293.txt")
  100.  
  101.  
  102. if __name__ == "__main__":
  103.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement