37
\$\begingroup\$

My stovetop has 10 (0 through 9) different settings of heat and a very odd way of cycling through them.

  • It always starts at 0

  • When I hit plus it increments the number, unless the number is 9 in which case it becomes 0, or the number is 0 in which case it becomes 9.

  • When I hit minus it decrements the number, unless the number is zero in which case it becomes 4.

There are no other temperature control buttons.

In this challenge you will take as input a string of commands and output which setting my stovetop ends up on after running that sequence.

Answers will be scored in bytes with fewer bytes being better.

Input

You may take input in any of the following formats:

  • A list/array/vector/stream of booleans/1s or 0s/1s or -1s
  • A string (or list/array/vector/stream) of two different characters (which should be consistent for your program)

And you may output

  • An integer on the range 0-9.

  • A character on the range '0'-'9'.

  • A string of size 1 consisting of the above.

Testcases

Input as a string - for decrement and + for increment.

 : 0
- : 4
+ : 9
-- : 3
-+ : 5
+- : 8
++ : 0
--- : 2
--+ : 4
-+- : 4
-++ : 6
+-- : 7
+-+ : 9
++- : 4
+++ : 9
---- : 1
---+ : 3
\$\endgroup\$
12
  • 1
    \$\begingroup\$ (I see that this is a relatively weird input format) Can I take input like this f(f(f(0,M),M),P)? (for the string MMP) What about (M)(M)(P)? \$\endgroup\$
    – DELETE_ME
    Feb 1, 2021 at 10:26
  • 1
    \$\begingroup\$ @user202729 No. \$\endgroup\$
    – Wheat Wizard
    Feb 1, 2021 at 10:35
  • 1
    \$\begingroup\$ @user202729 If it is a standard way of taking input you don't need to ask. If it is not then either it should be, in which case you should bring that up in the relevant place on meta, or it shouldn't be in which case I don't intend to permit it. \$\endgroup\$
    – Wheat Wizard
    Feb 1, 2021 at 11:13
  • 2
    \$\begingroup\$ @Jonah Under the equivalence by temperature (+ = +++ because they both give 9), they certainly don't form a group. Something like 5+9 is not well defined (9 can be represented by any odd number of +s so 5+9 could be 6, 8 or 0). Under the equivalence of the sequences themselves, they also don't form a group. They are isomorphic binary strings under concatenation and thus don't have an inverse. You can relax the latter into a category, and try some other stuff but nothing really interesting come out of it. I don't think this corresponds to any mathematical structure of any note. \$\endgroup\$
    – Wheat Wizard
    Feb 1, 2021 at 16:25
  • 1
    \$\begingroup\$ A related question could be "Take as input the initial temperature and the final temperature and output the necessary increment/decrement sequence." \$\endgroup\$
    – LorenDB
    May 22, 2021 at 13:36

41 Answers 41

1
2
1
\$\begingroup\$

PowerShell, 58 54 bytes

-4 bytes thanks @Zaelin Goodman

$args-ge0|%{$x=(($x-1),4,(($x+1)%10),9)[2*$_+!$x]}
+$x

Try it online!

Any version of PowerShell. See also the solution for PS7+

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 54 bytes? :) \$\endgroup\$ Feb 3, 2021 at 14:51
  • 1
    \$\begingroup\$ awesome! thanks! ٩(^‿^)۶ \$\endgroup\$
    – mazzy
    Feb 3, 2021 at 16:21
1
\$\begingroup\$

naz, 152 bytes

2x0v2x3v1a2x1v1a2x2v7a2x4v1x0f3v1o0x1x1f3v3x0v2e1s2x3v6f0x1x2f4a2x3v6f0x1x3f3v3x0v4e3x4v5e1a2x3v6f0x1x4f9a2x3v6f0x1x5f9s2x3v6f0x1x6f1r3x0v0e3x1v1e3f0x6f

Expects a null-terminated string of SOH and STX characters (U+0001 and U+0002), representing the minus and plus buttons respectively. Variable 3 stores the temperature.

Explanation (with 0x instructions removed)

2x0v2x3v                   # Set both variables 0 and 3 equal to 0
1a2x1v                     # Set variable 1 equal to 1
1a2x2v                     # Set variable 2 equal to 2
7a2x4v                     # Set variable 4 equal to 9
1x0f                       # Function 0
    3v1o                   # Output variable 3
1x1f                       # Function 1
    3v                     # Move variable 3 into the register
      3x0v2e               # Goto function 2 if it equals variable 0
            1s2x3v6f       # Otherwise, subtract 1, write to variable 3, & call function 6
1x2f                       # Function 2
    4a2x3v6f               # Add 4, write to variable 3, and call function 6
1x3f                       # Function 3
    3v                     # Move variable 3 into the register
      3x0v4e               # Goto function 4 if it equals variable 0
            3x4v5e         # Goto function 5 if it equals variable 4
                  1a2x3v6f # Otherwise, add 1, write to variable 3, and call function 6
1x4f                       # Function 4
    9a2x3v6f               # Add 9, write to variable 3, and call function 6
1x5f                       # Function 5
    9s2x3v6f               # Subtract 9, write to variable 3, and call function 6
1x6f                       # Function 6
    1r                     # Read a byte of input
      3x0v0e               # Goto function 0 if it equals variable 0 (NUL)
            3x1v1e         # Goto function 1 if it equals variable 1 (SOH)
                  3f       # Otherwise, call function 3
6f                         # Call function 6
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 40 bytes

Fold[+##/.-1->4/. 10|1/;#2>0->9-#&,0,#]&

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java (JDK), 42 bytes

a->a.reduce(0,(t,c)->t<1?9>>c:(c*8-~t)%10)

Try it online!

Another port of Arnauld's JavaScript answer.

The input is an IntStream with - being represented by 1 and + being represented by 0.

Credits

\$\endgroup\$
0
1
\$\begingroup\$

Stax, 23 14 bytes

Ç∞/▌Çklº<&CV@‼

Run and debug it

Input as a list of 1s and -1s.

-9 bytes from Kevin's answer.

\$\endgroup\$
1
\$\begingroup\$

jq, 44 bytes

reduce.[]as$n(0;(7?+(.>0//2.5*$n)//$n+.)%10)

Try it online!

with 1 for + and -1 for -

\$\endgroup\$
1
\$\begingroup\$

MathGolf, 17 bytes

0ê{∙85ߧ⌐_¬┌*ßΣ♂%

Uses -1/1 for -/+ respectively.

Try it online.

Explanation:

0                  # Start with a 0 (let's call this `r`)
                   #  STACK: 0
 ê                 # Push all inputs as integer-array
                   #  STACK: 0,[input-list]
  {                # Foreach over them (let's call these `n`):
                   #   STACK: r,n
   ∙               #  Triplicate the current integer
                   #   STACK: r,n,n,n
    85ß            #  Wrap it in a triplet-list with 8 and 5
                   #   STACK: r,n,n,[n,8,5]
       §           #  Use `n` to index into this list (0-based modulair, so 1→8; -1→5)
                   #  (let's call this `i`)
                   #   STACK: r,n,i
        ⌐          #  Rotate the stack
                   #   STACK: n,i,r
         _         #  Duplicate the top
                   #   STACK: n,i,r,r
          ¬        #  Rotate the stack back
                   #   STACK: r,n,i,r
           ┌       #  Check if the top is equal to 0
                   #   STACK: r,n,i,r==0
            *      #  Multiply the top two
                   #   STACK: r,i,n*(r==0)
             ß     #  Wrap the top three values into a list
                   #   STACK: [r,i,n*(r==0)]
              Σ    #  Sum them together
                   #   STACK: r+i+n*(r==0)
               ♂%  #  Modulo-10
                   #   STACK: (r+i+n*(r==0))%10
                   # (afterwards, the entire stack is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 23 bytes

≔⁰θFS≔⎇⁼ι+﹪⊕∨θ⁸χ⊖∨θ⁵θIθ

Try it online! Link is to verbose version of code. Explanation:

≔⁰θ

Start with heat setting 0.

FS

Loop through the input string.

≔⎇⁼ι+﹪⊕∨θ⁸χ⊖∨θ⁵θ

If the next character is a plus, then take the heat setting, or 8 if it was zero, and increment it modulo 10, otherwise take the heat setting, or 5 if it was zero, and decrement it.

Iθ

Output the final heat setting.

\$\endgroup\$
0
\$\begingroup\$

Japt, 21 bytes

Port of @Arnauld's Answer to Japt.

Expects input to be a binary array where 0 represents increment and 1 represents decrement

£T=T°?X?T-2:T%A:9ÁXÃT

Try it

Explanation

£              // Map input with func (X) {
 T=            //   update T:
   T°?         //     if T is not 0 (increment T afterwards)
      X?       //       if X is 1 ('-' case)
        T-2    //          subtract 2
      :        //       else      ('+' case)
        T%A    //          modulo 10
   :           //     else
     9ÁX       //       yield 4 if X is 1 ('-' case), or 9 if X is 0 ('+' case) 
ÃT             // End map; implicit output T
\$\endgroup\$
0
\$\begingroup\$

Raku, 49 46 41 bytes

{reduce {($^u%7-2,$^t+$u)[?$t]%10},0,|$_}

Takes input as a list where 1 represents + and -1 represents -.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Racket, 81 bytes

(λ(a)(foldl(λ(x r)(match(list x r)['(1 0)9]['(1 9)0]['(-1 0)4][_(+ r x)]))0 a))

Try it online!

Just simple but verbose pattern matching:

(define (f a)
  (foldl (λ (x r)
            (match (list x r)
                 ['( 1 0) 9]
                 ['( 1 9) 0]
                 ['(-1 0) 4]
                 [_ (+ r x)]))
         0
         a))
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.