Advertisement
Guest User

AoC 2022 - Day 18

a guest
Dec 20th, 2022
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. """
  2. Advent of Code 2022 Day 18
  3. """
  4. import sys
  5.  
  6. from advent_tools import get_daily_input
  7.  
  8. DAY = 18
  9.  
  10. TEST = sys.argv[1] == "test" if len(sys.argv) > 1 else False
  11.  
  12. TEST_DATA = """
  13. 2,2,2
  14. 1,2,2
  15. 3,2,2
  16. 2,1,2
  17. 2,3,2
  18. 2,2,1
  19. 2,2,3
  20. 2,2,4
  21. 2,2,6
  22. 1,2,5
  23. 3,2,5
  24. 2,1,5
  25. 2,3,5
  26. """
  27.  
  28. if TEST:
  29.     def get_daily_input(_):
  30.         for line in TEST_DATA.strip().split("\n"):
  31.             yield line.strip("\n")
  32.  
  33.  
  34. class Point:
  35.     def __init__(self, x, y, z):
  36.         self.x, self.y, self.z = x, y, z
  37.  
  38.     def __repr__(self):
  39.         return f"{self.x}, {self.y}, {self.z}"
  40.  
  41.  
  42. def part_1() -> int:
  43.     points = [Point(*[int(i) for i in s.split(",")]) for s in get_daily_input(DAY)]
  44.  
  45.     faces = 0
  46.     for p_i in range(len(points)):
  47.         p = points[p_i]
  48.         for r_i in range(p_i + 1, len(points)):
  49.             r = points[r_i]
  50.             faces += 2 * (
  51.                     (p.x == r.x and p.y == r.y and p.z == r.z + 1) +
  52.                     (p.x == r.x and p.y == r.y and p.z == r.z - 1) +
  53.                     (p.x == r.x and p.y == r.y + 1 and p.z == r.z) +
  54.                     (p.x == r.x and p.y == r.y - 1 and p.z == r.z) +
  55.                     (p.x == r.x + 1 and p.y == r.y and p.z == r.z) +
  56.                     (p.x == r.x - 1 and p.y == r.y and p.z == r.z)
  57.             )
  58.     return len(points) * 6 - faces
  59.  
  60.  
  61. def part_2() -> int:
  62.     points = [tuple(int(i) for i in s.split(",")) for s in get_daily_input(DAY)]
  63.  
  64.     min_range = min(min(i) for i in points) - 1
  65.     max_range = max(max(i) for i in points) + 2
  66.  
  67.     faces = 0
  68.     outside_points = [(min_range, min_range, min_range)]
  69.     visited = []
  70.     while outside_points:
  71.         x, y, z = outside_points.pop()
  72.         if (x, y, z) not in visited:
  73.             visited.append((x, y, z))
  74.  
  75.             for ox, oy, oz in [(x + 1, y, z), (x - 1, y, z),
  76.                                (x, y + 1, z), (x, y - 1, z),
  77.                                (x, y, z + 1), (x, y, z - 1)]:
  78.                 if all(min_range <= i <= max_range for i in (ox, oy, oz)):
  79.                     if (ox, oy, oz) in points:
  80.                         faces += 1
  81.                     else:
  82.                         outside_points.append((ox, oy, oz))
  83.     return faces
  84.  
  85.  
  86. def main():
  87.     print(f"Part 1: {part_1()}")
  88.     print(f"Part 2: {part_2()}")
  89.  
  90.  
  91. if __name__ == "__main__":
  92.     main()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement