Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2021
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. class Cube:
  2.     def __init__(self):
  3.         self.x_int = [None, None]
  4.         self.y_int = [None, None]
  5.         self.z_int = [None, None]
  6.         self.type="on"
  7.  
  8.     def overlap(self, other: 'Cube'):
  9.         """Determines the volume of the cross section.
  10.        """
  11.         # Initial checks of intervals
  12.         if other.x_int[0] > self.x_int[1] or self.x_int[0] > other.x_int[1]:
  13.             return None
  14.         if other.y_int[0] > self.y_int[1] or self.y_int[0] > other.y_int[1]:
  15.             return None
  16.         if other.z_int[0] > self.z_int[1] or self.z_int[0] > other.z_int[1]:
  17.             return None
  18.  
  19.         # Get the new intervals
  20.         x_min = max(self.x_int[0], other.x_int[0])
  21.         x_max = min(self.x_int[1], other.x_int[1])
  22.  
  23.         y_min = max(self.y_int[0], other.y_int[0])
  24.         y_max = min(self.y_int[1], other.y_int[1])
  25.  
  26.         z_min = max(self.z_int[0], other.z_int[0])
  27.         z_max = min(self.z_int[1], other.z_int[1])
  28.  
  29.         new_cube = Cube()
  30.         new_cube.x_int = [x_min, x_max]
  31.         new_cube.y_int = [y_min, y_max]
  32.         new_cube.z_int = [z_min, z_max]
  33.         return new_cube
  34.  
  35.     def volume(self):
  36.  
  37.         volume = (abs(self.x_int[1] - self.x_int[0]) + 1) * (abs(self.y_int[1] - self.y_int[0]) + 1) * (abs(self.z_int[1] - self.z_int[0]) + 1)
  38.         return volume
  39.  
  40. file = "day_22.dat"
  41. volume_sum = 0
  42. cubes = []
  43. with open(file, 'r') as f:
  44.     for line in f:
  45.         sline = line.split()
  46.  
  47.         # Intervals
  48.         ranges = sline[1].split(',')
  49.         x_int = [int(_) for _ in ranges[0][2:].split("..")]
  50.         y_int = [int(_) for _ in ranges[1][2:].split("..")]
  51.         z_int = [int(_) for _ in ranges[2][2:].split("..")]
  52.  
  53.         el = Cube()
  54.         el.x_int = x_int
  55.         el.y_int = y_int
  56.         el.z_int = z_int
  57.         el.type = sline[0]
  58.         cubes.append(el)
  59.  
  60. opposite = {"off": "on", "on": "off"}
  61. cube = None
  62. new_cube = None
  63. other_cube = None
  64. def part_one(part_one=True) -> int:
  65.     processed = []
  66.     for cube in cubes:
  67.         # Filter out the -50 to 50 region
  68.         if part_one:
  69.             if cube.x_int[0] < -50:
  70.                 continue
  71.             if cube.x_int[1] > 50:
  72.                 continue
  73.             if cube.y_int[0] < -50:
  74.                 continue
  75.             if cube.y_int[1] > 50:
  76.                 continue
  77.             if cube.z_int[0] < -50:
  78.                 continue
  79.             if cube.z_int[1] > 50:
  80.                 continue
  81.  
  82.         # Find all the cross sections
  83.         new_cubes = []
  84.         for other_cube in processed:
  85.             new_cube = other_cube.overlap(cube)
  86.             if new_cube:
  87.                 if other_cube.type == cube.type:
  88.                     # Mark it as opposite cube
  89.                     # Meaning if both cubes are on assign this as off and and
  90.                     # vice verse if both cubes are off
  91.                     new_cube.type = opposite[cube.type]
  92.                 else:
  93.                     # Now assign the cube type depending on what what before
  94.                     # and what is now
  95.                     if other_cube.type == "on":
  96.                         # Means that cube.type = off
  97.                         new_cube.type = "off"
  98.                     else: # Means that we flip this region on
  99.                         new_cube.type = "on"
  100.                 new_cubes.append(new_cube)
  101.  
  102.         # Append the cubes in order
  103.         if cube.type == "on":
  104.             processed.append(cube)
  105.         processed += new_cubes
  106.  
  107.     # Now sum all the processed cubes
  108.     c = 0
  109.     for cube in processed:
  110.         volume = cube.volume()
  111.         if cube.type == "on":
  112.             c += volume
  113.         else:
  114.             c -= volume
  115.  
  116.     return c
  117.  
  118. print("Part 1:", part_one())
  119. print("Part 2:", part_one(part_one=False))
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement