Compare commits
3 Commits
aca39ea46f
...
1ad78c23bb
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ad78c23bb | |||
| ceb55e86dc | |||
| e1bb9c3ac3 |
@@ -1,5 +1,6 @@
|
|||||||
package lib.turtle;
|
package lib.turtle;
|
||||||
|
|
||||||
|
import util.Pos3;
|
||||||
import kernel.turtle.Turtle;
|
import kernel.turtle.Turtle;
|
||||||
using tink.CoreApi;
|
using tink.CoreApi;
|
||||||
|
|
||||||
@@ -11,6 +12,51 @@ class TurtleExecuter {
|
|||||||
this.instructions = instructions;
|
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() {
|
public function execute() {
|
||||||
for (inst in instructions){
|
for (inst in instructions){
|
||||||
executeInst(inst);
|
executeInst(inst);
|
||||||
|
|||||||
@@ -27,4 +27,20 @@ abstract Pos(Vec2<Int>) from Vec2<Int> to Vec2<Int>{
|
|||||||
x: this.x - rhs.x,
|
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
51
src/util/Pos3.hx
Normal 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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user