Skip to content

Instantly share code, notes, and snippets.

@rabestro
Last active December 16, 2020 20:21
Show Gist options
  • Save rabestro/47002efeb9e725b98470d95c88dc6670 to your computer and use it in GitHub Desktop.
Save rabestro/47002efeb9e725b98470d95c88dc6670 to your computer and use it in GitHub Desktop.
Bad Code Coding Challenge #51 - Dude, where's my turtle?
// Bad Code Coding Challenge #51 - Dude, where's my turtle?
// https://www.reddit.com/r/badcode/comments/k8eh7a/bad_code_coding_challenge_51_dude_wheres_my_turtle/
public class Turtle {
public static void main(String[] args) {
System.out.println(java.text.MessageFormat.format(
"x = {1}, y = {0}, direction = {2, choice, 0#north|1#east|2#south|3#west}",
new java.util.Scanner(System.in).useDelimiter("\\R").tokens()
.mapToInt(s -> Integer.parseInt(s.substring(7).trim()) << s.charAt(0) / 'r' * ' ' / 2)
.mapToObj(data -> new Integer[] {(int)(short) data, (data >> 16) / 90})
.reduce(
new Integer[]{0, 0, 0},
(turtle, action) -> {
turtle[2] += action[1];
turtle[2] %= 4;
turtle[turtle[2] % 2] += turtle[2] < 2 ? action[0] : -action[0];
return turtle;
})));
}
}
import java.util.stream.IntStream;
public class Turtle2 {
public static void main(String[] args) {
IntStream.concat(IntStream.of(0), new Scanner(System.in)
.useDelimiter("\\R").tokens()
.mapToInt(s -> Integer.parseInt(s.substring(7).trim()) * (1 - s.charAt(0) / 'r' * 2)))
.reduce((a, b) -> b < 0
? (a << 3 >>> 3) + (((a >>> 29) - b / 90) % 4 << 29)
: a + (a << 1 < 0 ? -1 : 1) * (b << (a << 2 < 0 ? 0 : 16)))
.ifPresent(x -> System.out.println(java.text.MessageFormat.format(
"x = {0}, y = {1}, direction = {2, choice, 0#north|1#east|2#south|3#west}",
x << 16 >> 16, x << 3 >>> 19, x >> 29)));
}
}
@rabestro
Copy link
Author

rabestro commented Dec 10, 2020

Bad Code Coding Challenge #51 - Dude, where's my turtle?

Alternative title: It's turtles all the way down

Write a solution that accepts a list of instructions for a robotic turtle and outputs its coordinates and direction after following all instructions.

The turtle exists on an infinite grid of coordinates. It always starts at 0,0 with a direction of north. The turtle can face either north, east, south, or west.

The turtle accepts two instructions, each with an argument.

forward 10

forward moves the turtle forwards in its current direction with the number of steps provided. The turtle cannot move backwards given a negative number of steps.

For example, if the turtle is at 0,0 and is facing north, forward 10 will move it to 0,10. The turtle can move in four directions in total, which are:

  • North moves the turtle positively along the Y axis.
  • South moves the turtle negatively along the Y axis.
  • East moves the turtle positively along the X axis.
  • West moves the turtle negatively along the X axis.

rotate 90

rotate rotates the turtle clockwise at the angle provided. The turtle only supports rotating in multiples of 90. The turtle cannot rotate counter-clockwise given a negative rotation.

Examples

forward 10
rotate 90

x = 0, y = 10, direction = east

forward 10
rotate 90
forward 10
rotate 270
forward 10

x = 10, y = 20, direction = north

forward 10
rotate 180
forward 10
rotate 180

x = 0, y = 0, direction = north

Guidelines

Your solution should try to:

  • Avoid best practises
  • Potentially have woeful performance issues
  • It can look like the code you wrote when you were just starting out programming or it could be extremely "clever" code.
  • Be equal parts hilarious and cringe-worthy
  • If the code is too long for a comment (over approx. 125 lines), please upload it to a pastebin instead of submitting it as a comment.

The solution should work, although if it has the occasional bug, crash, or other oddies, that's fine. As long as it mostly works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment