Last active
December 16, 2020 20:21
-
-
Save rabestro/47002efeb9e725b98470d95c88dc6670 to your computer and use it in GitHub Desktop.
Bad Code Coding Challenge #51 - Dude, where's my turtle?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: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
Guidelines
Your solution should try to:
The solution should work, although if it has the occasional bug, crash, or other oddies, that's fine. As long as it mostly works.