Add 2022 solutions
This commit is contained in:
71
2022/d18.py
Normal file
71
2022/d18.py
Normal file
@@ -0,0 +1,71 @@
|
||||
from lib import *
|
||||
|
||||
EXAMPLE = """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
|
||||
"""
|
||||
|
||||
|
||||
def solve(input: Input, second=False):
|
||||
cubes = set([tuple(map(int, l.split(",")))for l in input.text.splitlines()])
|
||||
res = len(cubes) * 6
|
||||
|
||||
if second:
|
||||
min_x = min([c[0] for c in cubes]) - 1
|
||||
max_x = max([c[0] for c in cubes]) + 1
|
||||
min_y = min([c[1] for c in cubes]) - 1
|
||||
max_y = max([c[1] for c in cubes]) + 1
|
||||
min_z = min([c[2] for c in cubes]) - 1
|
||||
max_z = max([c[2] for c in cubes]) + 1
|
||||
surrounded = set([(x, y, z)
|
||||
for x in range(min_x, max_x + 1)
|
||||
for y in range(min_y, max_y + 1)
|
||||
for z in range(min_z, max_z + 1)])
|
||||
surrounded -= cubes
|
||||
vs = [(min_x, min_y, min_z)]
|
||||
while vs:
|
||||
(x, y, z) = vs.pop()
|
||||
for dx, dy, dz in [(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)]:
|
||||
t = x + dx, y + dy, z + dz
|
||||
if t in surrounded:
|
||||
surrounded.remove(t)
|
||||
vs.append(t)
|
||||
for x, y, z in list(surrounded):
|
||||
for dx, dy, dz in [(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)]:
|
||||
nc = x + dx, y + dy, z + dz
|
||||
if nc in cubes:
|
||||
res -= 1
|
||||
|
||||
for x, y, z in list(cubes):
|
||||
for dx, dy, dz in [(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)]:
|
||||
nc = x + dx, y + dy, z + dz
|
||||
if nc in cubes:
|
||||
res -= 1
|
||||
return res
|
||||
|
||||
def main():
|
||||
DAY_INPUT = "d18.txt"
|
||||
|
||||
print("Example 1:", solve(Input(EXAMPLE)))
|
||||
print("Solution 1:", solve(Input(DAY_INPUT)))
|
||||
# 10:30........
|
||||
|
||||
print("Example 2:", solve(Input(EXAMPLE), True))
|
||||
print("Solution 2:", solve(Input(DAY_INPUT), True))
|
||||
assert solve(Input(DAY_INPUT), True) == 2064
|
||||
# 30:00
|
||||
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user