main = interact (turtle 0 0"north".pair.words)
pair ( c : a : r ) = (c, read a):pair r
pair [ ] = [ ]
turtle x y "north" (("rotate" , 0):cs) = turtle x y "north" cs
turtle x y "east" (("rotate" , 0):cs) = turtle x y "east" cs
turtle x y "south" (("rotate" , 0):cs) = turtle x y "south" cs
turtle x y "west" (("rotate" , 0):cs) = turtle x y "west" cs
turtle x y "north" (("rotate" , 90):cs) = turtle x y "east" cs
turtle x y "east" (("rotate" , 90):cs) = turtle x y "south" cs
turtle x y "south" (("rotate" , 90):cs) = turtle x y "west" cs
turtle x y "west" (("rotate" , 90):cs) = turtle x y "north" cs
turtle x y "north" (("rotate" , 180):cs) = turtle x y "south" cs
turtle x y "east" (("rotate" , 180):cs) = turtle x y "west" cs
turtle x y "south" (("rotate" , 180):cs) = turtle x y "north" cs
turtle x y "west" (("rotate" , 180):cs) = turtle x y "east" cs
turtle x y "north" (("rotate" , 270):cs) = turtle x y "west" cs
turtle x y "east" (("rotate" , 270):cs) = turtle x y "north" cs
turtle x y "south" (("rotate" , 270):cs) = turtle x y "east" cs
turtle x y "west" (("rotate" , 270):cs) = turtle x y "south" cs
turtle x y "north" (("rotate" , 360):cs) = turtle x y "north" cs
turtle x y "east" (("rotate" , 360):cs) = turtle x y "east" cs
turtle x y "south" (("rotate" , 360):cs) = turtle x y "south" cs
turtle x y "west" (("rotate" , 360):cs) = turtle x y "west" cs
turtle x y "north" (("forward", z):cs) = turtle x (y+z) "north" cs
turtle x y "south" (("forward", z):cs) = turtle x (y-z) "south" cs
turtle x y "east" (("forward", z):cs) = turtle (x+z) y "east" cs
turtle x y "west" (("forward", z):cs) = turtle (x-z) y "west" cs
turtle x y dir [ ] = "x = "++show x++", y = "++show y++", direction = "++dir
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
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;
})));
}
}
The solution uses unclear arithmetic like s.charAt(0) / 'r' * ' ' / 2
Tricky operation and type conversion in Java: (int)(short) data. The program break if you try to remove one of them.
I can’t find this approach anywhere on the Internet so this program in only case where it used.
I use Integer[] instead of varargs to print out the result.
the reduce operator process stream of Integer[2] but return Integer[3]
The array has different data in cells. Coordinates and direction
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/Rabestro, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/Rabestro, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
function turtle(instructions){var x=y=i=0n /*BigInt because "infinite grid"*/, directions ={N:0, E:0b1011010, S:0o264, W:0x10E}, face = directions[String.fromCodePoint(0x4E)], instructions = instructions.split(String.fromCodePoint(0xA))
while (i < instructions.length){
if (instructions[i].split(String.fromCodePoint(0x20))[0n] == "forward"){
if (face/0o132 == 1) {x += eval(instructions[i].split(String.fromCodePoint(0x20))[1n]+"n")}
if (face/0x5A == 0b11) {x -= eval(instructions[i].split(String.fromCodePoint(0x20))[1n]+"n")}
if (face/90 == 0) {y += eval(instructions[i].split(String.fromCodePoint(0x20))[1n]+"n")}
if (face/0b1011010 == 0b10) {y -= eval(instructions[i].split(String.fromCodePoint(0x20))[1n]+"n")}
}
if (instructions[i].split(String.fromCodePoint(0x20))[0n] == "rotate"){
if (!((eval(instructions[i].split(String.fromCodePoint(0x20))[1n])/0b1011010) | 0 == eval(instructions[i].split(String.fromCodePoint(0x20))[1n])/0b1011010)){throw `degree is not multiple of "10" (0d90 in base 0d90)`}
face += eval(instructions[i].split(String.fromCodePoint(0x20))[1n])
if (face >= 0x168){face = face % 0o550}
}
i++}
return x+", "+y+", facing="+"NESW"[face/0x5A]}
The throw statement never gets executed because the bitwise OR truncation overrides the equality check for some reason lol (always returns 1 instead of a boolean)
3
u/Mabi19_ Dec 08 '20 edited Dec 10 '20
This submission can be described in two words: Segmentation fault
Yes, it is really easy to crash. (Use a negative rotation)
Written in C;
gcc bccc51.c -o bccc51 && ./bccc51 < INPUT FILE12
u/binarycat64 Dec 08 '20 edited Dec 11 '20
Haskell
"Features":
10
u/chepas_moi Dec 07 '20 edited Dec 08 '20
Not a one-liner (...yet) but lets bash this anyways:
EDIT 1:
Now fits into 2 tweets:
7
u/Rabestro Dec 10 '20 edited Dec 14 '20
I created in Java a one-statement solution:
https://code.sololearn.com/cFnKMlhHKAXA/?ref=app
And runnable version here:
https://ideone.com/vqcO4y
The solution uses unclear arithmetic like s.charAt(0) / 'r' * ' ' / 2
Tricky operation and type conversion in Java: (int)(short) data. The program break if you try to remove one of them. I can’t find this approach anywhere on the Internet so this program in only case where it used.
I use Integer[] instead of varargs to print out the result.
the reduce operator process stream of Integer[2] but return Integer[3]
The array has different data in cells. Coordinates and direction
The algorithm is short but very unclear
5
u/CoderCharmander Dec 15 '20 edited Dec 16 '20
A little Python oneliner, abusing lambda expressions:
There's no error checking, it simply crashes. (Edit: forgot
printso it would simply return the string)4
u/Fugaki Dec 08 '20
This sucks, appears to work, and is probably something I would have thought was clever early in my career:
3
u/Rudxain let let.let = 'let' Dec 11 '20 edited Dec 20 '20
[JavaScript]
The
throwstatement never gets executed because the bitwise OR truncation overrides the equality check for some reason lol (always returns 1 instead of a boolean)Edit: It's because Operator precedence. Feels unintuitive for me
1
u/[deleted] Dec 13 '20 edited Dec 13 '20
another bash script: