13
\$\begingroup\$

I have a small pill box of \$n\$ slots. (Think of it as a linear one like the image below, but not necessarily having 7 slots.)

a green daily pill box with the initials of the days of the weeks, starting with S for Sunday, and continuing as M, T, W, T, F, S for the remaining days

Each slot has at most one pill. Every day, I take one of two actions: take one pill from the pill box, or refill the pill box. The order of taking a pill alternates between left-to-right and right-to-left on each refill, except when the pill box was totally empty before the refill, in which case I always take the next pill from the left. The pill box is initially filled, and I initially take pills from the left. (I never try to take a pill from an empty box or refill a full box.)

Believe me, I take pills exactly as I describe here IRL. If you wonder why: I alternate directions as a simple way to prevent a pill from being too long in the pill box, and I just forget directions when the pill box is totally empty :P

Example with 5 slots:

Action Number Action State of slots Direction
0. Initial: [1, 1, 1, 1, 1] (order: ->)
1. Take pill: [0, 1, 1, 1, 1] (->)
2. Take pill: [0, 0, 1, 1, 1] (->)
3. Take pill: [0, 0, 0, 1, 1] (->)
4. Refill: [1, 1, 1, 1, 1] (<-)
5. Take pill: [1, 1, 1, 1, 0] (<-)
6. Take pill: [1, 1, 1, 0, 0] (<-)
7. Take pill: [1, 1, 0, 0, 0] (<-)
8. Refill: [1, 1, 1, 1, 1] (->)
9. Take pill: [0, 1, 1, 1, 1] (->)
10. Take pill: [0, 0, 1, 1, 1] (->)
11. Take pill: [0, 0, 0, 1, 1] (->)
12. Take pill: [0, 0, 0, 0, 1] (->)
13. Take pill: [0, 0, 0, 0, 0] (->)
14. Refill: [1, 1, 1, 1, 1] (->)
15. Take pill: [0, 1, 1, 1, 1] (->)
16. Refill: [1, 1, 1, 1, 1] (<-)
17. Take pill: [1, 1, 1, 1, 0] (<-)
18. Take pill: [1, 1, 1, 0, 0] (<-)
19. Take pill: [1, 1, 0, 0, 0] (<-)
20. Take pill: [1, 0, 0, 0, 0] (<-)
21. Take pill: [0, 0, 0, 0, 0] (<-)
22. Refill: [1, 1, 1, 1, 1] (->)
23. Take pill: [0, 1, 1, 1, 1]

Inputs to your program or function:

  • \$n\$, the size of the pill box.
  • The list of my actions. You may choose any two distinct, consistent values to represent "take pill" and "refill".

The output is the list of the state of my pill box after each action. You may choose to include or exclude the initial state. You may also choose any two distinct, consistent values to represent a slot being full or empty.

If you encode "take pill" and "refill" as numbers 1 and 0 respectively, the input to your code for the above scenario would be

5, [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1]

and the expected output is the list of arrays on the right side (you don't need to output the "order"), with or without the zeroth row.

Standard rules apply. The shortest code in bytes wins.

\$\endgroup\$
16
  • 1
    \$\begingroup\$ @Anush Uh...what? I assumed you meant the image at the top by "picture". If you actually mean the code block in the middle, it was entirely hand-written by me. "How could we do the same" - it's up to you, that's the challenge (though you don't need to exactly replicate the wall of vertically aligned text). \$\endgroup\$
    – Bubbler
    Sep 27 at 6:03
  • 3
    \$\begingroup\$ @Anush I believe it's just a picture of a real product, so no fancy computer graphics stuff. \$\endgroup\$
    – Bubbler
    Sep 27 at 8:04
  • 9
    \$\begingroup\$ I never thought of that meaning of "WTF" \$\endgroup\$
    – Luis Mendo
    Sep 27 at 9:16
  • 2
    \$\begingroup\$ "I alternate directions as a simple way to prevent a pill from being too long in the pill box" - but a pill can stay in a middle cell indefinitely if the number of days between refills is less than half the number of cells. You could avoid this by treating the pill box as a circular queue, taking pills from the head of the queue and adding pills at the tail, and maintaining at least one empty cell instead of fully refilling the box so you know where the head and tail are. \$\endgroup\$ Sep 28 at 1:54
  • 2
    \$\begingroup\$ (Or you could just not worry about how long pills are in the box, or if the box does have 7 cells, you could take the pill for the current day every day, like such 7-cell boxes are designed to be used.) \$\endgroup\$ Sep 28 at 1:55

11 Answers 11

5
\$\begingroup\$

05AB1E, 18 bytes

Takes input as a string of digits where 1 encodes take pill and 0 encodes refill.

-1 byte thanks to Kevin Cruijssen.

ηε0¡€g¤IL‹s5¡θgGR

Try it online!

I initially thought we are supposed to just generate the final state, which is why this program iterates over the prefixes. (But that might not even be a bad approach)

ηε                   # for each prefix of the input:
  0¡                 # split it on "0"
    €g               # take the length of each segment
       ¤             # take the last number (number of uses since last refill)
                     # this leaves the full list on the stack
        IL           # push the range [1 .. size of pill box]
          ‹          # element-wise less than
                     # this creates a list [0, ..., 0, 1, ..., 1] with the remaining number of pills
           s         # swap to the list of lengths
            5¡       # split the list in the register on 5 (direction resets)
              θg     # take the length of the last part
                GR   # reverse the list 1 times less than than this
\$\endgroup\$
1
  • 1
    \$\begingroup\$ ©θ and ® can be ¤ and s for -1. \$\endgroup\$ Sep 27 at 13:09
4
\$\begingroup\$

Vyxal, 30 20 bytes

$(n[¥↳£¹|‹]:×*¹↳¥ßṘ,

Try it Online!

It's slightly satisfying using two different overloads of the same element.

$(                   # Iterating over the list of instructions
  n[                 # If it's truthy (refill)
    ¥↳               # Bitshift the current value by the register
                     # Iff the register is 0, and the current value is 1, the result is truthy.
      £              # Store this to the register
       ¹             # Refill, pushing original input
        |‹]          # Otherwise decrement (take a pill)
           :         # Duplicate
            ×*       # Get that many asterisks
              ¹↳     # Align
                ¥ßṘ  # If the register is truthy (we're taking pills from the left) reverse
                   , # Print.

Older version, where I was a big brain and tried to store the whole box:

1£ƛ1;?(n[:T¥ßṘt0Ȧ|:a¬¥¬∨£ƛ1;]…

Try it Online!

1£                             # Store 1 to register (direction)
  ƛ1;                          # Array of length n, filled with 1
     ?(n                       # For each item in the list of refills...
        [                      # If it's truthy (Take pill)
         :T                    # Get the truthy indices
           ¥ßṘ                 # Reverse if direction is left
              t                # Get the last one (first empty pos if left, last if right0)
               0Ȧ              # Assign 0 to this index
                 |          ]  # Else (Refill)
                        £      # Set the register (direction) to...
                  :a¬          # Whether the current array is all falsy
                       ∨       # Logical or with...
                     ¥¬        # The opposite current value of the register
                               # If any values are truthy, the register (direction) flips
                               # Otherwise, it becomes 1 (left)

                         ƛ1;   # Refill (fill with 1s)
                             … # Whichever action we took, print the current array
\$\endgroup\$
2
  • \$\begingroup\$ If you store not only 1 and -1, but also the amount of pill, would that save bytes? \$\endgroup\$
    – okie
    Sep 27 at 4:51
  • \$\begingroup\$ @okie I've been storing the entire state of the box because big brain, so probably :) \$\endgroup\$
    – emanresu A
    Sep 27 at 6:23
3
\$\begingroup\$

JavaScript (V8), 80 bytes

(n,x,d)=>x.map(i=>'.'.repeat(i?--w:(d=w&&!d,w=n))[d?'padEnd':'padStart'](n),w=n)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @Dingus Forgot d was true even if +d is false. Fixed. \$\endgroup\$
    – l4m2
    Sep 27 at 9:12
3
\$\begingroup\$

Python 2, 89 88 bytes

n=input()
i,s=0,1
while 1:i,s=[0,i+1,i/n or-s,s][input()::2];print([0]*i+[1]*(n-i))[::s]

Try it online!

thanks to @ovs for -1 byte

Python 3, 100 bytes

n=int(input())
i,s=0,1
while 1:i,s=[0,i+1,i==n or-s,s][input()>"0"::2];print(([0]*i+[1]*(n-i))[::s])

Try it online!

take the actions in the form of individual inputs

How it works :

  • n store the length of the box, s the side from which the pill have to be taken (1 for left and -1 for right) and i the number of the pill

at each loop :

  • if the input() returns 1 (take a pill): then i,s=[0,i+1,i==n or-s,s][input()::2] will assign i to i+1 and s will stay inchanged

  • if the input() returns 0 (refill): then i,s=[0,i+1,i==n or-s,s][input()::2] will assign i to 0 and s to either -s or 1 dependig if the box is empty (i==n)

  • print([0]*i+[1]*(n-i))[::s] will then print imissig pills and n-i pills in the order defined by s

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

Ruby, 78 76 74 73 bytes

->n,m{b=*i=0;(b+m).map{|a|a<1?b=[1**i=b.max*~i/n]*n:b[i]=0&i+=i/n|1;p b}}

Try it online!

Explanation

For input, the 'take pill' and 'refill' actions are encoded as 1 and 0, respectively. Output is a series of arrays in which 1 indicates a full slot and 0 an empty slot. The initial state is included.

The initial state is created by adding a refill step to the start of the action list. The direction of movement is embedded in the slot index i. If we are moving left to right then i is non-negative (array indices \$0, 1, 2, \ldots\$). If we are moving right to left then i is negative (array indices \$-1,-2,-3,\ldots\$), that is, we read from the end of the pillbox array.

->n,m{                      # n = size of pillbox, m = list of actions
  b=*i=0;                   # initialise slot index i to 0 and pillbox b to [0]
  (b+m).map{|a|             # for each action, beginning with extra refill step (overloaded b)
    a<1?                    #   if refilling
      b=[1**i           ]*n #     fill (overwrite) the pillbox with n pills
            i=b.max*~i/n    #     if the pillbox is empty (b.max = 0), set i to 0, otherwise (b.max = 1) set i to -1|0 if previously moving right|left              
    :                       #   else
      b[i]=0&               #     take pill from slot i and...
      i+=i/n|1;             #     add 1|-1 to i if moving right|left
    p b                     #   print the pillbox
  }
}
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6),  78  76 bytes

Saved 2 bytes thanks to @l4m2

Expects (n)(array). Returns an array of strings. The initial state is included.

n=>a=>[d=0,...a].map(c=>a=c?a.replace(d||/1(?!1)/,0):'1'.repeat(n,d=!d|!+a))

Try it online!

Commented

n =>              // n = number of pills
a =>              // a[] = array of actions, re-used as the box string
[ d = 0,          // prepend a 'refill' directive
                  // and initialize the direction to 0 (right to left)
  ...a            // leave all other actions unchanged
].map(c =>        // for each action c:
  a =             //   update a:
    c ?           //     if this is a 'take pill' action:
      a.replace(  //       replace in a:
        d ||      //         either the first '1' (if d = 1)
        /1(?!1)/, //         or the last '1' (if d = 0)
        0         //         with '0'
      )           //       end of replace()
    :             //     else:
      '1'.repeat( //       generate a string consisting of ...
        n,        //       ... '1' repeated n times
        d = !d |  //       and update the direction to:
            !+a   //         - 1 (L -> R) if d = 0 or the box is empty
      )           //         - 0 (R -> L) if d = 1 and the box is not empty
)                 // end of map()
\$\endgroup\$
2
  • \$\begingroup\$ tio.run/… \$\endgroup\$
    – l4m2
    Sep 27 at 10:15
  • \$\begingroup\$ @l4m2 Thanks! I knew something was wrong with that part. :) \$\endgroup\$
    – Arnauld
    Sep 27 at 10:20
1
\$\begingroup\$

D, 250 202 198 197 bytes

import std.array,std.algorithm;U[]f(T,U)(T b,U A){U[]r=[[1].replicate(b)];T d=1,p;foreach(a;A){U P=r[$-1].dup;if(a*P.sum){P[p]=0;p+=d;}else{P=[1].replicate(b);d=a?1:-d;p=d>0?0:b-1;}r~=P;}return r;}

Try it online!

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

Zsh, 109 bytes

a=({1..$1})
$a(){<*&&r+=\|tac||r+=\|sort;>$a}
0()rm `eval ls$r|head -1`
for x;$x&&(eval \<$^a';s+=$?;';<<<$s)

Attempt This Online!

This seemed like a good way to go about it, until I re-read the challenge and it was more complicated than I thought (it started as a 40-byte answer... for a totally different challenge). A proper rewrite would surely be half the length.

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

Python 2, 105 bytes

n=input();k=[1]*n;d=0
for i in input():exec(i*"k[d]=0;d+=[1,-1][d<0]#"+"d=-(d>0)*any(k);k=[1]*n");print k

Try it online!

Geez. Need to find a better way to inverse the direction.

-22 thanks to @Jakque

\$\endgroup\$
3
  • \$\begingroup\$ d=[(d<0)-1,0][sum(k)<1] can be simplified into d=-(d>0)*any(k) also using a for loop to iterate directly trough l (instead poping out the elements) also save some bytes. Finnaly ["code 1","code 2"][condition] can be changed to condition*"code 1#"+"code 2" for 2 bytes. 105 bytes \$\endgroup\$
    – Jakque
    Sep 27 at 15:08
  • \$\begingroup\$ @Jakque thanks!!!! sorry for being late \$\endgroup\$
    – wasif
    Sep 29 at 15:06
  • \$\begingroup\$ [1,-1][d<0] can be shortened to -(d<0)|1 (or d>-1or-1). \$\endgroup\$
    – ovs
    Sep 29 at 15:17
0
\$\begingroup\$

Pip, 37 bytes

Ft0.bP[Yt?yR`\b1`s#(i:1Ny&!i)XaRVy]@i

Takes the steps as a string of 1s (take pill) and 0s (refill box); outputs the pillbox as a string of 1s (pills) and spaces (empty slots). Try it online!

Explanation

It's easier to store the pillbox in a consistent order and conditionally reverse it when printing. Arguments are size of pillbox (integer) and actions (string of 1s and 0s).

Ft0.bP[Yt?yR`\b1`s#(i:1Ny&!i)XaRVy]@i
                                       a,b are cmdline args; s is space; i is 0; y is ""
                                       We're going to store the pillbox in y and the
                                       direction in i (0 = LTR, 1 = RTL)
Ft                                     For each action t in
  0.b                                  the input actions with a 0 prepended (so that the
                                       box is "refilled" as the first step):
       Y                                Set y to the following quantity:
        t?                              If t is truthy (1, take pill):
          yR                             The pillbox string, replacing
            `\b1`                        regex matching the first 1
                 s                       with a space
                                        Otherwise (0, refill box):
                    i:                   Set i to
                      1Ny                 Number of pills left in the box
                         &                Logical AND
                          !i              Logical negation of previous i value
                  #(        )            Length of the above (always 1)
                             Xa          string-repeated (size of pillbox) times
      [                           ]     Put the new value of y in a list with
                               RVy      its reverse
                                   @i   Index into the list using i (i.e. reverse iff i=1)
     P                                  Print
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 40 bytes

Nθ≔⁰ηF⁺RS«≡ιR«≔∧η¬ζζ≔θη»≦⊖η≔⭆θ‹κηε⟦⎇ζε⮌ε

Try it online! Link is to verbose version of code. Uses R for refill and P (or anything else really) for pill and outputs a string of 0s and 1s for each state. Explanation:

Nθ

Input n.

≔⁰η

Start with 0 pills.

F⁺RS«

Loop over the commands, but with an extra Refill action. This saves me from having to explicitly initialise the direction.

≡ιR«

If this is a refill action, then...

≔∧η¬ζζ

Flip the direction, unless there are no pills, in which case reset the direction.

≔θη

Refill the pills.

»≦⊖η

Otherwise, decrement the number of pills.

≔⭆θ‹κηε

Generate a string of full and empty slots.

⟦⎇ζε⮌ε

Output the string or its reverse depending on the current direction.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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