อุปกรณ์ตรวจสอบรถจักรยานยนต์!


36

บางท่านอาจคุ้นเคยกับวิธีการที่รถจักรยานยนต์เปลี่ยน แต่สำหรับผู้ที่ไม่ได้ดูเหมือนว่านี้

6

5

4

3

2

ยังไม่มีข้อความ

1

ตอนนี้ฉันอยากรู้ว่าฉันกำลังอยู่ในเกียร์หลังจากทำการเลื่อนขึ้นและลงเล็กน้อย โปรแกรมควรทำงานจากเป็นกลาง

ตัวอย่างอินพุต:

V^^

ตัวอย่างผลลัพธ์:

2

อย่างที่คุณเห็นฉันเปลี่ยนเกียร์หนึ่งครั้งจาก N เป็น 1 และเปลี่ยนสองครั้งเป็นเกียร์ 2

นี่คือรหัสกอล์ฟ คำตอบที่สั้นที่สุดในการชนะไบต์

หมายเหตุ: อินพุตสามารถมีอักขระได้ 2 ตัว สามารถ U และ D สำหรับการขึ้นและลงหรือสิ่งที่คุณต้องการก็จะต้องมีสตริง คุณไม่สามารถเลื่อนเกินเกียร์ 1 หรือ 6 หากคุณอยู่ในอันดับที่ 6 และขึ้นอีกครั้งก็จะอยู่ในอันดับที่ 6 โชคดี!


5
ครั้งต่อไปโปรดโพสต์ความท้าทายของคุณไปที่กล่องทรายเพื่อรับข้อเสนอแนะก่อนโพสต์บนหน้าหลัก
fəˈnɛtɪk

1
@seshoumara ข้อกำหนดเพียงสองข้อเท่านั้นคือต้องเป็นสตริงและคุณสามารถป้อนได้เพียง 2 อักขระเท่านั้น ดังนั้นสามารถใช้การขึ้นบรรทัดใหม่เป็นตัวละคร แต่ถ้าคุณจะใช้มันด้วยเหตุผลอื่นฉันก็ไม่รังเกียจ มันน่าสนใจที่จะเห็นสิ่งที่คุณมีอยู่ในใจ แต่ให้อธิบายสั้น ๆ ว่าทำไมคุณถึงทำแบบนั้นถ้าคุณทำ GL!
Martijn Vissers

4
มันเป็นความอัปยศที่ไม่ได้หมายถึงการเปลี่ยนครึ่งขั้นตอนระหว่าง 1 ถึง N มันเป็นระเบียบที่จะไม่เพียงแค่ไป 2 N 1 N 2 3 แต่ยังไป 2 1 N 2 3
Cort Ammon

2
ฉันเห็นด้วยกับ @CortAmmon - มีการเปลี่ยนแปลงเพียงครั้งเดียวระหว่าง 1 ถึง 2 มันเป็นการเปลี่ยนครึ่งครั้งโดยที่เป็นกลาง
Džuris

2
ไม่ใช่รถมอเตอร์ไซค์ทุกคันที่เปลี่ยนแบบนี้ รถจักรยานยนต์แบบไม่มีคลัตช์ส่วนใหญ่จะเปลี่ยนเป็น N-1-2-3-4 (หรือ N-1-2-3 สำหรับรถยนต์โบราณบางรุ่น) พวกเขาไม่มีเกียร์ 5 หรือ 6 และใช้เกียร์กลมนั่นคือเมื่อ 4 เพิ่มเกียร์จะทำให้ห่อรอบเพื่อ N.
phuclv

คำตอบ:


15

JavaScript (ES6), 49 48 47 46 ไบต์

คาดว่า:

  • 1 สำหรับลง
  • 7 สำหรับขึ้น
f=([c,...s],g=2)=>c?f(s,g-c&7||g):'1N'[--g]||g

จัดรูปแบบและแสดงความคิดเห็น

f = (                   // f is a recursive function which takes:
  [c,                   // - c = next character in input string
      ...s],            // - s = remaining characters
  g = 2                 // - g = current gear, with default = neutral = 2
) =>                    //
  c ?                   // if there's still a character to process:
    f(                  //   do a recursive call with:
      s,                //     - the remaining characters
      g - c & 7 ||      //     - the updated gear if it is valid
      g                 //       or the previous gear if not
    )                   //
  :                     // else:
    '1N'[--g] ||        //   decrement g and output '1' for g = 0, 'N' for g = 1,
    g                   //   or simply the corresponding digit for other values

Gears ถูกแมปดังนี้:

g MOD 8 = 0 1 2 3 4 5 6 7
          ---------------
gear    = X 1 N 2 3 4 5 6       (where 'X' is an invalid state)

ซึ่งทำให้เราสามารถตรวจสอบความถูกต้องของเกียร์ปัจจุบันได้อย่างง่ายดายด้วย:

(g & 7) != 0

การสาธิต


7

05AB1E , 22 20 ไบต์

Îvy<+®‚Z5‚W}6LÀ'N¸ìè

ลองออนไลน์!

คำอธิบาย

Î                      # push 0 (accumulator) and input
 v         }           # for each in input
  y<+                  # decrement current element and add to accumulator
     ®‚Z               # take maximum of current value and -1
        5‚W            # take minimum of current value and 5
            6L         # push the range [1 ... 6]
              À        # rotate left
               'N¸ì    # prepend the letter "N" producing the list [N, 2, 3, 4, 5, 6, 1]
                   è   # index into this list with the value produced by the loop

6

MATL, 32 28 23 ไบต์

บันทึก 5 ไบต์ด้วย @Luis

'234561N'j!Uq[aA]&Ys0))

วิธีนี้ใช้'2'สำหรับ up-shift และ'0'down-shift

ลองใช้ที่MATL Online

คำอธิบาย

'234561N'   % Push the string literal to the stack
j           % Grab the input as a string
!U          % Convert the input to a numeric array based on the characters.
q           % Subtract 1 to turn '2' into 1 and '0' into -1
[aA]        % Push the array [-1 5] to the stack
&Ys         % Compute the cumulative sum using the array [-1, 5] as
            % the upper and lower limits at each point
0)          % Get the last value from the cumulative sum
)           % Use this to index into the initial string literal.
            % Implicitly display the result

@Lifeless อัปเดตพร้อมคำอธิบาย
Suever

ดีมาก! ฉันขอถามได้ไหมว่าทำไมสตริงถึง 234561N แทนที่จะเป็น 1n23456 หรือ 65432n1 ฉันยังพบข้อบกพร่อง! หากคุณยกระดับขึ้นเรื่อย ๆ ควรอยู่ในเกียร์ที่ 6 แต่จะคืน N
Martijn Vissers

1
ดี! ไม่ทราบเกี่ยวกับเคล็ดลับการ จำกัด ใน cumsum
B. Mehta

1
@ B.Mehta ฉันไม่! หลุยส์แนะนำ
Suever

1
@ B.Mehta และอย่าลังเลที่จะเข้าร่วมกับเราในห้องสนทนา MATL !
Suever

6

V , 20 , 15 ไบต์

:sil!î¬61énÀxVp

ลองออนไลน์!

อินพุตเป็นสตริงของอักขระh(ขึ้น) และl(ลง)

ขอบคุณ @nmjcman สำหรับการบันทึก 5 ไบต์และสอนฉันเกี่ยวกับคุณสมบัติกลุ่มที่ฉันไม่เคยรู้มาก่อน!

หากเราสามารถสมมติว่าอินพุตไม่เคยเกินขอบเขตมันก็จะเป็น 9 ไบต์:

¬61énÀxVp

แต่น่าเสียดายที่ไม่ได้รับอนุญาต

คำอธิบาย:

:sil!           " If the cursor goes out of bounds, ignore it and continue
     î          " Run the following keystrokes as V code, rather than vimscript:
      ¬61       "   Insert the string "654321". This will leave the cursor on the '1'
         én     "   Insert an 'n'
           À    "   Run the first arg as V code. This will move left and right a bunch of times
            x   "   Delete whatever character we ended up on
             V  "   Select this whole line,
              p "   And replace it with the character we just deleted

2
นอกจากนี้ลองออนไลน์! การบีบอัด V มันเท่ห์มาก
nmjcman101

อันที่จริงฉันคิดว่าสิ่งนี้ไม่ได้ผลเพราะไปเกินหรือต่ำกว่า 1/6 แบ่งแมโคร: /
nmjcman101

เหตุใดจึงไม่อนุญาตให้ใช้คำตอบ 9 ไบต์
Albert Renshaw

@AlbertRenshaw มันล้มเหลวถ้าคุณเคยลองเปลี่ยนเกียร์ 1 หรือ 6 ตัวอย่างเช่นลองออนไลน์! เอาท์พุทควรไม่n 1
DJMcMayhem

โอ้ฉันเข้าใจแล้วฉันคิดว่าการป้อนข้อมูลให้ถูกต้องคุณหมายถึงตัวละครที่ไม่ขยับ ยังเป็นคำตอบที่ยอดเยี่ยมแม้ว่าจะไม่ถูกต้องก็ตาม
Albert Renshaw

6

Java 7, 106 105 103 ไบต์

char c(String s){int r=1;for(int c:s.toCharArray())r+=c<99?1:-1;return"1N23456".charAt(r<0?0:r>6?6:r);}

คำอธิบาย:

char c(String s){                // Method with String input and character return-type
  int r = 1;                     // Result-index (0-indexed and starting at 1)
  for(int c : s.toCharArray()){  // Loop over all characters in the input String
    r += c < 99 ?                //  If the current character is '^':
           1                     //   Raise the result-index by 1
         :                       //  If it is 'v' instead:
           -1;                   //   Decrease the result-index by 1
  }
  return "1N23456".charAt(       // Return the character based on the return-index:
           r < 0 ?               //  If the result-index is below 0
                  0              //   Use 0 instead
                 : r > 6 ?       //  Else-if the result-index is above 6
                          6      //   Use 6 instead
                         :       //  Else
                          r);    //   Use the result-index
}

รหัสทดสอบ:

ลองที่นี่

class M{
  static char c(String s){int r=1;for(int c:s.toCharArray())r+=c<99?1:-1;return"1N23456".charAt(r<0?0:r>6?6:r);}

  public static void main(String[] a){
    System.out.println(c("v^^"));
    System.out.println(c("^^^^^^^^"));
    System.out.println(c("vvvv^^^vvvv"));
    System.out.println(c("^v^v^v^v"));
  }
}

เอาท์พุท:

2
6
1
N

5

Haskell, 59 53 51 Bytes

("1N23456"!!).foldl(\l c->max 0$min 6$l+read[c]-1)1

ใช้0สำหรับลงและ2ขึ้น ตัวอย่างการใช้งาน:

(("1N23456"!!).foldl(\l c->max 0$min 6$l+read[c]-1)1) "0002"

ขอบคุณ @xnor สำหรับการปิด 6 ไบต์! นอกจากนี้ปรากฎว่าฉันไม่ต้องการชื่อฟังก์ชั่นหรือวงเล็บดังนั้นนั่นคืออีก 2 ไบต์


ถ้าคุณใช้ตัวอักษรที่ป้อนเข้าเป็น 0 และ 2 read[c]-2ที่คุณสามารถทำได้
xnor

ยินดีต้อนรับสู่ PPCG! g=ฟังก์ชั่นที่ไม่ระบุชื่อจะมีการปรับเกินไปดังนั้นคุณจึงไม่จำเป็นต้องใช้
Laikoni

@Laikoni ฉันต้องห่อมันด้วยวงเล็บใช่ไหม? ที่จะไม่เปลี่ยนจำนวนไบต์ดังนั้นฉันคิดว่าฉันจะออกg=เพราะมันชัดเจน
user1472751


4

JavaScript (ES6), 48 58 ไบต์

s=>"1N23456"[[1,...s].reduce((a,b)=>a?a<6?+b?a+1:a-1:6:0)]

การใช้

กำหนดให้กับฟังก์ชั่นแล้วเรียกมันว่า อินพุตเป็นสตริงที่มีการ1สำหรับการเปลี่ยนและ0สำหรับการเปลี่ยน

f=s=>"1N23456"[[1,...s].reduce((a,b)=>a?a<6?+b?++a:a--:6:0)]
f("011")
-> "2"
f("0")
-> "1"
f("01")
-> "N"

f (0) ส่งคืน 1 ไม่ใช่ N ... และโค้ดของคุณจะส่งคืน undef หากคุณเลื่อนจาก 6 หรือลดลงจาก 1
fəˈnɛtɪk

จับดี. แก้ไขที่ราคาสองไบต์
ลุค

ไม่ทำงานอินพุตเช่นf("001")ที่ควรส่งคืนN (ลดลงถึง 1, ลดเกียร์ลงที่ 1, เพิ่มสูงสุดเป็น N)
Emigna

ไม่ควรวนรอบ มันควรจะอยู่ที่ 6 ถ้า upshifted จาก 6 และอยู่ที่ 1 ถ้า downshifted จาก 1 นอกจากนี้มันยังให้ undef ถ้าคุณ downshift จาก 1
fəˈnɛtɪk

4

PHP 7.1, 71 ไบต์

for(;$c=$argv[1][$i++];))$g=max(-1,min($g+=$c<=>X,5));echo N234561[$g];

เปลี่ยน$gจาก -1 เป็น 5 ใช้การชดเชยสตริงเชิงลบสำหรับเกียร์แรก
เรียกใช้ด้วย-nrระบุสตริงการขยับเป็นอาร์กิวเมนต์บรรทัดคำสั่ง


4

เยลลี่ , 17 14 ไบต์

1;r2ị$¥/CỊ¡o”N

ใช้6สำหรับขึ้นและ0ลง

ลองออนไลน์!

มันทำงานอย่างไร

1;r2ị$¥/CỊ¡o”N  Main link. Argument: s (string of 6's and 0's)

1;              Prepend a 1 (integer) to the string/character array s.
       /        Reduce the result by the following dyadic link.
                Let's call the arguments x and y.
      ¥           Create a dyadic chain of 2 links:
  r                 Construct the range [x, ..., y] (increasing or decreasing).
                    y will be a character, but 'r' casts both argument to int.
                    This is intended for use with floats, but it works just as well
                    when the argument is a digit.
     $              Create a monadic chain of two links:
   2ị                 Take the second element of the constructed range.
                  When y = 6 > x, this gives x + 1.
                  When y = 0 < x, this gives x - 1.
                  When y = x, the range is a singleton array, so this gives x.
          ¡     Conditional application:
         Ị        If the previous result is insignificant (0 or 1):
        C           Subtract it from 1.
                This swaps 0 and 1 without affecting the other potential outcomes.
           o”N  Logical OR with 'N', replacing 0 with that character.

2

ทับทิม 58 ไบต์

->s{a=1;s.chars{|b|a-=[a<=>0,a<=>6][b<=>?v]};"1N23456"[a]}

อินพุตที่คาดไว้คือ 'v' สำหรับ downshift และ '^' สำหรับ upshift


2

กำลังประมวลผล JS (แก้ไข) 121 ไบต์

var g="1N23456",a="",c=0; for(var i=0;i<a.length;i++){if(a[i]==="d"&&c>0){c--;}else if(c<6&&a[i]==="u"){c++;}}println(g[c]);

Ungolfed

var g=[1,"N",2,3,4,5,6],a="",c=0;
for(var i=0;i<a.length;i++)
{if(a[i]==="d"&&c>0)
{
    c--;

}else if(c<6&&a[i]==="u")
{
    c++;

}

}
println(g[c]);

ลองออนไลน์!

ฉันไปกับ PJs เพราะฉันรู้ดี ปัญหาเดียวคือรุ่นที่ฉันใช้พิมพ์อย่างเคร่งครัดมาก ฉันไม่สามารถละทิ้งวงเล็บเหลี่ยมและลูกเล่นอื่น ๆ ได้อีกมากมาย มันค่อนข้างง่าย การป้อนข้อมูลที่ควรจะไปลงในตัวแปรและจะใช้เวลาเป็นตัวพิมพ์เล็กa u dโปรแกรมวนซ้ำจนกว่าจะถึงจุดสิ้นสุดของสตริงและตรวจสอบซ้ำทุกครั้งเพื่อดูว่ามันเป็น au หรือ d หากเป็นและจะไม่พยายามและ "เลื่อน" อดีตซึ่งคุณสามารถเลื่อนได้ ในตอนท้ายฉันพิมพ์ผลลัพธ์!


หากรุ่นของคุณอนุญาตให้ผู้ประกอบการที่ประกอบไปด้วยไตรภาคคุณอาจจะสามารถเขียน ifs ของคุณในแบบที่สั้นกว่านี้
Bojidar Marinov

The input should go into the variable aโดยใช้การป้อนรหัสยากไม่ได้เป็นวิธีการป้อนข้อมูลเริ่มต้นดูที่นี่
Laikoni

@Laikoni จริงเหรอ? นั่นมันโง่ ฉันไม่มีวิธีที่ดีกว่านี้อีกแล้ว ถ้าฉันต้องทำซ้ำมันจะนานกว่า 100 ไบต์
Christopher

คุณไม่เพียงห่อโค้ดของคุณในฟังก์ชั่นหรือไม่? เช่น. void f(String[] a){...}มีขนาดไม่ถึง 100 ไบต์
Laikoni

@Laikoni ใน Khan Academy ProcessingJS เป็นJS บริสุทธิ์และด้วยเหตุนี้จึงไม่มี String หรือโมฆะ แต่คุณพูดถูกฟังก์ชั่นจะสั้นลง
Kritixi Lithos

2

k, 25 ไบต์

"1N23456"@{6&0|x+y-92}/1,

มันใช้อินพุตเป็นสตริงและใช้[สำหรับ downshift และ]upshift เพราะมันอยู่ในตำแหน่งที่สะดวก

"1N23456"@                / the numbers 0 to 6 are used for the gears,
                          / this turns them into the correct number/letter
                       1, / prepend 1 because this is our initial gear
          {          }/   / fold the function through the list
                          / x is before, y is the next character
                 y-92     / subtract 92, which is between "[" and "]"
                          / "[" becomes -1 and "]" becomes 1
               x+         / add it to what we had before
           6&0|           / use max and min to set boundaries 6 and 0

ตัวอย่าง:

 shift:"1N23456"@{6&0|x+y-92}/1,
 shift"[]]"
"2"
 shift"]]]]]]]]"
"6"
 shift"[[[[]]][[[["
"1"
 shift"[][][][]"
"N"
 shift"[[[[[[[[]"
"N"
 shift"]]]]]]]]["
"5"

2

GNU sed , 89 87 + 1 (แฟล็ก r) = 88 ไบต์

เนื่องจาก sed ไม่มีประเภทจำนวนเต็มหรือการดำเนินการทางคณิตศาสตร์วิธีแก้ปัญหาจึงมาถึงโดยใช้นิพจน์ทั่วไปเท่านั้น

s:$:65432Nx1:
:
/6x/!s:^U(.*)(.)x:\1x\2:
s:^D(.*)x(.):\1\2x:
t
s:U|D::
t
s:.*(.)x.*:\1:

มันทำงานได้โดยการเลื่อนตัวชี้xตามแต่ละกะป้อนข้อมูลซ้าย (สำหรับUp) หรือขวา (สำหรับDตัวเอง) พร้อมเทปที่ไม่ห่อที่ประกอบด้วยเซลล์65432N1เท่านั้น คำตอบที่ท้ายคือค่าในเซลล์ด้านซ้ายของตัวชี้

ตัวอย่างการรัน:หรือลองออนไลน์!

sed -rf gear.sed <<< "UUUUUUD"
5

คำอธิบาย:

s:$:65432Nx1:              # assign initial tape and pointer
:                          # start loop
/6x/!s:^U(.*)(.)x:\1x\2:   # if shift 'U', slide `x` to left, but not past the edge
s:^D(.*)x(.):\1\2x:        # if shift 'D', slide `x` to right, -||-
t                          # repeat
s:U|D::                    # if a shift couldn't be applied, delete it "manually",
t                          # and jump to the start of the loop again
s:.*(.)x.*:\1:             # print value left of pointer `x` (answer)

นี่คือ 76 ไบต์แต่ผลลัพธ์ออกมาเป็นนารี
Riley

@Riley Unary แน่นอน! วิธีแก้ปัญหาของคุณแตกต่างดังนั้นทำไมไม่โพสต์มัน
seshoumara

คุณให้แรงบันดาลใจกับฉัน ฉันคิดว่าฉันจะให้คุณใช้ถ้าคุณต้องการ
Riley

@Riley แล้วฉันจะแยกส่วนกับรุ่นของคุณและให้เครดิตคุณ
seshoumara

ฉันเพิ่งจะโพสต์ของตัวเอง :)
ไรลีย์

2

GNU sed , 76 73 ไบต์

รวม +1 สำหรับ -r

s/$/1/
:
/1{6}/!s/^U(.*)/\11/
s/^D(.*)1/\1/
t
s/U|D//
t
s/^1$/N/
s/^$/1/

เอาต์พุตอยู่ในสภาวะเดียวกันยกเว้นเป็นกลางซึ่งยังคงอยู่N(ดูฉันทามตินี้ )

ลองออนไลน์!

สิ่งนี้โดยทั่วไปจะนับขึ้นและลงในเอกภาพจากนั้นแปลง 1 ถึง N และ 0 ถึง 1

s/$/1/               # add 1 to the end (the starting value)
:                    # loop start
/1{6}/!s/^U(.*)/\11/ # If the string starts with 'U' and doesn't have 6 ones, increment
s/^D(.*)1/\1/        # If the string starts with 'D' decrement (but not past 0)
t                    # if something changed loop back
s/U|D//              # if the U or D couldn't be applied, remove it.
t                    # if something changed loop back
s/^1$/N/             # replace 1 with N
s/^$/1/              # if "0", replace with 1

รุ่น sed ของคุณจะสั้นลง 4 ไบต์ผมคิดว่าถ้าคุณทำงานกับ1เป็นค่าเริ่มต้นเป็นและไม่มีอะไรเป็น N 1s/$/1/;:;/1{6}/!s/^U(.*)/\11/;s/^D(.*)1/\1/;t;s/U|D//;t;s/^1$/N/;s/^$/1/
seshoumara

2

Rebol, 96 93 ไบต์

f: func[s][g: next"1N23456"parse s[any["D"(g: back g)|"U"(unless tail? x: next g[g: x])]]g/1]

Ungolfed:

f: func [s] [
    g: next "1N23456"
    parse s [
        any [
              "D" (g: back g)
            | "U" (unless tail? x: next g [g: x])
        ]
    ]
    g/1
]

ตัวอย่างการใช้งาน (ในคอนโซล Rebol):

>> print f "DUU"         
2

>> print f "DDDUU"
2

>> print f "UUUUUUUUU"  
6

>> print f "UUUUUUUUUD"
5

2

> <> , 35 ไบต์

รหัสที่กระตือรือร้นที่กระตุ้นให้คุณขับรถเกินขีด จำกัด ความเร็ว

ยอมรับสองปัจจัยการผลิตที่มีรหัสแบบโมดูโล 3 0 และ 2 ตัวอย่างและ0 สำหรับ fishiness พิเศษผมขอแนะนำให้ใช้และ2
<>

1i:0(?;3%1-+:0(?0:6)?6:1go!
1N23456

คำอธิบาย:

1i:0(?;3%1-+:0(?0:6)?6:1go!
1                             # initial position
 i                            # read the next char
  :0(?;                       # copies it, test copy against 0, if lower stops (EOF detection)
       3%1-                   # map the char to -1 or 1
           +                  # add it to the position
            :0(?0             # if the new position is lower than 0, set to 0
                 :6)?6        # if the new position is greater than 6, set to 6
                      :1go    # output a character from line 1 at the position
                          !   # loops and skip the position initialisation

คุณสามารถลองได้ที่นี่ !


1

SpecBAS - 102

1 g=2: INPUT s$
2 FOR i=1 TO LEN s$
3 g+=(s$(i)="^" AND g<7)-(s$(i)="v" AND g>1)
4 NEXT i
5  ?"1N23456"(g)

ย้ายดัชนีของสตริงขึ้นอยู่กับอินพุตและพิมพ์อักขระที่เกี่ยวข้อง


1

Pyth, 32 ไบต์

J1VQ=JhtS[06+J-qNbqNd;?qJ1\N?JJ1

ใช้พื้นที่และขึ้นบรรทัดใหม่สำหรับการขึ้นลง

คำอธิบาย

J1VQ=JhtS[06+J-qNbqNd;?qJ1\N?JJ1
J1                                 Initialize J to 1
  VQ                 ;             For each character in the input
            +J-qNbqNd              Increment or decrement J
      htS[06                       Get the middle sorted value of [0,6,J]
    =J                             Assign it to J
                      ?qJ1\N?JJ1   Change 1 to 'N' and 0 to 1

มีวิธีที่ดีกว่าในการเพิ่มและเอาท์พุท


1

CJam , 24 22 ไบต์

"1N23456"1q{~0e>6e<}/=

ใช้(สำหรับลงและ)ขึ้น

ลองออนไลน์!

คำอธิบาย

"1N23456"               e# Push the string containing all gears
         1              e# Push 1, the initial index
          q             e# Push the input
           {            e# For each character in the input
            ~           e#   Eval that character. ( is decrement and ) is increment.
             0e>        e#   Take the maximum of (0, index)
                6e<     e#   Take the minimum of (6, index)
                   }/   e# (end of block)
                     =  e# Take that index of the string

1

แบตช์ 144 ไบต์

@set/ps=
@set g=1
:l
@if %g% neq %s:~,1% set/ag+=%s:~,1%/3-1
@set s=%s:~1%
@if not "%s%"=="" goto l
@if %g%==1 (echo N)else cmd/cset/ag+!g

รับอินพุตบน STDIN โดยใช้0เพื่อไปที่เกียร์ต่ำและ6เพื่อไปที่เกียร์สูงขึ้น ตัวเลขเหล่านี้ถูกเลือกเพื่อให้ง่ายต่อการละเว้นเกียร์ปัจจุบัน สุดท้ายหากเกียร์เป็น1แล้วNจะมีการพิมพ์มิฉะนั้น0จะถูกแปลง1และเกียร์จะถูกพิมพ์


0

Javascript ES6 ไม่ จำกัด , 136 120 ตัวอักษร

136 ตัวอักษรสำหรับ V^

with({get x(){return Math.max(0,Math.min(y,6))},set x(v){y=v}})f=s=>eval(s.replace(/(V)|./g,(m,v)=>`x${"-+"[+!v]}=1,`,y=1)+'"1N"[x]||x')

with({get x(){return Math.max(0,Math.min(y,6))},set x(v){y=v}})
f=s=>eval(s.replace(/(V)|./g,(m,v)=>`x${"-+"[+!v]}=1,`,y=1)+'"1N"[x]||x')
console.log(f("V^^"))

120 ตัวอักษรสำหรับ -+

with({get x(){return Math.max(0,Math.min(y,6))},set x(v){y=v}})f=s=>eval(s.replace(/./g,m=>`x${m}=1,`,y=1)+'"1N"[x]||x')

with({get x(){return Math.max(0,Math.min(y,6))},set x(v){y=v}})
f=s=>eval(s.replace(/./g,m=>`x${m}=1,`,y=1)+'"1N"[x]||x')
console.log(f("-++"))


0

เรติน่า 65 ไบต์

^
1 N23456
+(` (.)?(\w*6)u
$1 $2
)`(.)? (\w*6)d
 $1$2
.* (.).*
$1

ใช้uและdสำหรับขึ้นและลง

ลองออนไลน์!

คำอธิบาย

โปรแกรมนี้ทำงานโดยการ1N23456เรียงลำดับคำสั่งไว้เบื้องหลัง มันติดตามเกียร์ปัจจุบันโดยมีช่องว่างด้านหลัง จากนั้นจะใช้เวลาสอนหนึ่งครั้งจนกว่าจะไม่มีอีกต่อไป

^
1 N23456

เริ่มต้นด้วยการใส่1 N23456ก่อนที่จะป้อน ช่องว่างก่อนหน้าNบ่งชี้ว่าNเป็นเกียร์ปัจจุบัน


+(` (.)?(\w*6)u
$1 $2
)`(.)? (\w*6)d
 $1$2

เหล่านี้เป็นสองขั้นตอนการแทนที่จัดกลุ่มเข้าด้วยกันและทำงานจนกว่าพวกเขาจะหยุดเปลี่ยนสตริง:

 (.)?(\w*6)u
$1 $2

คนแรกจัดการการเปลี่ยนเกียร์ขึ้น มันจะมองหาจำนวนของเกียร์หลังจากช่องว่างตามด้วย a 6แล้วตามด้วยu( uระบุคำแนะนำในการเลื่อนเกียร์ขึ้น) หากมีตัวอักษรอยู่ก่อนหน้า 6 จะเป็นการสลับช่องว่างด้วยอักขระในทันทีหลังจากนั้นลบu, และปล่อยให้ส่วนที่เหลือของสตริงไม่เปลี่ยนแปลง เนื่องจาก6มีผลบังคับใช้ในการแข่งขันก็จะสลับพื้นที่ที่มีตัวอักษรใด ๆ 6ก่อนที่จะ 6มันจะไม่สลับกับ

(.)? (\w*6)d
 $1$2

ขั้นตอนที่สองจัดการกับการเปลี่ยนเกียร์และทำงานในทำนองเดียวกัน มันดูเป็นตัวเลือกสำหรับตัวก่อนพื้นที่แล้วบางส่วนเกียร์อื่น ๆ หลังจากที่ลงท้ายด้วยตามมาด้วย6 dมันสลับช่องว่างกับตัวละครก่อนหน้านั้นลบdและออกจากส่วนที่เหลือเหมือนเดิม หากช่องว่างอยู่ที่จุดเริ่มต้นของสตริงจะไม่มีการจับคู่อักขระก่อนช่องว่างดังนั้นจึงไม่มีการสลับเกิดขึ้น


.* (.).*
$1

หลังจากการเปลี่ยนทั้งสองข้างต้นไม่สามารถทำได้อีกต่อไปการเปลี่ยนเกียร์ทั้งหมดเสร็จสมบูรณ์แล้ว เส้นจะถูกล้างทุกอย่างยกเว้นเกียร์ทันทีหลังจากที่ว่าง นี่คือเกียร์สุดท้าย


0

Powershell, 112 87 85 ไบต์

$i=1;switch([char[]]$args[0]){'^'{if(5-gt$i){$i++}}'v'{if(1-le$i){$i--}}}'1N2345'[$i]

ungolfed

$i=1;                                # index which gear we are in
switch([char[]]$args[0]){            # loop over all elements using a switch
  '^'{if(5-gt$i){$i++}}             # if there is a ^ and we are not in sixth yet, shift up
  'v'{if(1-le$i){$i--}}             # if there is a v and we are not in first, shift down
}
'1N2345'[$i]                         # print the output

บันทึก 25 ไบต์โดยการอ่านเคล็ดลับ codegolf powershell

บันทึก 2 ไบต์โดยพลิกตัวดำเนินการ gt / le


0

Perl 6, 144 ไบต์

my enum C <1 N 2 3 4 5 6>;my $n=prompt("");my $p=1;for split("",$n) ->$l {$l eq "u" && $p < 6 ?? ++$p !! 0;$l eq"d"&&$p>0 ?? --$p!!0};say C($p);

ทำงานได้ตามที่ควรฉันเชื่อ ยินดีต้อนรับการปรับปรุง ครั้งแรกที่ใช้ Perl เพื่ออะไร แต่ฉันชอบความคิดของภาษาดังนั้นฉันต้องลอง


0

Clojure, 74 ไบต์

#((vec "1N23456")(reduce(fn[c s](max 0(min 6((if(= s \^)inc dec)c))))1 %))

พับทับสายอักขระการบำรุงรักษาดัชนีเป็นตัวสะสม การวนซ้ำแต่ละครั้งจะเพิ่มหรือลดดัชนีจากนั้นบีบให้อยู่ในช่วง 0-6 ในที่สุดสตริงที่ถือเกียร์จะถูกทำดัชนีและส่งคืน

ส่งคืนอักขระ Clojure ที่แสดงถึงเกียร์ปัจจุบัน เกียร์ 1 ถูกส่งกลับเป็น\1และเกียร์ 'N' \Nถูกส่งกลับเป็น

คำอธิบายก่อนตีกอล์ฟ ติดตามตัวเลขเพราะมันไม่ได้อ่านจากบนลงล่าง

; Expects ^ for shift-up, and V (or anything else) for shift down
; Returns a character representing the current gear
(defn shift [shift-str]
  ((vec "1N23456") ; 4. Then index the gear list with the calculated index, and return
   (reduce (fn [current-gear s] ; 1. Fold over the shift symbols
             (max 0 (min 6 ; 3. Clamp it to the range 0-6 so we don't overflow
                      ((if (= s \^) inc dec) ; 2. If the shift is up, increase, else decrease
                       current-gear))))
           1
           shift-str)))

0

Python 3, 67 63 ไบต์

k=1
for i in input():k+=[k<6,-(k>0)][i<'1']
print('1N23456'[k])

ทางออกที่ตรงไปตรงมาสวย

-4 ไบต์ขอบคุณ @ovs!

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.