Tidy up openscad-generate.py
This commit is contained in:
parent
72364791bf
commit
2b91880512
3 changed files with 33 additions and 13 deletions
|
@ -1,6 +1,6 @@
|
|||
import sys
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Tuple, NamedTuple, Generator
|
||||
from typing import List, NamedTuple, Generator
|
||||
|
||||
|
||||
# This script exists to generate OpenSCAD code to represent challenge inputs.
|
||||
|
@ -18,10 +18,6 @@ class Point(NamedTuple):
|
|||
def to_list(self) -> List[int]:
|
||||
return [self.x, self.y, self.z]
|
||||
|
||||
def apply_point_shift(point: List[int], shift: Tuple[int, int, int]) -> List[int]:
|
||||
x, y, z = point
|
||||
xs, ys, zs = shift
|
||||
return [x+abs(xs), y+abs(ys), z+abs(zs)]
|
||||
|
||||
@dataclass
|
||||
class Shape:
|
||||
|
@ -31,7 +27,7 @@ class Shape:
|
|||
|
||||
def __init__(self, mode, x, y, z: str):
|
||||
self.mode = mode
|
||||
|
||||
|
||||
x1, x2 = [int(a) for a in x.split("..")]
|
||||
y1, y2 = [int(a) for a in y.split("..")]
|
||||
z1, z2 = [int(a) for a in z.split("..")]
|
||||
|
@ -40,9 +36,9 @@ class Shape:
|
|||
self.p2 = Point(x2, y2, z2)
|
||||
|
||||
def get_contained_points(self) -> Generator[Point, None, None]:
|
||||
for x in range(self.p1.x, self.p2.x+1):
|
||||
for y in range(self.p1.y, self.p2.y+1):
|
||||
for z in range(self.p1.z, self.p2.z+1):
|
||||
for x in range(self.p1.x, self.p2.x + 1):
|
||||
for y in range(self.p1.y, self.p2.y + 1):
|
||||
for z in range(self.p1.z, self.p2.z + 1):
|
||||
yield Point(x, y, z)
|
||||
|
||||
def verticies(self) -> List[Point]:
|
||||
|
@ -62,11 +58,17 @@ class Shape:
|
|||
def openscad(self) -> str:
|
||||
shift = self.p1.to_list()
|
||||
o = "cube("
|
||||
o += str(Point(self.p2.x+1-self.p1.x, self.p2.y+1-self.p1.y, self.p2.z+1-self.p1.z).to_list())
|
||||
o += str(
|
||||
Point(
|
||||
self.p2.x + 1 - self.p1.x,
|
||||
self.p2.y + 1 - self.p1.y,
|
||||
self.p2.z + 1 - self.p1.z,
|
||||
).to_list()
|
||||
)
|
||||
o += ", center=false);"
|
||||
if shift[0] != 0 and shift[1] != 0 and shift[2] != 0:
|
||||
o = "translate(" + str(shift) + ") {\n" + o + "\n};"
|
||||
return o
|
||||
return o
|
||||
|
||||
|
||||
def parse(instr: str) -> List[Shape]:
|
||||
|
@ -77,16 +79,22 @@ def parse(instr: str) -> List[Shape]:
|
|||
o.append(Shape(sp[0], x, y, z))
|
||||
return o
|
||||
|
||||
|
||||
def is_point_out_of_bounds(p: Point) -> bool:
|
||||
return not (-50 <= p.x <= 50 and -50 <= p.y <= 50 and -50 <= p.z <= 50)
|
||||
|
||||
|
||||
def main(part_one_only=False):
|
||||
shapes = parse(open("input.txt").read())
|
||||
|
||||
current_program = shapes[0].openscad()
|
||||
for shape in shapes[1:]:
|
||||
|
||||
if is_point_out_of_bounds(shape.p1) and is_point_out_of_bounds(shape.p2) and not part_one_only:
|
||||
if (
|
||||
is_point_out_of_bounds(shape.p1)
|
||||
and is_point_out_of_bounds(shape.p2)
|
||||
and part_one_only
|
||||
):
|
||||
continue
|
||||
|
||||
function = ""
|
||||
|
@ -95,10 +103,18 @@ def main(part_one_only=False):
|
|||
else:
|
||||
function = "difference"
|
||||
|
||||
current_program = function + "() {\n" + current_program + "\n" + shape.openscad() + "\n};"
|
||||
current_program = (
|
||||
function + "() {\n" + current_program + "\n" + shape.openscad() + "\n};"
|
||||
)
|
||||
|
||||
current_program = (
|
||||
"// This file was generated by openscad.generate.py as part of Advent Of Code\n// 2021, day 22\n"
|
||||
+ current_program
|
||||
)
|
||||
|
||||
print(current_program)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
x = False
|
||||
if len(sys.argv) > 1:
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// This file was generated by openscad.generate.py as part of Advent Of Code
|
||||
// 2021, day 22
|
||||
union() {
|
||||
difference() {
|
||||
union() {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// This file was generated by openscad.generate.py as part of Advent Of Code
|
||||
// 2021, day 22
|
||||
difference() {
|
||||
difference() {
|
||||
difference() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue