Advertisement
petrlos

Untitled

Dec 19th, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. #Advent of Code 2022: Day 18
  2. from datetime import datetime
  3. from collections import deque
  4. start = datetime.now()
  5. def create_cubes(lines):
  6.     cubes = set()
  7.     for line in lines:
  8.         cube = tuple(map(int,line.split(",")))
  9.         cubes.add(cube)
  10.     return cubes
  11.  
  12. def create_directions():
  13.     directions = []
  14.     all_directions = set([(x, y, z) for x in range(-1,2) for y in range(-1, 2) for z in range(-1,2)])
  15.     for direction in all_directions:
  16.         if manhDistance(direction, (0,0,0)) == 1:
  17.             directions.append(direction)
  18.     return directions
  19.  
  20. def tuple_sum(a,b):
  21.     return tuple([x + y for x, y in zip(a,b)])
  22.  
  23. def manhDistance(a, b):
  24.     return sum(abs(val1-val2) for val1, val2 in zip(a,b))
  25.  
  26. def check_neighbour(cube):
  27.     open_sides = 6
  28.     for direction in directions:
  29.             neighbour = tuple_sum(direction, cube)
  30.             if neighbour in cubes:
  31.                 open_sides -= 1
  32.     return open_sides
  33.  
  34. def check_sides(cubes):
  35.     counter = 0
  36.     for cube in cubes:
  37.         open_sides = check_neighbour(cube)
  38.         counter += open_sides
  39.     return counter
  40.  
  41. def count_surface(water_surface):
  42.     counter = 0
  43.     for cube in cubes:
  44.         for direction in directions:
  45.             if tuple_sum(direction, cube) in water_surface:
  46.                 counter += 1
  47.     return counter
  48.  
  49. #MAIN
  50. with open("data.txt") as file:
  51.     lines = file.read().splitlines()
  52. #cubes = create_cubes(lines)
  53. directions = create_directions()
  54.  
  55. #Cuve 3x3x3 with mid segment+one corner missing
  56. cubes = set((x,y,z) for x in range(2,5) for y in range(2,5) for z in range(2,5))
  57. cubes.remove((3,3,3))
  58. cubes.remove((2,2,2))
  59.  
  60. #Task1:
  61. task1 = check_sides(cubes)
  62. print("Task 1:", task1)
  63. print("Runtime:", datetime.now()-start)
  64.  
  65. #Task2
  66. max_size = 24
  67. all_cubes = set((x,y,z) for x in range(0, max_size+1) for y in range(0, max_size+1) for z in range(0,max_size+1))
  68. start_cube = (1,1,1)
  69. queue = deque([start_cube])
  70. water_surface = set()
  71. while queue:
  72.     current_cube = queue[0]
  73.     for direction in directions:
  74.         neighbour = tuple_sum(direction, current_cube)
  75.         if neighbour in all_cubes and neighbour not in cubes:
  76.             if check_neighbour(neighbour) <= 5:
  77.                 water_surface.add(neighbour)
  78.             queue.append(neighbour)
  79.             all_cubes.remove(neighbour)
  80.     queue.popleft()
  81.  
  82. size = 6
  83. for z in range(0,size):
  84.     print("Layer:",z)
  85.     for y in range(0,size):
  86.         for x in range(0,size):
  87.             if (x,y,z) in cubes:
  88.                 print("#", end="")
  89.             elif (x,y,z) in water_surface:
  90.                 print(".", end="")
  91.             else:
  92.                 print(" ", end="")
  93.         print(" ")
  94.     print(" ")
  95.  
  96. print(count_surface(water_surface))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement