1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ split_data = True
5+ completed = True
6+ raw_data = None # Not To be touched
7+
8+ def part1 (data ):
9+ # One easy way is to just calculate the laviathan distance and if its 1 then subtract 2 from the upper limit
10+ limit = len (data ) * 6
11+
12+ checks = [(- 1 , 0 , 0 ), (1 , 0 , 0 ), (0 , - 1 , 0 ), (0 , 1 , 0 ), (0 , 0 , - 1 ), (0 , 0 , 1 )]
13+
14+ for cube in data :
15+ x , y , z = map (int , cube .split (',' ))
16+
17+ for dx , dy , dz in checks :
18+ nx , ny , nz = x + dx , y + dy , z + dz
19+ if f"{ nx } ,{ ny } ,{ nz } " in data :
20+ limit -= 1
21+
22+ return limit
23+
24+ def part2 (data ):
25+ # Will be solved using flood-fill
26+ cubes = []
27+ maxX = float ('-inf' )
28+ maxY = float ('-inf' )
29+ maxZ = float ('-inf' )
30+
31+ for cube in data :
32+ x , y , z = map (int , cube .split (',' ))
33+ maxX = max (maxX , x )
34+ maxY = max (maxY , y )
35+ maxZ = max (maxZ , z )
36+ cubes .append ((x , y , z ))
37+
38+ maxX , maxY , maxZ = maxX + 2 , maxY + 2 , maxZ + 2 # <- God knows why I have a two off error!
39+
40+ # print(f'{maxX}x{maxY}x{maxZ} grid')
41+
42+ surface = 0 # The number of times we hit the cube during the flood fill
43+
44+ checks = [(- 1 , 0 , 0 ), (1 , 0 , 0 ), (0 , - 1 , 0 ), (0 , 1 , 0 ), (0 , 0 , - 1 ), (0 , 0 , 1 )]
45+ queue = [(- 1 , - 1 , - 1 )]
46+ visited = set ()
47+
48+ while queue :
49+ x , y , z = queue .pop (0 )
50+
51+ for dx , dy , dz in checks :
52+ nx , ny , nz = x + dx , y + dy , z + dz
53+ if not (- 1 <= nx < maxX and - 1 <= ny < maxY and - 1 <= nz < maxZ ): continue
54+ if f"{ nx } ,{ ny } ,{ nz } " in visited : continue
55+ if (nx , ny , nz ) in cubes :
56+ surface += 1
57+ continue
58+ queue .append ((nx , ny , nz ))
59+ visited .add (f"{ nx } ,{ ny } ,{ nz } " )
60+
61+ return surface
0 commit comments