| Bytes | Lang | Time | Link |
|---|---|---|---|
| 088 | Tcl | 181219T195032Z | david |
| 102 | Tcl | 180605T231303Z | sergiol |
| 066 | Zsh | 230124T010729Z | roblogic |
| 7125 | Vyxal s | 230723T120128Z | lyxal |
| 010 | Thunno 2 | 230722T164447Z | The Thon |
| 056 | Arturo | 230121T123255Z | chunes |
| 185 | Scala | 230417T070447Z | 138 Aspe |
| 009 | Husk | 230121T145926Z | Razetime |
| 044 | Wolfram Language Mathematica | 230210T041009Z | att |
| 081 | PowerShell | 230210T023834Z | Julian |
| 030 | Raku | 230209T054724Z | Sean |
| 082 | JavaScript | 230125T155938Z | EzioMerc |
| 106 | Elm | 230122T203301Z | Kirill L |
| 105 | Nim | 230124T211834Z | xigoi |
| 009 | Nibbles | 230124T205641Z | xigoi |
| 117 | C GCC | 230123T195604Z | Peter |
| 089 | Julia | 230123T202239Z | Ashlin H |
| 014 | Pip p | 230123T172118Z | DLosc |
| 013 | Vyxal | 230121T165933Z | lesobrod |
| nan | 230121T123959Z | The Thon | |
| 084 | Desmos | 220522T200349Z | naffetS |
| 058 | Factor + pairrocket | 220520T174804Z | chunes |
| 108 | C# Visual C# Interactive Compiler | 181219T181200Z | dana |
| 059 | Ruby | 181219T141628Z | Kirill L |
Tcl, 98 88 bytes
thx to sergiol
proc A {l 1\ 0 2\ 0} {lmap a\ b\ c $l {incr [llength [lsort -u "$a $b $c"]]}
list $1 $2}
Tcl, 102 bytes
proc S {L 1\ 0 2\ 0} {lmap {x y z} $L {incr [expr $x-$y|$y-$z?$x==$y|$y==$z|$x==$z?2:0:1]}
list $1 $2}
Zsh, 66 bytes
for i ({1..$#..3})T+=${#${(u)@[i,i+2]}}
<<<${#T//[^1]},${#T//[^2]}
Try it online!
65b(bugged)
67b(pure)
67b(utils)
72b
78b
83b
102b
116b
128b
Vyxal s, 57 bitsv1, 7.125 bytes
3ẇƛUL12f=
Explained
3ẇƛUL12f=
3ẇ # Wrap into chunks of 3
ƛ # to each chunk
UL # take the length of the chunk uniquified
12f= # and push whether 1 and 2 is in that length list
# the s flag reduces the list by addition, which is performed element-wise vectorised
💎
Created with the help of Luminespire.
Thunno 2, 10 bytes
3ẇıUl;12dc
Explanation
3ẇıUl;12dc # Implicit input
3ẇ # Split into threes
ı ; # Map over the list:
Ul # Length of uniquified list
12dc # Count 1s and 2s
# Implicit output
Scala, 185 bytes
Golfed version. Try it online!
val f:Array[Int]=>Array[Int]=l=> {var r=Array(0,0,0);var i=0;var a,b,c=0;while(i<l.length){b=l(i);c=l(i+1);a=l(i+2);r(if(a==b){if(b==c)0 else 1} else if(b==c||a==c)1 else 2)+=1;i+=3};r}
Ungolfed version. Try it online!
import java.util.Arrays
import java.util.function.Function
object Main {
def main(args: Array[String]): Unit = {
val f: Array[Int] => Array[Int] = l => {
var r = Array(0, 0, 0)
var i = 0
var a, b, c = 0
while (i < l.length) {
b = l(i)
c = l(i + 1)
a = l(i + 2)
r(if (a == b) {
if (b == c) 0 else 1
} else if (b == c || a == c) 1 else 2) += 1
i += 3
}
r
}
val tests = Array(
Array(2, 4, 2, 5, 5, 5, 4, 2, 1, 3, 3, 1),
Array(3, 5, 6, 5, 5, 7, 6, 6, 8, 7, 7, 7, 3, 4, 2, 4, 4, 3),
Array(3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 5, 4, 3),
Array(3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1)
)
for (test <- tests) {
val result = f.apply(test)
printf("%s: %s%n", Arrays.toString(test), Arrays.toString(result))
}
}
}
Wolfram Language (Mathematica), 44 bytes
0&@@@c@BlockMap[Max@*c,#,3]/@{3,2}&
c=Counts
BlockMap[Max@*Counts,#,3] #equal in each block of 3
Counts@ /@{3,2} count 3 equal, 2 equal
0&@@@ coerce Missing[...] to 0
PowerShell, 81 bytes
for($r=@{};$i-$args.Count;$i++){$r[($args[$i++..++$i]|sort|gu).Count]++}
$r[1..2]
Explanations
Look at the array by taking sub arrays of three successive elements $args[$i..($i+2)]
We count the number of items in the sub array after sorting and removing duplicates (gu)
Then we increment the count in the result object $r
From $r, we then show only the entries with a single or two numbers
Raku, 30 bytes
(+«*.rotor(3)».Set).Bag{1,2}
*.rotor(3)breaks up the input list into groups of three elements.».Setconverts each of those groups into aSet.+«converts each of those sets into its size..Bagmakes aBag(a set with multiplicity) out of those sizes.{1, 2}looks up in thatBagthe number of times sets with sizes of 1 and 2 were present.
JavaScript, 82 bytes
Without recursion
a=>(b=[,0,0],a.map(d=>([x,y,z,...a]=a,x&&++b[new Set([x,y,z]).size])),[b[1],b[2]])
Try it:
f=a=>(b=[,0,0],a.map(d=>([x,y,z,...a]=a,x&&++b[new Set([x,y,z]).size])),[b[1],b[2]])
console.log(f([3,5,6,5,5,7,6,6,8,7,7,7,3,4,2,4,4,3])); // [1, 3]
console.log(f([3,3,3,4,4,4,5,5,5,6,6,6,5,4,3])); // [4, 0]
console.log(f([3,4,5,6,7,8,9,8,7,6,5,4,3,2,1])); // [0, 0]
Elm, 108 106 bytes
import List.Extra as E
m=List.map
f x=m(\j->E.count((==)j)<|m(List.length<<E.unique)<|E.groupsOf 3 x)[1,2]
Nim, 105 bytes
import sequtils
proc e[S](s:S):S=[1,2].mapIt s.distribute(s.len div 3).mapIt(it.deduplicate.len).count it
Nibbles, 9 bytes
.,2,`?.`/3@,`$$@
Explanation
. Map
,2 [1,2]
, length of
`? find indices in
. map
`/ 3 split into chunks of 3
@ input
, length of
`$ deduplicate
$ the chunk
@ of the number (1 or 2)
C (GCC), 135 128 117 bytes
-7 bytes thanks to @ceilingcat
-11 more, again thanks to @ceilingcat
This is basically @ceilingcat's answer now, since he's improved it by more than 13 percent.
#define E(k)v[k-~i]==v[i
a;b;i;f(v,l)int*v;{for(a=b=i=0;i<l;i+=3)E()]&E(1)]?++a:E()]|E(1)]|E(1)+1]&&++b;*v=a;v[1]=b;}
Unfortunately, I'm too lazy to write a new explanation for the most recent answer, but it's quite similar to the previous 128-byte one:
#define E(j,k)v[j+i]==v[k-~i]
a;b;i;f(v,l)int*v;{for(a=b=i=0;i<l;i+=3)E(,)&&E(,1)&&++a||(E(,)||E(,1)||E(1,1))&&++b;*v=a;v[1]=b;}
f takes a vector (v) and its length (l) as arguments, and return its values in the first two elements of that vector. Explanation:
#define E(j,k)v[j+i]==v[k-~i] // k-~i == k-(-i-1) == k+i+1. an empty first argument equals +i, and an empty second argument equals -~i == i+1
a;b;i;
f(v,l)int*v;
{
for(a=b=i=0;i<l;i+=3)
E(,)&&E(,1)&&++a // if element i, i+1 and i+2 are all equal, increment a
||(E(,)||E(,1)||E(1,1))&&++b; // otherwise, if any two elements are equal, increment b
*v=a; // set the first element in v to a
v[1]=b; // set the second one to b
}
Julia, 89 bytes
!x=(~=sum;a=length(x);b=[x[i:i+2] for i=1:3:a];c=~allequal.(b);[c,a÷3-c-~allunique.(b)])
Julia has allequal and allunique built in. To find the number of triplets containing exactly 2 matching values (all remaining cases), I subtract these two amounts from the total. Replacing ÷ with / saves one byte, but the value becomes a float.
Pip -p, 14 bytes
_N#*UQ*g<>3M12
Takes the numbers as command-line arguments. Attempt This Online!
Explanation
_N#*UQ*g<>3M12
g ; List of command-line args
<>3 ; Group into 3-element sublists
UQ* ; Uniquify each
#* ; Get length of each
_N ; Lambda expression: how many times does the argument appear in ^
M12 ; Map that lambda to the arguments 1 and 2
This returns a list containing the number of length-1 uniquified sublists and the number of length-2 uniquified sublists.
Vyxal 14 13 bytes
3ẇƛ∪L;:1O$2Ox
Explanation:
3ẇ # 3-partition
ƛ # Open lambda map
∪L # Length of sublist union
; # Close lambda map
: # Duplicate for next operation
1O # Count 1
$ # Swap
2O # Count 2
x # Print stack as list
Thunno, \$ 12 \log_{256}(96) \approx \$ 9.88 bytes
3ApeZULE12dc
Port of Emigna's 05AB1E answer.
Explanation
3ApeZULE12dc # Implicit input
3Ap # Split into sublists of length 3
e E # For each sublist:
ZU # Remove duplicates
L # And get the length
12dc # Count the number of 1s and 2s in this list
# Implicit output
Desmos, 84 bytes
l(r)=r.length
b=[l(k[3x-2...3x].unique)forx=[1...l(k)/3]]
f(k)=[l(b[b=1]),l(b[b=2])]
Factor + pair-rocket, 58 bytes
[ 3 group 1 => 2 [ '[ cardinality _ = ] count ] with map ]
C# (Visual C# Interactive Compiler), 108 bytes
x=>new[]{1,2}.Select(n=>x.Select((v,i)=>(v,g:i/3)).GroupBy(y=>y.g,y=>y.v).Count(y=>y.Distinct().Count()==n))
Less golfed...
// x is the input list of ints
x=>x
// 1 distinct number means 3/3 are the same
// 2 distinct number means 2/3 are the same
new[]{1,2}
// iterate over the outer array to get an index
.Select(n=>x
// iterate over the whole list with an index
// and break into groups of size 3
.Select((v,i)=>v,g:i/3))
.GroupBy(y=>y.g,y=>y.v)
// count the distinct values in each group
// and get the result based on outer array value
.Count(y=>y.Distinct().Count()==n))