-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.1.py
More file actions
49 lines (46 loc) · 1.62 KB
/
22.1.py
File metadata and controls
49 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
print(chr(27)+'[2j')
print('\033c')
f = open('22.test', 'r')
f = open('22.test2', 'r')
f = open('22.input', 'r')
data = [x.strip() for x in f.readlines()]
data = data[:-2]
def clamp1(x, minimum, maximum):
a,b = x
fully_outside = (a < minimum and b < minimum) or (a > maximum and b > maximum)
if fully_outside:
return None
return (
max(min(a, maximum), minimum),
max(min(b, maximum), minimum),
)
def part1(data):
on = set()
for cube in data:
action, area = cube.split(' ')
xarea, yarea,zarea = area.split(',')
xrange = tuple(map(int, xarea[2:].split('..')))
yrange = tuple(map(int, yarea[2:].split('..')))
zrange = tuple(map(int, zarea[2:].split('..')))
xrange = clamp1(xrange, -50, 50)
yrange = clamp1(yrange, -50, 50)
zrange = clamp1(zrange, -50, 50)
if not xrange or not yrange or not zrange:
continue
count = 0
if action =='on':
for z in range(zrange[0], zrange[1] + 1):
for y in range(yrange[0], yrange[1] + 1):
for x in range(xrange[0], xrange[1] + 1):
if (x,y,z) not in on:
on.add((x,y,z))
count += 1
else:
for z in range(zrange[0], zrange[1] + 1):
for y in range(yrange[0], yrange[1] + 1):
for x in range(xrange[0], xrange[1] + 1):
if (x,y,z) in on:
on.remove((x,y,z))
count += 1
print('Solution part 1: %d' % len(on))
part1(data)