Skip to content

Instantly share code, notes, and snippets.

@saml
Last active December 11, 2015 03:38
Show Gist options
  • Save saml/4538748 to your computer and use it in GitHub Desktop.
Save saml/4538748 to your computer and use it in GitHub Desktop.
yolo gridlang
int s;
int e;
int n;
int w;
int direction;
int
go() {
return ffi(@move, direction);
}
int
clear() {
n = -1;
s = -1;
w = -1;
e = -1;
}
int
survey() {
e = ffi(@look, @east);
n = ffi(@look, @north);
w = ffi(@look, @west);
s = ffi(@look, @south);
}
int
act() {
if (s == @cell_robot) {
return ffi(@punch, @south);
}
}
int
immediates() {
e = ffi(@look, @east);
if (e == @cell_robot) {
return ffi(@punch, @east);
}
s = ffi(@look, @south);
if (s == @cell_robot) {
return ffi(@punch, @south);
}
n = ffi(@look, @north);
if (n == @cell_robot) {
return ffi(@punch, @north);
}
w = ffi(@look, @west);
if (w == @cell_robot) {
return ffi(@punch, @west);
}
if (s == @cell_resource) {
return ffi(@pull, @south);
}
if (e == @cell_resource) {
return ffi(@pull, @east);
}
if (n == @cell_resource) {
return ffi(@pull, @north);
}
if (w == @cell_resource) {
return ffi(@pull, @west);
}
return -10;
}
int
get_direction(proposed_direction, init_x, init_y, incr_x, incr_y, goal_x) {
while (init_x != goal_x) {
if (ffi(@scan, init_x, init_y) == @cell_resource) {
return proposed_direction;
}
init_x = init_x + incr_x;
init_y = init_y + incr_y;
}
if (ffi(@scan, init_x, init_y) == @cell_resource) {
return proposed_direction;
}
return -10;
}
int
set_sail() {
// 2
direction = get_direction(@east, 1, -1, 1, 1, 2);
if (direction == @east) {
return direction;
}
direction = get_direction(@south, 1, 1, -1, 1, 0);
if (direction == @south) {
return direction;
}
direction = get_direction(@west, -1, 1, -1, -1, -2);
if (direction == @west) {
return direction;
}
direction = get_direction(@north, -1, -1, 1, -1, 0);
if (direction == @north) {
return direction;
}
// 3
direction = get_direction(@east, 1, -2, 1, 1, 3);
if (direction == @east) {
return direction;
}
direction = get_direction(@south, 2, 1, -1, 1, 0);
if (direction == @south) {
return direction;
}
direction = get_direction(@west, -1, 2, -1, -1, -3);
if (direction == @west) {
return direction;
}
direction = get_direction(@north, -2, -1, 1, -1, 0);
if (direction == @north) {
return direction;
}
// 4
direction = get_direction(@east, 1, -3, 1, 1, 4);
if (direction == @east) {
return direction;
}
direction = get_direction(@south, 3, 1, -1, 1, 0);
if (direction == @south) {
return direction;
}
direction = get_direction(@west, -1, 3, -1, -1, -4);
if (direction == @west) {
return direction;
}
direction = get_direction(@north, -3, -1, 1, -1, 0);
if (direction == @north) {
return direction;
}
// 5
direction = get_direction(@east, 1, -4, 1, 1, 5);
if (direction == @east) {
return direction;
}
direction = get_direction(@south, 4, 1, -1, 1, 0);
if (direction == @south) {
return direction;
}
direction = get_direction(@west, -1, 4, -1, -1, -5);
if (direction == @west) {
return direction;
}
direction = get_direction(@north, -4, -1, 1, -1, 0);
if (direction == @north) {
return direction;
}
}
int
main() {
while (1) {
immediates();
set_sail();
go();
}
}
PUSH 0
DUPN << 5
CALL << @main
END
@go
PUSH @move
PEEK << 4
CALLFF << 2
STORE retval
PUSH retval
RETURN
@clear
PUSH -1
PUSH 2
POKE
PUSH -1
PUSH 0
POKE
PUSH -1
PUSH 3
POKE
PUSH -1
PUSH 1
POKE
PUSH 0
RETURN
@survey
PUSH @look
PUSH @east
CALLFF << 2
PUSH 1
POKE
PUSH @look
PUSH @north
CALLFF << 2
PUSH 2
POKE
PUSH @look
PUSH @west
CALLFF << 2
PUSH 3
POKE
PUSH @look
PUSH @south
CALLFF << 2
PUSH 0
POKE
PUSH 0
RETURN
@act
PEEK << 0
PUSH @cell_robot
EQUAL
IFFGOTO << @L0
PUSH @punch
PUSH @south
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L0
@L1
PUSH 0
RETURN
@immediates
PUSH @look
PUSH @east
CALLFF << 2
PUSH 1
POKE
PEEK << 1
PUSH @cell_robot
EQUAL
IFFGOTO << @L2
PUSH @punch
PUSH @east
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L2
@L3
PUSH @look
PUSH @south
CALLFF << 2
PUSH 0
POKE
PEEK << 0
PUSH @cell_robot
EQUAL
IFFGOTO << @L4
PUSH @punch
PUSH @south
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L4
@L5
PUSH @look
PUSH @north
CALLFF << 2
PUSH 2
POKE
PEEK << 2
PUSH @cell_robot
EQUAL
IFFGOTO << @L6
PUSH @punch
PUSH @north
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L6
@L7
PUSH @look
PUSH @west
CALLFF << 2
PUSH 3
POKE
PEEK << 3
PUSH @cell_robot
EQUAL
IFFGOTO << @L8
PUSH @punch
PUSH @west
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L8
@L9
PEEK << 0
PUSH @cell_resource
EQUAL
IFFGOTO << @L10
PUSH @pull
PUSH @south
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L10
@L11
PEEK << 1
PUSH @cell_resource
EQUAL
IFFGOTO << @L12
PUSH @pull
PUSH @east
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L12
@L13
PEEK << 2
PUSH @cell_resource
EQUAL
IFFGOTO << @L14
PUSH @pull
PUSH @north
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L14
@L15
PEEK << 3
PUSH @cell_resource
EQUAL
IFFGOTO << @L16
PUSH @pull
PUSH @west
CALLFF << 2
STORE retval
PUSH retval
RETURN
@L16
@L17
PUSH -10
RETURN
@get_direction
@L18
PEEK << -5
PEEK << -2
NEQUAL
IFFGOTO << @L21
PUSH @scan
PEEK << -6
PEEK << -6
CALLFF << 3
PUSH @cell_resource
EQUAL
IFFGOTO << @L19
PEEK << -6
STORE retval
POPN << 6
PUSH retval
RETURN
@L19
@L20
PEEK << -5
PEEK << -4
ADD
PUSH -5
POKE
PEEK << -4
PEEK << -3
ADD
PUSH -4
POKE
GOTO << @L18
@L21
PUSH @scan
PEEK << -6
PEEK << -6
CALLFF << 3
PUSH @cell_resource
EQUAL
IFFGOTO << @L22
PEEK << -6
STORE retval
POPN << 6
PUSH retval
RETURN
@L22
@L23
POPN << 6
PUSH -10
RETURN
@set_sail
PUSH @east
PUSH 1
PUSH -1
PUSH 1
PUSH 1
PUSH 2
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @east
EQUAL
IFFGOTO << @L24
PEEK << 4
STORE retval
PUSH retval
RETURN
@L24
@L25
PUSH @south
PUSH 1
PUSH 1
PUSH -1
PUSH 1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @south
EQUAL
IFFGOTO << @L26
PEEK << 4
STORE retval
PUSH retval
RETURN
@L26
@L27
PUSH @west
PUSH -1
PUSH 1
PUSH -1
PUSH -1
PUSH -2
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @west
EQUAL
IFFGOTO << @L28
PEEK << 4
STORE retval
PUSH retval
RETURN
@L28
@L29
PUSH @north
PUSH -1
PUSH -1
PUSH 1
PUSH -1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @north
EQUAL
IFFGOTO << @L30
PEEK << 4
STORE retval
PUSH retval
RETURN
@L30
@L31
PUSH @east
PUSH 1
PUSH -2
PUSH 1
PUSH 1
PUSH 3
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @east
EQUAL
IFFGOTO << @L32
PEEK << 4
STORE retval
PUSH retval
RETURN
@L32
@L33
PUSH @south
PUSH 2
PUSH 1
PUSH -1
PUSH 1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @south
EQUAL
IFFGOTO << @L34
PEEK << 4
STORE retval
PUSH retval
RETURN
@L34
@L35
PUSH @west
PUSH -1
PUSH 2
PUSH -1
PUSH -1
PUSH -3
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @west
EQUAL
IFFGOTO << @L36
PEEK << 4
STORE retval
PUSH retval
RETURN
@L36
@L37
PUSH @north
PUSH -2
PUSH -1
PUSH 1
PUSH -1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @north
EQUAL
IFFGOTO << @L38
PEEK << 4
STORE retval
PUSH retval
RETURN
@L38
@L39
PUSH @east
PUSH 1
PUSH -3
PUSH 1
PUSH 1
PUSH 4
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @east
EQUAL
IFFGOTO << @L40
PEEK << 4
STORE retval
PUSH retval
RETURN
@L40
@L41
PUSH @south
PUSH 3
PUSH 1
PUSH -1
PUSH 1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @south
EQUAL
IFFGOTO << @L42
PEEK << 4
STORE retval
PUSH retval
RETURN
@L42
@L43
PUSH @west
PUSH -1
PUSH 3
PUSH -1
PUSH -1
PUSH -4
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @west
EQUAL
IFFGOTO << @L44
PEEK << 4
STORE retval
PUSH retval
RETURN
@L44
@L45
PUSH @north
PUSH -3
PUSH -1
PUSH 1
PUSH -1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @north
EQUAL
IFFGOTO << @L46
PEEK << 4
STORE retval
PUSH retval
RETURN
@L46
@L47
PUSH @east
PUSH 1
PUSH -4
PUSH 1
PUSH 1
PUSH 5
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @east
EQUAL
IFFGOTO << @L48
PEEK << 4
STORE retval
PUSH retval
RETURN
@L48
@L49
PUSH @south
PUSH 4
PUSH 1
PUSH -1
PUSH 1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @south
EQUAL
IFFGOTO << @L50
PEEK << 4
STORE retval
PUSH retval
RETURN
@L50
@L51
PUSH @west
PUSH -1
PUSH 4
PUSH -1
PUSH -1
PUSH -5
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @west
EQUAL
IFFGOTO << @L52
PEEK << 4
STORE retval
PUSH retval
RETURN
@L52
@L53
PUSH @north
PUSH -4
PUSH -1
PUSH 1
PUSH -1
PUSH 0
CALL << @get_direction
PUSH 4
POKE
PEEK << 4
PUSH @north
EQUAL
IFFGOTO << @L54
PEEK << 4
STORE retval
PUSH retval
RETURN
@L54
@L55
PUSH 0
RETURN
@main
@L56
PUSH 1
IFFGOTO << @L57
CALL << @immediates
POP
CALL << @set_sail
POP
CALL << @go
POP
GOTO << @L56
@L57
PUSH 0
RETURN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment