Algunos de ustedes pueden estar familiarizados con la forma en que cambia una motocicleta. Pero para aquellos que no, se ve así
6 6
5 5
4 4
3
2
norte
1
Ahora quiero saber en qué equipo estoy después de realizar algunos cambios hacia arriba y hacia abajo. El programa debería funcionar desde neutral.
Entrada de muestra:
V^^
Salida de muestra:
2
Como puede ver, bajé una vez de N a 1 y cambié dos veces a segunda velocidad.
Este es un código de golf. La respuesta más corta en bytes gana.
Nota: La entrada puede tener 2 caracteres. Puede ser U y D para subir y bajar o lo que quieras , tiene que ser una cadena . No puedes cambiar más allá de la 1ra o 6ta marcha. Si está en sexto y sube de nuevo, permanecerá en sexto. ¡Buena suerte!
Respuestas:
JavaScript (ES6),
49484746 bytesExpects:
1
for down7
for upFormatted and commented
Gears are mapped as follows:
Which allows us to easily check the validity of the current gear with:
Demo
Show code snippet
fuente
05AB1E,
2220 bytesTry it online!
Explanation
fuente
MATL,
322823 bytes5 bytes saved thanks to @Luis
This solution uses
'2'
for up-shift and'0'
for down-shift.Try it at MATL Online
Explanation
fuente
V,
20, 15 bytesTry it online!
Input is a string of
h
(up) andl
(down) characters.Thanks to @nmjcman for saving 5 bytes and teaching me about a vim feature I never knew about!
If we could assume the input never goes out of bounds, it would simply be 9 bytes:
But unfortunately that isn't allowed.
Explanation:
fuente
n
, not1
.Java 7,
106105103 bytesExplanation:
Test code:
Try it here.
Output:
fuente
Haskell,
595351 BytesUses
0
for down and2
for up. Usage example:Thanks to @xnor for taking off 6 bytes! Also, turns out I don't need a function name or parentheses so that's another 2 bytes.
fuente
read[c]-2
.g=
.g=
because it's clearerJavaScript (ES6),
4858 bytesUsage
Assign it to a function, and then call it. Input is a string containing a
1
for an upshift, and a0
for a downshift.fuente
f("001")
which should return N (gear down to 1, gear down staying at 1, gear up to N)PHP 7.1, 71 bytes
shifts
$g
from -1 to 5, uses negative string offset for first gear.Run with
-nr
, provide shifting string as command line argument.fuente
Jelly,
1714 bytesUses
6
for up and0
for down.Try it online!
How it works
fuente
Ruby, 58 bytes
Expected input is 'v' for a downshift and '^' for upshift
fuente
Processing JS (modified) 121 bytes
Ungolfed
Try it online!
I went with PJs since I know it well. The only problem is the version I use is very strictly typed. I can't leave out brackets and lots of other tricks. It is pretty simple. The input should go into the variable
a
and it takes lowercaseu d
. The program loops until it hits the end of the string and every iteration it checks to see if it is a u or a d. If it is and it won't try and "shift" past where you can it shifts. At the end I print the results!fuente
The input should go into the variable a
Using hard coded input is not a default input method, see here.void f(String[] a){...}
That's hardly 100 bytes.k, 25 bytes
It takes input as a string, and uses
[
for downshift, and]
for upshift, because they're conveniently situated.Examples:
fuente
GNU sed,
8987 + 1(r flag) = 88 bytesBecause sed has no integer types or arithmetic operations, the solution is arrived at by using regular expressions only.
It works by sliding the pointer
x
based on each input shift, left (forU
p) or right (forD
own), along a non-wrapping tape that contains only the cells65432N1
. The answer at the end is the value in the cell left of the pointer.Example run: or Try it online!
Explanation:
fuente
GNU sed,
7673 bytesIncludes +1 for
-r
Output is in unary except neutral, which is still
N
(see this consensus).Try it online!
This basically counts up and down in unary then converts 1 to N and 0 to 1.
fuente
1
as the starting value, asN
, and nothing as1
.s/$/1/;:;/1{6}/!s/^U(.*)/\11/;s/^D(.*)1/\1/;t;s/U|D//;t;s/^1$/N/;s/^$/1/
Rebol,
9693 bytesUngolfed:
Example usage (in Rebol console):
fuente
><>, 35 bytes
An enthusiastic piece of code that encourages you to drive above the speed limit.
Accepts any two inputs whose code modulo 3 are 0 and 2, for example 0 and 2.
For extra fishiness, I recommend the use of < and >.
Explanation :
You can try it here !
fuente
SpecBAS - 102
Moves the index of the string depending on input and prints the relevant character.
fuente
Pyth, 32 bytes
Uses space and newline for down and up.
Explanation
There's almost certainly a better way to do the incrementing and the output.
fuente
CJam,
2422 bytesUses
(
for down and)
for up.Try it online!
Explanation
fuente
Batch, 144 bytes
Takes input on STDIN, using
0
to go to a lower gear and6
to go to a higher gear. These numbers were chosen to make it easy to ignore the current gear. Finally if the gear is1
thenN
is printed otherwise0
is converted to1
and the gear is printed.fuente
Javascript ES6 nonstrict,
136120 chars136 chars for
V^
120 chars for
-+
fuente
Retina, 65 bytes
Uses
u
andd
for up and down.Try it online!
Explanation
This program works by keeping
1N23456
behind the sequence of instructions. It keeps track of the current gear by having a space behind it. Then it takes one instruction at a time until there's no more.Start by putting
1 N23456
before the input. The space beforeN
indicates thatN
is the current gear.These are two replacement stages, grouped together, and run until they stop changing the string:
The first one handles shifting the gear up. It will look for any number of gears after the space, followed by a
6
, then followed byu
(u
indicates the instruction to gear shift up). If there were characters before the 6, it swaps the space with the character immediately after it, deletes theu
, and leaves the rest of the string intact. Since the6
is mandatory in the match, it will only swap the space with any character before the6
. It will never swap with the6
.The second stage handles gear shifting down, and works similarly. It looks optionally for a character before the space, then some other gears after ending in
6
, followed byd
. It swaps the space with the character before it, deletes thed
, and leaves the rest intact. If the space was at the start of the string, there was no match for a character before the space, so no swap occurs.After neither of the above replacements can be done anymore, all gear shifts have been completed. The line is cleared of everything except the gear immediately after the space. This is the final gear.
fuente
Powershell,
1128785 bytes$i=1;switch([char[]]$args[0]){'^'{if(5-gt$i){$i++}}'v'{if(1-le$i){$i--}}}'1N2345'[$i]
ungolfed
saved 25 bytes by reading the powershell codegolf tips
saved 2 bytes by flipping the gt/le operators
fuente
Perl 6, 144 bytes
Works as it should, i believe. Improvements are welcome. First time using Perl for anything, but i loved the thought of the language, so i had to try.
fuente
Clojure, 74 bytes
Folds over the shift string, maintaining an index as the accumulator. Each iteration it either increases or decreases the index, then clamps it to the range 0-6. Finally, a string holding the gears is indexed and returned.
Returns a Clojure character representing the current gear. Gear 1 is returned as
\1
, and gear 'N' is returned as\N
.Pre-golfed explanation. Follow the numbers, since it doesn't read well top-down.
fuente
Python 3,
6763 bytesPretty straightforward solution.
-4 bytes thanks to @ovs!
fuente