-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstar1.py
More file actions
52 lines (48 loc) · 1.91 KB
/
star1.py
File metadata and controls
52 lines (48 loc) · 1.91 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
50
51
52
import re, itertools
sampleData="""\
on x=-20..26,y=-36..17,z=-47..7
on x=-20..33,y=-21..23,z=-26..28
on x=-22..28,y=-29..23,z=-38..16
on x=-46..7,y=-6..46,z=-50..-1
on x=-49..1,y=-3..46,z=-24..28
on x=2..47,y=-22..22,z=-23..27
on x=-27..23,y=-28..26,z=-21..29
on x=-39..5,y=-6..47,z=-3..44
on x=-30..21,y=-8..43,z=-13..34
on x=-22..26,y=-27..20,z=-29..19
off x=-48..-32,y=26..41,z=-47..-37
on x=-12..35,y=6..50,z=-50..-2
off x=-48..-32,y=-32..-16,z=-15..-5
on x=-18..26,y=-33..15,z=-7..46
off x=-40..-22,y=-38..-28,z=23..41
on x=-16..35,y=-41..10,z=-47..6
off x=-32..-23,y=11..30,z=-14..3
on x=-49..-5,y=-3..45,z=-29..18
off x=18..30,y=-20..-8,z=-3..13
on x=-41..9,y=-7..43,z=-33..15
on x=-54112..-39298,y=-85059..-49293,z=-27449..7877
on x=967..23432,y=45373..81175,z=27513..53682\
"""
data = open('data.txt', 'r', encoding='utf-8').read()
# data = sampleData
cubes = set()
for line in data.split('\n'):
match = re.match(r'(on|off) x=(-?\d+)\.\.(-?\d+),y=(-?\d+)\.\.(-?\d+),z=(-?\d+)\.\.(-?\d+)', line)
status = match.group(1)
x_start, x_end = int(match.group(2)), int(match.group(3))
y_start, y_end = int(match.group(4)), int(match.group(5))
z_start, z_end = int(match.group(6)), int(match.group(7))
if not -50 <= x_start <= 50 or not -50 <= x_end <= 50 \
or not -50 <= y_start <= 50 or not -50 <= y_end <= 50 \
or not -50 <= z_start <= 50 or not -50 <= z_end <= 50:
continue
x_range = range(int(match.group(2)), int(match.group(3))+1)
y_range = range(int(match.group(4)), int(match.group(5))+1)
z_range = range(int(match.group(6)), int(match.group(7))+1)
combinations = list(itertools.product(x_range, y_range, z_range))
for combination in combinations:
if status == 'on':
cubes.add(combination)
elif status == 'off' and combination in cubes:
cubes.remove(combination)
print(len(cubes))