Browse Source

finished day 18

main
Luna Lailatova 1 year ago
parent
commit
8ebc6430ae
  1. 27
      day18-1.py
  2. 97
      day18-2.py
  3. 14
      in.txt
  4. 2192
      input18.txt

27
day18-1.py

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
#reading input
with open('input18.txt','r') as f:
inp = f.read().splitlines(keepends=False)
cubes = []
sides = {}
surface = 0
for line in inp:
x, y, z = line.split(',')
cubes.append((int(x), int(y), int(z)))
#main function
for cube in cubes:
x, y, z = cube
sides[((x - 1, x),(y, y),(z, z))] = sides.get(((x - 1, x),(y, y),(z, z)), 0) + 1
sides[((x, x + 1),(y, y),(z, z))] = sides.get(((x, x + 1),(y, y),(z, z)), 0) + 1
sides[((x, x),(y - 1, y),(z, z))] = sides.get(((x, x),(y - 1, y),(z, z)), 0) + 1
sides[((x, x),(y, y + 1),(z, z))] = sides.get(((x, x),(y, y + 1),(z, z)), 0) + 1
sides[((x, x),(y, y),(z - 1, z))] = sides.get(((x, x),(y, y),(z - 1, z)), 0) + 1
sides[((x, x),(y, y),(z, z + 1))] = sides.get(((x, x),(y, y),(z, z + 1)), 0) + 1
for side in sides:
if sides[side] == 1:
surface += 1
print(surface)

97
day18-2.py

@ -0,0 +1,97 @@ @@ -0,0 +1,97 @@
#reading input
with open('input18.txt','r') as f:
inp = f.read().splitlines(keepends=False)
cubes = []
sides = {}
surface = 0
maxX = None
maxY = None
maxZ = None
minX = None
minY = None
minZ = None
bubbles = []
bubbleSides = {}
insideSurface = 0
for line in inp:
x, y, z = line.split(',')
x = int(x)
y = int(y)
z = int(z)
if maxX == None or x > maxX:
maxX = x
if minX == None or x < minX:
minX = x
if maxY == None or y > maxY:
maxY = y
if minY == None or y < minY:
minY = y
if maxZ == None or z > maxZ:
maxZ = z
if minZ == None or z < minZ:
minZ = z
cubes.append((x, y, z))
#main function
for cube in cubes:
x, y, z = cube
sides[((x - 1, x),(y, y),(z, z))] = sides.get(((x - 1, x),(y, y),(z, z)), 0) + 1
sides[((x, x + 1),(y, y),(z, z))] = sides.get(((x, x + 1),(y, y),(z, z)), 0) + 1
sides[((x, x),(y - 1, y),(z, z))] = sides.get(((x, x),(y - 1, y),(z, z)), 0) + 1
sides[((x, x),(y, y + 1),(z, z))] = sides.get(((x, x),(y, y + 1),(z, z)), 0) + 1
sides[((x, x),(y, y),(z - 1, z))] = sides.get(((x, x),(y, y),(z - 1, z)), 0) + 1
sides[((x, x),(y, y),(z, z + 1))] = sides.get(((x, x),(y, y),(z, z + 1)), 0) + 1
for side in sides:
if sides[side] == 1:
surface += 1
for x in range(minX, maxX + 1):
for y in range(minY, maxY + 1):
for z in range(minZ, maxZ + 1):
bubbles.append((x, y, z))
for cube in cubes:
if cube in bubbles:
bubbles.remove(cube)
outsideBubbles = []
for bubble in bubbles:
x, y, z = bubble
if x in [minX, maxX] or y in [minY, maxY] or z in [minZ, maxZ]:
outsideBubbles.append((x, y, z))
done = False
while not done:
done = True
for bubble in outsideBubbles:
bubbleSides.setdefault(bubble, 1)
for bubble in bubbles:
x, y, z = bubble
if any(i in bubbleSides for i in((x - 1, y, z), (x + 1, y, z), (x, y - 1, z), (x, y + 1, z), (x, y, z - 1), (x, y, z + 1))):
if (x, y, z) not in outsideBubbles:
outsideBubbles.append((x, y, z))
done = False
for bubble in outsideBubbles:
if bubble in bubbles:
bubbles.remove(bubble)
bubbleSides = {}
for bubble in bubbles:
x, y, z = bubble
bubbleSides[((x - 1, x),(y, y),(z, z))] = bubbleSides.get(((x - 1, x),(y, y),(z, z)), 0) + 1
bubbleSides[((x, x + 1),(y, y),(z, z))] = bubbleSides.get(((x, x + 1),(y, y),(z, z)), 0) + 1
bubbleSides[((x, x),(y - 1, y),(z, z))] = bubbleSides.get(((x, x),(y - 1, y),(z, z)), 0) + 1
bubbleSides[((x, x),(y, y + 1),(z, z))] = bubbleSides.get(((x, x),(y, y + 1),(z, z)), 0) + 1
bubbleSides[((x, x),(y, y),(z - 1, z))] = bubbleSides.get(((x, x),(y, y),(z - 1, z)), 0) + 1
bubbleSides[((x, x),(y, y),(z, z + 1))] = bubbleSides.get(((x, x),(y, y),(z, z + 1)), 0) + 1
for side in bubbleSides:
if bubbleSides[side] == 1:
insideSurface += 1
print(surface - insideSurface)

14
in.txt

@ -1 +1,13 @@ @@ -1 +1,13 @@
>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>
2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5

2192
input18.txt

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save