65
Labyrinth, 5 bytes
):
\!
♫ The IP in the code goes round and round ♫
Relevant instructions:
) Increment top of stack (stack has infinite zeroes at bottom)
: Duplicate top of stack
! Output top of stack
\ Output newline
56
Python, Score: 24 16
This solution, like Falko's one, is based on measuring the "foreground" area and dividing it by the average grain area.
In fact, what this program tries to detect is the background, not so much as the foreground.
Using the fact that rice grains never touch the image boundary, the program starts by flood-filling white at the top-...
54
Python, no string manipulation
def f(n):
n += 1
p = 1
m = n
while m:
if m % 1000 == 666:
n += p - n % p
p *= 10
m /= 10
return n
Works by finding powers of 10, p, where 666 appears, and adding p - n % p to n which replaces 666xxxxx with 66700000.
51
><>, 8 bytes
01+:nao!
Steps:
Push 0 on the stack
Add 1 to the top stack element
Duplicate top stack element
Output the top of the stack as number
Output a newline
Go to step 2 by wrapping around and jumping the next instruction (step 11)
(A less memory efficient (hence invalid) program is llnao.)
50
JavaScript (updated to work with all test cases)
The little-known truth is that there are actually four 6s, but one of the betrayed the others and polymorphed into code form to eradicate them from the world digits of the numbers. Here is that traitorous six:
x=prompt(''+
'Enter number');
alert( ( (~x[
'ind'+
'exOf']('666')))?(x
.replace(/666(.*...
answered Apr 29 '14 at 12:51
37
80386 Machine Code, 4 bytes
F3 0F B8 C1
which takes the integer in cx and outputs the count in ax, and is equivalent to:
popcnt ax, cx ; F3 0F B8 C1
And here is an 11 10 byte solution not using POPCNT:
31 C0 D1 E9 10 E0 85 C9 75 F8
which is equivalent to:
xor ax, ax ; 31 C0 Set ax to 0
shr cx, 1 ; D1 E9 Shift cx to the right by ...
36
Jelly, 4 bytes
;\f"
Try it online!
How it works
;\f" Main link. Argument: S (string)
;\ Cumulatively reduce by concatenation.
This yields the array of all prefixes of S.
f" Vectorized filter.
Keep only occurrences of the nth letter in the nth prefix.
35
Dyalog APL, 6 2 bytes
⊥⍨
Test it on TryAPL.
How it works
⊥ (uptack, dyadic: decode) performs base conversion. If the left operand is a vector, it performs mixed base conversion, which is perfect for this task.
For a base vector b = bn, ⋯, b0 and a digit vector a = an, ⋯, a0, b ⊥ a converts a to the mixed base b, i.e., it computes b0⋯...
35
JavaScript (Node.js), 38 bytes
a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n
Try it online!
Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.
Thanks Arnauld, save 2 3 bytes
30
Python + OpenCV : Score 27
Horizontal line scanning
Idea : scan the image, one row at a time. For each line, count the number rice grains encountered (by checking if pixel turns black to white or the opposite). If number of grains for the line increase (compared to previous line), it means we encountered a new grain. If that number decrease, it means we ...
30
MATL, 21 bytes
3XytPJ*-X*Xj~4&1ZIunq
The input is a matrix with 1 for \ and j (imaginary unit) for /.
Try it online! Or verify all test cases.
With some extra code, you can see the different pieces in random colours. Or increase the resolution for a better looking result.
Explanation
Consider input [1,j; 1,1; j,1; j,j] as an example. This ...
29
JavaScript - 184 169 (with jQuery)
b="input",a="<input type=checkbox>",c=":checked";$("body").html("SELECT ANY TWO"+a+"FAST"+a+"GOOD"+a+"CHEAP").click(function(){$(b).not(c).attr("disabled",!!$(b+c)[1])})
http://jsfiddle.net/L33JK/16/
EDIT: improved with help from @Daniel Lisik - https://codegolf.stackexchange.com/a/26805/16278
28
MATL, 19 18 17 13 bytes
5 bytes off thanks to @LeakyNun's idea (see his answer) of using the imaginary unit as a base for exponentiation.
Jj11\^Ys8#uos
Try it online! Test cases: 1, 2.
Explanation
The code traces the path using unit steps in the complex plane. Then it counts how many times each position was visited, and outputs how many positions were ...
28
05AB1E, 8 7 5 bytes
Saved 2 bytes thanks to @Adnan
0š¥þO
Try it online!
How?
This is using the algorithm that was first found by @tsh. If you like this answer, make sure to upvote their answer as well!
Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.
For instance, ...
26
bash (20)
seq $1 $2|grep -c $3
Usage
$ bash count.sh 0 1000000 2
468559
25
Haskell, 21 bytes
main=mapM_ print[1..]
Arbitrary-precision integers and infinite lists make this easy :-)
Luckily mapM_ is in the Prelude. If Data.Traversable was as well, we even could shrink it to 19 bytes:
main=for_[1..]print
24
Python + OpenCV: Score 84
Here is a first naive attempt. It applies an adaptive threshold with manually tuned parameters, closes some holes with subsequent erosion and dilution and derives the number of grains from the foreground area.
import cv2
import numpy as np
filename = raw_input()
I = cv2.imread(filename, 0)
I = cv2.medianBlur(I, 3)
bw = cv2....
24
Mathematica, score: 7
i = {"http://i.stack.imgur.com/8T6W2.jpg", "http://i.stack.imgur.com/pgWt1.jpg",
"http://i.stack.imgur.com/M0K5w.jpg", "http://i.stack.imgur.com/eUFNo.jpg",
"http://i.stack.imgur.com/2TFdi.jpg", "http://i.stack.imgur.com/wX48v.jpg",
"http://i.stack....
24
Jelly, 6 bytes
Qx2œ&@
Try it online! or verify all test cases.
How it works
Qx2œ&@ Main link. Argument: s (string)
Q Unique; deduplicate s.
x2 Repeat each character.
œ&@ Take the multiset intersection of s and the previous result.
23
Javascript (ES5) with jQuery - 143 (Demo)
I modified Matt's solution and golfed it as far down as I think it can go:
$("*").html(["SELECT ANY TWO","FAST","GOOD","CHEAP"].join("<input type=checkbox onclick=(a=$('input:not(:checked)')).prop('disabled',!a[1])>"))
Javascript (ES5) without jQuery - 185 175 (Demo)
Using jQuery is kind of cheating, so ...
23
Gol><>, 3 bytes
P:N
Steps:
Add 1 to the top stack element (at start it is an implicit 0)
Duplicate top stack element
Pop and output the top of the stack as number and a newline
Wrap around to step 1 as we reached the end of the line
23
C (64-bit architecture only), 53 bytes
Relies on pointers being at least 64 bits and prints them in hex using the %p specifier. The program would return right when it hits 2^128.
char*a,*b;main(){for(;++b||++a;)printf("%p%p ",a,b);}
23
Retina, 1 byte
]
Try it online! (The first line enables a linefeed-separated test suite.)
By default, Retina counts the number of matches of the given regex in the input. The unwrapped size is simply equal to the number of [] pairs in the input and therefore to the number of ].
answered Nov 10 '16 at 15:07
Martin Ender
188k6262 gold badges418418 silver badges927927 bronze badges
23
Python 2, 77 75 74 70 bytes
Thanks to @MartinEnder for suggesting the limit of 9e5 which enderd up working after a change.Thanks to @mschauer for suggesting an infinite stream, saving 4 bytes.
def f(n=0):
i=f()
while 1:n+=1;yield next(i)if'7'in`n`or n%7<1else n
This is a generator that yields an infinite stream of the numbers.
23
05AB1E, 3 bytes
Code:
ÝJg
Uses the CP-1252 encoding. Try it online!
Explanation:
Ý # Range [0 .. input]
J # Join into one string
g # Get the length of the string
22
Marbelous, 11450 4632 bytes
Printing decimals is a pain!!
Definitely not winning with this one, but I thought I'd give it a shot. I hope it's ok that it pads the output to 40 zeros (to fit 2^128).
00@0..@1..@2..@3..@4..@5..@6..@7..@8..@9..@A..@B..@C..@D..@E..@F..@G..@H..@I..@J
\\++..00..00..00..00..00..00..00..00..00..00..00..00..00..00..00..00..00..00.....
21
GolfScript, 20 bytes
~]7/${2%256base}/)\-
Try it online.
Test cases
$ echo 0.0.0.0 255.255.255.255 | golfscript range.gs
4294967296
$ echo 255.255.255.255 0.0.0.0 | golfscript test.gs
4294967296
$ echo 1.2.3.4 1.2.3.4 | golfscript test.gs
1
$ echo 56.57.58.59 60.61.62.63 | golfscript test.gs
67372037
How it works
~] # Evaluate and collect into ...
21
C# + OpenCvSharp, Score: 2
This is my second attempt. It is quite different from my first attempt, which is a lot simpler, so I am posting it as a separate solution.
The basic idea is to identify and label each individual grain by an iterative ellipse fit. Then remove the pixels for this grain from the source, and try to find the next grain, until every ...
21
K, 4 bytes
#,/\
In K, ,/ will join all the elements of a list. The common idiom ,// iterates to a fixed point, flattening an arbitrarily nested list completely. ,/\ will iterate to a fixed point in a similar way, but gather a list of intermediate results. By counting how many intermediate results we visit before reaching the fixed point (#), we get the ...
21
Pyth, 6 bytes
Thanks to @Doorknob for taking off 1 byte.
Thanks to @Maltysen for taking off 5 bytes.
s@VQ._
Try it online!
How it works
For example, take the string "bonobo".
._ makes a list: ['b', 'bo', 'bon', 'bono', 'bonob', 'bonobo']
VQ._ means "the preceding function vectorized (applied in parallel) over Q and ._", which means Q (...
Only top voted, non community-wiki answers of a minimum length are eligible
Related Tags
counting × 112code-golf × 105
string × 27
number × 15
grid × 11
array-manipulation × 10
ascii-art × 9
sequence × 8
math × 7
combinatorics × 7
code-challenge × 4
geometry × 4
restricted-source × 3
primes × 3
binary × 3
base-conversion × 3
image-processing × 3
subsequence × 3
arithmetic × 2
popularity-contest × 2
game × 2
date × 2
matrix × 2
permutations × 2
path-finding × 2