Colorful numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 571: | Line 571: | ||
Total: 57,256 |
Total: 57,256 |
||
</pre> |
|||
=={{header|jq}}== |
|||
''' Generic Utility Functions''' |
|||
<syntaxhighlight lang=jq> |
|||
# Uncomment for gojq |
|||
# def _nwise($n): |
|||
# def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end; |
|||
# n; |
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
|||
# Generate a stream of the permutations of the input array. |
|||
def permutations: |
|||
if length == 0 then [] |
|||
else |
|||
range(0;length) as $i |
|||
| [.[$i]] + (del(.[$i])|permutations) |
|||
end ; |
|||
</syntaxhighlight> |
|||
'''Colorful Numbers''' |
|||
<syntaxhighlight lang=jq> |
|||
def isColorful: |
|||
def digits: [tostring | explode[] | [.] | implode | tonumber]; |
|||
if . < 0 then false |
|||
elif . < 10 then true |
|||
else . as $n |
|||
| digits as $digits |
|||
| if any($digits[]; . == 0 or . == 1) then false |
|||
else ($digits|unique) as $set |
|||
| ($digits|length) as $dc |
|||
| if ($set|length) < $dc then false |
|||
else label $out |
|||
| foreach range(2; $dc) as $k ({$set}; |
|||
foreach range(0; $dc-$k+1) as $i (.; |
|||
(reduce range($i; $i+$k) as $j (1; . * $digits[$j])) as $prod |
|||
| if .set|index($prod) then .return = 0, break $out |
|||
else .set += [$prod] |
|||
end ; |
|||
. ); |
|||
select(.return) ) // null |
|||
| if .return == 0 then false else true end |
|||
end |
|||
end |
|||
end; |
|||
# Emit a stream of colorfuls in range(a;b) |
|||
def colorfuls(a;b): |
|||
range(a;b) | select(isColorful); |
|||
</syntaxhighlight> |
|||
'''The Tasks''' |
|||
<syntaxhighlight lang=jq> |
|||
def task($n): |
|||
[colorfuls(0; $n)] |
|||
| "The \(length) colorful numbers less than \($n) are:", |
|||
(_nwise($10) | map(lpad(4)) | join(" ")) ; |
|||
def largestColorful: |
|||
[[range(2;10)] | permutations | join("") | tonumber | select(isColorful)] | max; |
|||
# Emit a JSON object giving the counts by number of digits |
|||
def classifyColorful: |
|||
def nonTrivialCandidates: |
|||
[range(2; 10)] |
|||
| range(1; 9) as $length |
|||
| combinations($length) |
|||
| join("") |
|||
| tonumber; |
|||
reduce (0,1,nonTrivialCandidates) as $i ({}; |
|||
if $i|isColorful |
|||
then .[$i|tostring|length|tostring] += 1 |
|||
else . |
|||
end); |
|||
task(100), |
|||
"", |
|||
"The largest possible colorful number is \(largestColorful)." |
|||
"", |
|||
"The counts of colorful numbers by number of digits are:", |
|||
(classifyColorful |
|||
| (., "\nTotal: \([.[]]|add)")) |
|||
</syntaxhighlight> |
|||
'''Invocation''': jq -ncr -f colorful.jq |
|||
{{Output}} |
|||
<pre> |
|||
The 66 colorful numbers less than 100 are: |
|||
0 1 2 3 4 5 6 7 8 9 |
|||
23 24 25 26 27 28 29 32 34 35 |
|||
36 37 38 39 42 43 45 46 47 48 |
|||
49 52 53 54 56 57 58 59 62 63 |
|||
64 65 67 68 69 72 73 74 75 76 |
|||
78 79 82 83 84 85 86 87 89 92 |
|||
93 94 95 96 97 98 |
|||
The largest possible colorful number is 98746253. |
|||
The counts of colorful numbers by number of digits are: |
|||
{"1":10,"2":56,"3":328,"4":1540,"5":5514,"6":13956,"7":21596,"8":14256} |
|||
Total: 57256 |
|||
</pre> |
</pre> |
||