-2
\$\begingroup\$

Assumption

A cigarette can be made by combining four cigarette butts. Cigarette butts last infinitely until smoked.

Explanation

Say you have 31 butts. That means, you can make 7 cigarettes from 28 butts and have 3 left over.
Now, these 7 cigarettes will yield 7 butts when you're done smoking them. That means you have 3 + 7 = 10 butts.
These 10 butts make 2 new cigarettes and 2 butts are left over, but the two new cigarettes yield 2 more butts, for a total of 4 butts remaining.
Therefore, 31 butts means you can smoke 10 cigarettes in total.

The question

Given a number of butts N, where 0 <= N < 1000000, find out how many cigarettes can be made from those N butts.

Input format

A single line containing the integer N.

Output format

Print the number of cigarettes that can be made from N butts.

Test cases

  1. Input:
    31
    
    Output:
    10
    
  2. Input:
    7
    
    Output:
    2
    
  3. Input:
    4
    
    Output:
    1
    

For N <= 3, the output should be 0.

My personal best C solution is 66 bytes. A friend helped reduce it to 63 bytes, and someone else I know was able to come up with a 43-byte solution (also in C).

\$\endgroup\$
2
  • 3
    \$\begingroup\$ I downvoted this for being a chameleon challenge because it reduces to a trick that makes it trivial. \$\endgroup\$
    – xnor
    Feb 4 at 23:03
  • 3
    \$\begingroup\$ The trick is that the "recursive" look at the problem is a distraction. With 31 butts, you can turn four of them into a cigarette, smoke it, and gain a butt to reach 28. So we are simply counting the number of times we can subtract 3 before we reach n<4. \$\endgroup\$
    – Lynn
    Feb 5 at 3:33

8 Answers 8

7
\$\begingroup\$

JavaScript (ES6), 10 bytes

Unless I'm missing something, this boils down to:

n=>~-n/3|0

Try it online!

Or 9 bytes with BigInts.

\$\endgroup\$
4
\$\begingroup\$

C (gcc), 14 bytes

f(n){n=~-n/3;}

Try it online!

Port of Arnauld's JavaScript answer

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This was the eventual best 43 byte solution a friend ended up on, too -- main(c){scanf("%d",&c);printf("%d",~-c/3);}. I can't figure out how to shorten the scanf/printf though, unfortunately. \$\endgroup\$ Feb 5 at 8:21
3
\$\begingroup\$

Charcoal, 6 bytes

I÷↔⊖N³

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

    N   Input as a number
   ⊖    Decremented
  ↔     Absolute value
 ÷      Integer divided by
     ³  Literal integer `3`
I       Cast to string
        Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Jelly, 4 bytes

s3ṖL

A monadic Link that accepts the number of butts and yields the number of cigarettes.

Try it online! Or see the test-suite.

How?

Group the butts into threes, and consider the last up to three butts your "Pile". Take each of the other groups of three butts in turn and combine with one of your Pile, replenishing your Pile with the generated butt.

s3ṖL - Link: integer, Butts
s3   - split into threes (rightmost lacking if necessary)
  Ṗ  - pop (remove your Pile from the right side)
   L - length
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @emanresuA doesn't work when n=0 (should give 0) \$\endgroup\$
    – Bubbler
    Feb 4 at 23:37
2
\$\begingroup\$

Funge-98, 12 bytes

&:1-3/\!!*.@

Try it online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 3 bytes

<3÷

Try it online or verify all test cases.

Explanation:

<    # Decrease the (implicit) input-integer by 1
 3÷  # Then integer-divide it by 3
     # (after which the result is output implicitly)

Note: Integer-division in 05AB1E (built in Elixir) will round towards 0 for negative values, so this works even for \$n=0\$.

\$\endgroup\$
1
  • \$\begingroup\$ code means heart divides \$\endgroup\$
    – qwr
    Feb 7 at 4:31
1
\$\begingroup\$

Retina 0.8.2, 11 bytes

.+
$*
...\B

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

.+
$*

Convert to unary.

...\B

For every three butts, if there is at least one other butt left over, you can smoke another cigarette, which will leave a butt, which therefore replenishes the butt left over. So this just counts the number of groups of three butts while still leaving at least one butt left over.

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

Rattle, 6 bytes

|-/3R1

Try it Online!

Explanation

|         take input
 -        decrement
  /3      divide by 3
    R1    reformat as int, implicit output

Alternatively, the code could be |!-/3i

\$\endgroup\$

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.