Compare commits

...

3 Commits

Author SHA1 Message Date
1ad78c23bb extented TurtleExcuter 2022-04-26 19:43:34 +02:00
ceb55e86dc added Pos3 2022-04-26 19:42:58 +02:00
e1bb9c3ac3 Pos2 multiply and negate 2022-04-26 19:42:50 +02:00
3 changed files with 113 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
package lib.turtle;
import util.Pos3;
import kernel.turtle.Turtle;
using tink.CoreApi;
@@ -11,6 +12,51 @@ class TurtleExecuter {
this.instructions = instructions;
}
public function getRequiredFuel(): Int {
var fuel = 0;
for(inst in instructions){
if (inst == Forward || inst == Back || inst == Up || inst == Down) {
fuel++;
}
}
return fuel;
}
public function getRequiredBlocks(): Int {
var blocks = 0;
for(inst in instructions){
switch inst {
case Place(_):
blocks++;
default:
}
}
return blocks;
}
public function getFinalOffset(): Pos3 {
var pos: Pos3 = {x:0, y:0, z:0};
var forwardVec: Pos3 = {x: 1, y: 0, z: 0};
for (inst in instructions){
switch inst {
case Forward:
pos = pos + forwardVec;
case Back:
pos = pos - forwardVec;
case TurnRight:
forwardVec = {x: -forwardVec.z, z: forwardVec.x, y: forwardVec.y};
case TurnLeft:
forwardVec = {x: forwardVec.z, z: -forwardVec.x , y: forwardVec.y};
default:
}
}
return pos;
}
public function execute() {
for (inst in instructions){
executeInst(inst);

View File

@@ -27,4 +27,20 @@ abstract Pos(Vec2<Int>) from Vec2<Int> to Vec2<Int>{
x: this.x - rhs.x,
});
}
@:op(A * B)
public function multiply(rhs: Vec2<Int>): Pos {
return new Pos({
y: this.y * rhs.y,
x: this.x * rhs.x,
});
}
@:op(-A)
public function negate(): Pos {
return new Pos({
y: -this.y,
x: -this.x,
});
}
}

51
src/util/Pos3.hx Normal file
View File

@@ -0,0 +1,51 @@
package util;
import util.Vec.Vec3;
/**
Reporesents a Point in a 3D `Int` System.
Basicly a wrapper for Vec3<Int> with some extra functions.
`Y` represents the height of the point.
**/
@:forward(x,y,z)
abstract Pos3(Vec3<Int>) from Vec3<Int> to Vec3<Int>{
inline public function new(i:Vec3<Int>) {
this = i;
}
@:op(A + B)
public function add(rhs: Vec3<Int>):Pos3 {
return new Pos3({
y: this.y + rhs.y,
x: this.x + rhs.x,
z: this.z + rhs.z
});
}
@:op(A - B)
public function sub(rhs: Vec3<Int>):Pos3 {
return new Pos3({
y: this.y - rhs.y,
x: this.x - rhs.x,
z: this.z - rhs.z
});
}
@:op(A * B)
public function multiply(rhs: Vec3<Int>): Pos3 {
return new Pos3({
y: this.y * rhs.y,
x: this.x * rhs.x,
z: this.z * rhs.z
});
}
@:op(-A)
public function negate(): Pos3 {
return new Pos3({
y: -this.y,
x: -this.x,
z: -this.z
});
}
}