Advertisement
Guest User

Untitled

a guest
Dec 14th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #Get all data and divide per line
  2. data = open("Day 14.txt",'r').read().split('\n')
  3.  
  4. #Set initial values
  5. rocks = []
  6. sands = []
  7. size = [[500,500],[0,0]]
  8. start = [500,0]
  9. grains = 0
  10. first = True
  11.  
  12. #Define printing image
  13. def printImage(image, extra):
  14. print('')
  15. for row in range(size[1][0],size[1][1]+3):
  16. for col in range(size[0][0],size[0][1]+1):
  17. #print(col,row)
  18. if [col,row] == start:
  19. print('@',end='')
  20. elif [col,row] in extra:
  21. print('O',end='')
  22. elif [col,row] in image:
  23. print('#',end='')
  24. else:
  25. print('.',end='')
  26. print('')
  27. print('')
  28.  
  29. #Get rock formations
  30. for row in data:
  31. #Get points
  32. points = row.split(' -> ')
  33.  
  34. #Go through all points
  35. for i in range(len(points)-1):
  36. #Set line
  37. line = [[*eval(points[i])],[*eval(points[i+1])]]
  38.  
  39. #Check if horizontal or vertical
  40. if line[0][0] == line[1][0]:
  41. #Get direction
  42. sign = int((line[1][1]-line[0][1])/abs(line[0][1]-line[1][1]))
  43.  
  44. #Go through the line
  45. for point in range(line[0][1],line[1][1],sign):
  46. #Add to rocks
  47. rocks.append([line[0][0],point])
  48.  
  49. if line[0][1] == line[1][1]:
  50. #Get direction
  51. sign = int((line[1][0]-line[0][0])/abs(line[0][0]-line[1][0]))
  52.  
  53. #Go through the line
  54. for point in range(line[0][0],line[1][0],sign):
  55. #Add to rocks
  56. rocks.append([point,line[0][1]])
  57. else:
  58. rocks.append([*eval(points[i+1])])
  59.  
  60. #Get size of field and add margin
  61. for rock in rocks:
  62. if rock[0] < size[0][0]:
  63. size[0][0] = rock[0]
  64. if rock[0] > size[0][1]:
  65. size[0][1] = rock[0]
  66. if rock[1] < size[1][0]:
  67. size[1][0] = rock[1]
  68. if rock[1] > size[1][1]:
  69. size[1][1] = rock[1]
  70.  
  71.  
  72. #Part 2
  73. #Add floor to rocks
  74. height = size[1][1]+2
  75. for i in range(start[0]-height,start[0]+height+1):
  76. rocks.append([i,height])
  77.  
  78. #Keep going until end point reached
  79. while True:
  80. #Set starting value
  81. sand = start.copy()
  82.  
  83. #Check if grain is already in rocks
  84. if sand in rocks:
  85. printImage(rocks, sands)
  86. print("Total grains to fill start:",grains)
  87. break
  88.  
  89. #Give an update every 1000 grains
  90. if grains%1000 == 0:
  91. printImage(rocks, sands)
  92. if grains%100 == 0:
  93. print(grains,"grains")
  94.  
  95. while True:
  96. #Check if grain has reached out of bounds for the first time
  97. if sand[1] > size[1][1] and first:
  98. #Part 1
  99. printImage(rocks, sands)
  100. print("Total amount of grains before out of bounds:",grains)
  101. first = False
  102.  
  103. #Check directly below
  104. if [sand[0],sand[1]+1] not in rocks:
  105. sand[1] += 1
  106. continue
  107.  
  108. #Check bottom-left
  109. if [sand[0]-1,sand[1]+1] not in rocks:
  110. sand[0] -= 1
  111. sand[1] += 1
  112. continue
  113.  
  114. #Check bottom-right
  115. if [sand[0]+1,sand[1]+1] not in rocks:
  116. sand[0] += 1
  117. sand[1] += 1
  118. continue
  119.  
  120. #If grain has nowhere to move, add it to rocks and start new grain
  121. rocks.append(sand)
  122. sands.append(sand)
  123.  
  124. #Increment grain
  125. grains += 1
  126. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement