Update day 22 solution
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -5,22 +5,43 @@ from collections import Counter
|
|||||||
|
|
||||||
def part_1(input):
|
def part_1(input):
|
||||||
result = 0
|
result = 0
|
||||||
reactor = set()
|
cubes = Counter()
|
||||||
for line in input:
|
for line in input:
|
||||||
cmd, cubes = line.rstrip().split()
|
cmd, cube = line.rstrip().split()
|
||||||
x, y, z = cubes.split(',')
|
x, y, z = cube.split(',')
|
||||||
x_min, x_max = [int(c) for c in x.split('=')[1].split('..')]
|
xn_min, xn_max = [int(c) for c in x.split('=')[1].split('..')]
|
||||||
y_min, y_max = [int(c) for c in y.split('=')[1].split('..')]
|
yn_min, yn_max = [int(c) for c in y.split('=')[1].split('..')]
|
||||||
z_min, z_max = [int(c) for c in z.split('=')[1].split('..')]
|
zn_min, zn_max = [int(c) for c in z.split('=')[1].split('..')]
|
||||||
on = True if 'on' == cmd else False
|
xn_min = xn_min if xn_min > -50 else -50
|
||||||
for x in range(max(-50, x_min), min(50, x_max) + 1):
|
xn_max = xn_max if xn_max < 51 else 51
|
||||||
for y in range(max(-50, y_min), min(50, y_max) + 1):
|
yn_min = yn_min if yn_min > -50 else -50
|
||||||
for z in range(max(-50, z_min), min(50, z_max) + 1):
|
yn_max = yn_max if yn_max < 51 else 51
|
||||||
if on:
|
zn_min = zn_min if zn_min > -50 else -50
|
||||||
reactor.add((x, y, z))
|
zn_max = zn_max if zn_max < 51 else 51
|
||||||
elif (x, y, z) in reactor:
|
sn = 1 if 'on' == cmd else -1
|
||||||
reactor.remove((x, y, z))
|
if xn_max < xn_min or yn_max < yn_min or zn_max < zn_min:
|
||||||
result = len(reactor)
|
continue
|
||||||
|
update = Counter()
|
||||||
|
for (xi_min, xi_max, yi_min, yi_max, zi_min, zi_max), si in cubes.items():
|
||||||
|
x_min = xn_min if xn_min > xi_min else xi_min
|
||||||
|
x_max = xn_max if xn_max < xi_max else xi_max
|
||||||
|
y_min = yn_min if yn_min > yi_min else yi_min
|
||||||
|
y_max = yn_max if yn_max < yi_max else yi_max
|
||||||
|
z_min = zn_min if zn_min > zi_min else zi_min
|
||||||
|
z_max = zn_max if zn_max < zi_max else zi_max
|
||||||
|
if x_min <= x_max and y_min <= y_max and z_min <= z_max:
|
||||||
|
update[(x_min, x_max, y_min, y_max, z_min, z_max)] -= si
|
||||||
|
if sn > 0:
|
||||||
|
update[(xn_min, xn_max, yn_min, yn_max, zn_min, zn_max)] += sn
|
||||||
|
cubes.update(update)
|
||||||
|
to_delete = []
|
||||||
|
for c in cubes:
|
||||||
|
if not cubes[c]:
|
||||||
|
to_delete.append(c)
|
||||||
|
for d in to_delete:
|
||||||
|
del cubes[d]
|
||||||
|
result = sum((x1 - x0 + 1) * (y1 - y0 + 1) * (z1 - z0 + 1) * sgn
|
||||||
|
for (x0, x1, y0, y1, z0, z1), sgn in cubes.items())
|
||||||
print("Part 1 result:", result)
|
print("Part 1 result:", result)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user