Calculating total slotsOutput a list of all rational numbersGolf some quine stripes in different languagesPrinting the Cracker Barrel GameThe Combinatorics of TransistorImplement the Enigma MachineCalculating dehydration synthesis resultsLeaping Lizards!Fluctuating rangesFinite Cantor's DiagonalRandomly Assign People to Tasks
Pulling the rope with one hand is as heavy as with two hands?
How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?
Is there any limitation with Arduino Nano serial communication distance?
Why isn't the definition of absolute value applied when squaring a radical containing a variable?
Is it possible to determine the symmetric encryption method used by output size?
What is the most expensive material in the world that could be used to create Pun-Pun's lute?
A Strange Latex Symbol
How could Tony Stark make this in Endgame?
How to type a section sign (§) into the Minecraft client
How can I place the product on a social media post better?
Examples of subgroups where it's nontrivial to show closure under multiplication?
Phrase for the opposite of "foolproof"
What makes accurate emulation of old systems a difficult task?
Was there a Viking Exchange as well as a Columbian one?
Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?
How did Captain America manage to do this?
simple conditions equation
How to reduce LED flash rate (frequency)
Does holding a wand and speaking its command word count as V/S/M spell components?
What happened to Captain America in Endgame?
Pass By Reference VS Pass by Value
Please, smoke with good manners
Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?
Do I have an "anti-research" personality?
Calculating total slots
Output a list of all rational numbersGolf some quine stripes in different languagesPrinting the Cracker Barrel GameThe Combinatorics of TransistorImplement the Enigma MachineCalculating dehydration synthesis resultsLeaping Lizards!Fluctuating rangesFinite Cantor's DiagonalRandomly Assign People to Tasks
$begingroup$
Given a list of jobs, which must be done in order, with each taking a slot to do, how long will it take to perform them all if after doing a job the same job cannot be done for the next two slots (cooling off slots)? However, a different job can be assigned in this cooling off slots.
For example,
[9,10,9,8] => output: 5
Because jobs will be allocated as [9 10 _ 9 8]
.
1. First, 9 needs two cooling off spots _ _. So we start with 9 _ _
.
2. Next job 10 is different from the previous job 9, so we can allocate one of _ _. Then we will have 9 10 _
.
3. Third, 9 cannot be allocated now, since first job 9 is the same job and it needs cooling off time. 9 10 _ 9
.
4. Last, 8 is not same as any other previous two jobs, so it can be allocated right after 9 and since this is last job, it does not need cooling off time. Final list is 9 10 _ 9 8
and expected output is 5, which is the number of spots (or number of slots)
Test cases:
[1,2,3,4,5,6,7,8,9,10] => output : 10 ([1 2 3 4 5 6 7 8 9 10])
[1,1,1] => output: 7 ([1 _ _ 1 _ _ 1])
[3,4,4,3] => output: 6 ([3 4 _ _ 4 3])
[3,4,5,3] => output: 4 ([3 4 5 3])
[3,4,3,4] => output : 5 ([3 4 _ 3 4])
[3,3,4,4] => output : 8 ([3 _ _ 3 4 _ _ 4])
[3,3,4,3] => output : 7 ([3 _ _ 3 4 _ 3])
[3,2,1,3,-4] => output : 5 ([3 2 1 3 -4])
[] => output : 0 ([])
[-1,-1] => output : 4 ([-1 _ _ -1])
Input value can be any integer (negative, 0, positive).
Length of job-list is 0 <= length <= 1,000,000.
Output will be an integer, the total number of slots, which is indicated in test case as output. The list inside the parenthesis is how the output would be generated.
Winning criterion
code-golf
code-golf number array-manipulation
$endgroup$
add a comment |
$begingroup$
Given a list of jobs, which must be done in order, with each taking a slot to do, how long will it take to perform them all if after doing a job the same job cannot be done for the next two slots (cooling off slots)? However, a different job can be assigned in this cooling off slots.
For example,
[9,10,9,8] => output: 5
Because jobs will be allocated as [9 10 _ 9 8]
.
1. First, 9 needs two cooling off spots _ _. So we start with 9 _ _
.
2. Next job 10 is different from the previous job 9, so we can allocate one of _ _. Then we will have 9 10 _
.
3. Third, 9 cannot be allocated now, since first job 9 is the same job and it needs cooling off time. 9 10 _ 9
.
4. Last, 8 is not same as any other previous two jobs, so it can be allocated right after 9 and since this is last job, it does not need cooling off time. Final list is 9 10 _ 9 8
and expected output is 5, which is the number of spots (or number of slots)
Test cases:
[1,2,3,4,5,6,7,8,9,10] => output : 10 ([1 2 3 4 5 6 7 8 9 10])
[1,1,1] => output: 7 ([1 _ _ 1 _ _ 1])
[3,4,4,3] => output: 6 ([3 4 _ _ 4 3])
[3,4,5,3] => output: 4 ([3 4 5 3])
[3,4,3,4] => output : 5 ([3 4 _ 3 4])
[3,3,4,4] => output : 8 ([3 _ _ 3 4 _ _ 4])
[3,3,4,3] => output : 7 ([3 _ _ 3 4 _ 3])
[3,2,1,3,-4] => output : 5 ([3 2 1 3 -4])
[] => output : 0 ([])
[-1,-1] => output : 4 ([-1 _ _ -1])
Input value can be any integer (negative, 0, positive).
Length of job-list is 0 <= length <= 1,000,000.
Output will be an integer, the total number of slots, which is indicated in test case as output. The list inside the parenthesis is how the output would be generated.
Winning criterion
code-golf
code-golf number array-manipulation
$endgroup$
$begingroup$
Is it ok if we output nothing instead of 0 for[]
?
$endgroup$
– wastl
Mar 22 at 18:50
8
$begingroup$
Isn’t it a bit early to accept an answer?
$endgroup$
– Nick Kennedy
Mar 22 at 21:10
7
$begingroup$
As @NickKennedy said, that's far, far too soon to be accepting a solution. Some even recommend never accepting a solution.
$endgroup$
– Shaggy
Mar 22 at 23:41
add a comment |
$begingroup$
Given a list of jobs, which must be done in order, with each taking a slot to do, how long will it take to perform them all if after doing a job the same job cannot be done for the next two slots (cooling off slots)? However, a different job can be assigned in this cooling off slots.
For example,
[9,10,9,8] => output: 5
Because jobs will be allocated as [9 10 _ 9 8]
.
1. First, 9 needs two cooling off spots _ _. So we start with 9 _ _
.
2. Next job 10 is different from the previous job 9, so we can allocate one of _ _. Then we will have 9 10 _
.
3. Third, 9 cannot be allocated now, since first job 9 is the same job and it needs cooling off time. 9 10 _ 9
.
4. Last, 8 is not same as any other previous two jobs, so it can be allocated right after 9 and since this is last job, it does not need cooling off time. Final list is 9 10 _ 9 8
and expected output is 5, which is the number of spots (or number of slots)
Test cases:
[1,2,3,4,5,6,7,8,9,10] => output : 10 ([1 2 3 4 5 6 7 8 9 10])
[1,1,1] => output: 7 ([1 _ _ 1 _ _ 1])
[3,4,4,3] => output: 6 ([3 4 _ _ 4 3])
[3,4,5,3] => output: 4 ([3 4 5 3])
[3,4,3,4] => output : 5 ([3 4 _ 3 4])
[3,3,4,4] => output : 8 ([3 _ _ 3 4 _ _ 4])
[3,3,4,3] => output : 7 ([3 _ _ 3 4 _ 3])
[3,2,1,3,-4] => output : 5 ([3 2 1 3 -4])
[] => output : 0 ([])
[-1,-1] => output : 4 ([-1 _ _ -1])
Input value can be any integer (negative, 0, positive).
Length of job-list is 0 <= length <= 1,000,000.
Output will be an integer, the total number of slots, which is indicated in test case as output. The list inside the parenthesis is how the output would be generated.
Winning criterion
code-golf
code-golf number array-manipulation
$endgroup$
Given a list of jobs, which must be done in order, with each taking a slot to do, how long will it take to perform them all if after doing a job the same job cannot be done for the next two slots (cooling off slots)? However, a different job can be assigned in this cooling off slots.
For example,
[9,10,9,8] => output: 5
Because jobs will be allocated as [9 10 _ 9 8]
.
1. First, 9 needs two cooling off spots _ _. So we start with 9 _ _
.
2. Next job 10 is different from the previous job 9, so we can allocate one of _ _. Then we will have 9 10 _
.
3. Third, 9 cannot be allocated now, since first job 9 is the same job and it needs cooling off time. 9 10 _ 9
.
4. Last, 8 is not same as any other previous two jobs, so it can be allocated right after 9 and since this is last job, it does not need cooling off time. Final list is 9 10 _ 9 8
and expected output is 5, which is the number of spots (or number of slots)
Test cases:
[1,2,3,4,5,6,7,8,9,10] => output : 10 ([1 2 3 4 5 6 7 8 9 10])
[1,1,1] => output: 7 ([1 _ _ 1 _ _ 1])
[3,4,4,3] => output: 6 ([3 4 _ _ 4 3])
[3,4,5,3] => output: 4 ([3 4 5 3])
[3,4,3,4] => output : 5 ([3 4 _ 3 4])
[3,3,4,4] => output : 8 ([3 _ _ 3 4 _ _ 4])
[3,3,4,3] => output : 7 ([3 _ _ 3 4 _ 3])
[3,2,1,3,-4] => output : 5 ([3 2 1 3 -4])
[] => output : 0 ([])
[-1,-1] => output : 4 ([-1 _ _ -1])
Input value can be any integer (negative, 0, positive).
Length of job-list is 0 <= length <= 1,000,000.
Output will be an integer, the total number of slots, which is indicated in test case as output. The list inside the parenthesis is how the output would be generated.
Winning criterion
code-golf
code-golf number array-manipulation
code-golf number array-manipulation
edited Mar 22 at 10:16
Kevin Cruijssen
43.8k573223
43.8k573223
asked Mar 22 at 0:46
jayko03jayko03
2948
2948
$begingroup$
Is it ok if we output nothing instead of 0 for[]
?
$endgroup$
– wastl
Mar 22 at 18:50
8
$begingroup$
Isn’t it a bit early to accept an answer?
$endgroup$
– Nick Kennedy
Mar 22 at 21:10
7
$begingroup$
As @NickKennedy said, that's far, far too soon to be accepting a solution. Some even recommend never accepting a solution.
$endgroup$
– Shaggy
Mar 22 at 23:41
add a comment |
$begingroup$
Is it ok if we output nothing instead of 0 for[]
?
$endgroup$
– wastl
Mar 22 at 18:50
8
$begingroup$
Isn’t it a bit early to accept an answer?
$endgroup$
– Nick Kennedy
Mar 22 at 21:10
7
$begingroup$
As @NickKennedy said, that's far, far too soon to be accepting a solution. Some even recommend never accepting a solution.
$endgroup$
– Shaggy
Mar 22 at 23:41
$begingroup$
Is it ok if we output nothing instead of 0 for
[]
?$endgroup$
– wastl
Mar 22 at 18:50
$begingroup$
Is it ok if we output nothing instead of 0 for
[]
?$endgroup$
– wastl
Mar 22 at 18:50
8
8
$begingroup$
Isn’t it a bit early to accept an answer?
$endgroup$
– Nick Kennedy
Mar 22 at 21:10
$begingroup$
Isn’t it a bit early to accept an answer?
$endgroup$
– Nick Kennedy
Mar 22 at 21:10
7
7
$begingroup$
As @NickKennedy said, that's far, far too soon to be accepting a solution. Some even recommend never accepting a solution.
$endgroup$
– Shaggy
Mar 22 at 23:41
$begingroup$
As @NickKennedy said, that's far, far too soon to be accepting a solution. Some even recommend never accepting a solution.
$endgroup$
– Shaggy
Mar 22 at 23:41
add a comment |
26 Answers
26
active
oldest
votes
$begingroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 22 bytes
v¯R¬yQiõˆ}2£yåiˆ}yˆ}¯g
Try it online or verify all test cases.
Explanation:
v # Loop over the integers `y` of the (implicit) input-list:
¯R # Push the global_array, and reverse it
¬ # Get the first item (without popping the reversed global_array itself)
yQi } # If it's equal to the integer `y`:
õˆ # Add an empty string to the global_array
2£ # Then only leave the first 2 items of the reversed global_array
yåi } # If the integer `y` is in these first 2 items:
ˆ # Add the (implicit) input-list to the global_array
yˆ # And push the integer `y` itself to the global_array
}¯g # After the loop: push the global array, and then pop and push its length
# (which is output implicitly as result)
$endgroup$
$begingroup$
What is the global areay? Is it empty on start of the program?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:09
$begingroup$
@EmbodimentofIgnorance Yes, it's a single array to which I can add something, which I can push, and which I can clear. And it indeed starts empty initially.
$endgroup$
– Kevin Cruijssen
Mar 23 at 8:59
add a comment |
$begingroup$
Brachylog, 10 bytes
It's always nice to see problem where Brachylog performs best
⊆Is₃ᶠ≠ᵐ∧Il
Explanation
⊆I # Find the minimal ordered superset of the input (and store in I) where:
s₃ᶠ # each substring of length 3
≠ᵐ # has only distinct numbers
∧Il # and output the length of that superset
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 123 bytes
`-`=nchar;x=scan(,'');while(x!=(y=gsub("([^,]+),(([^,]*,)0,1)\1(,|$)","\1,\2,\1\4",x)))x=y;-gsub("[^,]","",y)+(-y>1)
Try it online - single program!
Try it online - multiple examples!
A full program that reads a comma-separated list of integers as the input, and outputs the slots needed. I’m sure this could be golfed some more, and implementing this regex-based solution in some other languages would be more efficient in bytes.
Note on the second TIO I’ve wrapped it in a function to permit multiple examples to be shown. This function also shows the final list, but this is not output my the main program if run in isolation.
$endgroup$
add a comment |
$begingroup$
TSQL query, 158 bytes
Input data as a table.
The query is recursive so
OPTION(MAXRECURSION 0)
is necessary, because the list of numbers can exceed 100 although it can only go handle 32,767 recursions - is the limitation really needed in this task ?
DECLARE @ table(a int, r int identity(1,1))
INSERT @ VALUES(3),(3),(4),(4);
WITH k as(SELECT null b,null c,1p
UNION ALL
SELECT iif(a in(b,c),null,a),b,p+iif(a in(b,c),0,1)FROM @,k
WHERE p=r)SELECT sum(1)-1FROM k
OPTION(MAXRECURSION 0)
Try it online
$endgroup$
add a comment |
$begingroup$
R, 81 70 bytes
sum(l<-rle(s<-scan())$l*3-3,1-l%/%6,((r=rle(diff(s,2)))$l+1)%/%2*!r$v)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least it works now...
First, we evaluate the lengths of consecutive runs of the same job. E.g. for 3, 3, 4, 3
this gives:
Run Length Encoding
lengths: int [1:3] 2 1 1
values : num [1:3] 3 4 3
Each of these runs produces (len - 1) * 3 + 1
steps (+ 1
is handled separately).
Next, we process occurrences of the same job 2 places apart, like: x, y, x
, by using diff(s, lag=2)
. The resulting vector is also chunked into consecutive runs (r
) by rle
function. Now, because of various interleaved alternations we need to add ceiling(r$len/2)
steps for all runs of zeroes. E.g.:
x y x
(length 1) and x y x y
(length 2) both need 1 extra step: x y _ x (y)
x y x y x
(length 3) and x y x y x y
(length 4) both need 2 extra steps: x y _ x y _ x (y)
Finally, we need to compensate for occurrences of these alternations in the middle of a long run of the same job: x, x, x, x...
, hence 1-l%/%6
instead of simply 1
.
$endgroup$
$begingroup$
I was in the middle of commenting about usingdiff(s,lag=2)
to detect proximity! Now you're a byte shorter than my solution...
$endgroup$
– Giuseppe
Mar 22 at 19:51
$begingroup$
Yeah, not giving up yet :) Now trying to get rid of some parentheses...
$endgroup$
– Kirill L.
Mar 22 at 19:52
add a comment |
$begingroup$
Python 2, 67 bytes
r=[]
for x in input():
while x in r[-2:]:r+=r,
r+=x,
print len(r)
Try it online!
Implements the challenge pretty literally. Uses copies of the list itself as "blanks", since these can't equal any number.
$endgroup$
add a comment |
$begingroup$
Charcoal, 27 23 bytes
Fθ«W№✂υ±²¦¦¦ι⊞υω⊞υι»ILυ
Try it online! Link is to verbose version of code. Explanation:
Fθ«
Loop over the jobs.
W№✂υ±²¦¦¦ι⊞υω
Add cooling off spots while the job is one of the last two in the result.
⊞υι»
Add the current job to the result.
ILυ
Print the number of spots.
$endgroup$
add a comment |
$begingroup$
R, 74 68 bytes
length(Reduce(function(x,y)c(y,rep("",match(y,x[2:1],0)),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter longer than Kirill L.'s answer, so sometimes, the naive approach is pretty good. EDIT: shorter again! I also borrowed Kirill's test template.
-6 bytes replacing max(0,which(y==x[2:1]))
with match(y,x,0)
.
$endgroup$
$begingroup$
@Giuspeppe what does thec
function do?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 22:50
$begingroup$
@EmbodimentofIgnorance --c
stands forcombine
, althoughconcatenate
might be better; it combines its arguments into a single list.
$endgroup$
– Giuseppe
Mar 22 at 23:01
$begingroup$
Thanks, I thought it was weird that a language not designed for golfing would have one letter functions
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:04
add a comment |
$begingroup$
Perl 6, 98 bytes
$_ Z$_ Z .[1..*+1])>>.repeated.squish(:with($+^=[*] $! ne$^a ne$^b,$b==($!=$a))).sum+$_
Try it online!
Blergh, there's got to be a better way of doing this. I'm not 100% sure this is fully correct, though it passes all the edge cases I could think of.
Basically, this starts by grouping all the triplets of the input list, with padding to either side. For example, [1,2,1,2]
becomes (Any,1,2), (1,2,1), (2,1,2), (1,2,Nil)
. We get the repeated
elements in each triplet, becoming (), (1), (2), ()
.
It then squish
es consecutive elements that are not the same list, but are the same size (to not squish something like [1,1,1]
), and the first element is not equal to the element before it (because we can't merge the hours in [1,1,2,2]
), and finally the element before hasn't also been squished ([1,2,1,2,1,2]
). So (1), (2)
in the above example above would be squished together.
Finally, we get the sum
of all lengths of this list, which represent our inserted hours, and add the length of the original list.
For example:
(1,1,1) => (Any,1,1),(1,1,1),(1,1,Nil) => (1),(1,1),(1) => (no squishes) => 4+3 = 7
(1,2,1,2,1,2) => (Any,1,2), (1,2,1), (2,1,2), (1,2,1), (2,1,2), (1,2,Nil) => (),(1),(2),(1),(2),() => squish (1),(2) and (1),(2) => 2+6 = 8
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 57 bytes
f=([x,...a],p,q)=>1/x?1+f(x!=p&x!=q?a:[x,...a,x=f],x,p):0
Try it online!
Commented
f = ( // f is a recursive function taking:
[x, // x = next job
...a], // a[] = array of remaining jobs
p, // p = previous job, initially undefined
q // q = penultimate job, initially undefined
) => //
1 / x ? // if x is defined and numeric:
1 + // add 1 to the grand total
f( // and do a recursive call to f:
x != p & // if x is different from the previous job
x != q ? // and different from the penultimate job:
a // just pass the remaining jobs
: // else:
[ x, // pass x, which can't be assigned yet
...a, // pass the remaining jobs
x = f // set x to a non-numeric value
], //
x, // previous job = x
p // penultimate job = previous job
) // end of recursive call
: // else:
0 // stop recursion
$endgroup$
add a comment |
$begingroup$
C (gcc), 69 bytes
f(j,l)int*j;j=l>1?(*j-*++j?j[-1]==j[l>2]?j++,l--,3:1:3)+f(j,l-1):l;
Try it online!
Straightforward recursion.
f(j,l)int*j; //Jobs, (array) Length
j=l>1 //if l > 1, do a recursion:
? (*j-*++j // check if first and second elements are equal (j++)
? j[-1]== // 1st!=2nd; check if first and third are equal
j[l>2] // (first and second if l==2, but we already know 1st!=2nd)
? j++,l--,3 // 1st==3rd (j++,l--) return 3+f(j+2,l-2)
: 1 // 1st!=3rd (or l==2) return 1+f(j+1,l-1)
: 3 // 1st==2nd return 3+f(j+1,l-1)
)+f(j,l-1) // j and l were modified as needed
: l; // nothing more needed return l
$endgroup$
add a comment |
$begingroup$
Perl 6, 48 bytes
$!=0;.map:$_=($!=$!+1 max$_)+3((%)$_);$!
Try it online!
45 bytes if the list has at least two elements:
+*.reduce:(*xx 3-(
Try it online!
$endgroup$
add a comment |
$begingroup$
Smalltalk, 125 bytes
c:=0.n:=q size.1to:n-2do:[:i|(j:=q at:i)=(k:=q at:i+1)ifTrue:[c:=c+2].j=(m:=q at:i+2)ifTrue:[c:=c+1]].k=m ifTrue:[c:=c+1].c+n
Explanation
c : accumulator of proximity penalty
q : input array.
n := q length
i : iteration index from 1 to: n-2 (arrays are 1-based in Smalltalk).
j := memory for element i, saves some few bytes when reused
k := similar to j but for i+1.
m := similar to k but for i+2.
$endgroup$
$begingroup$
Isn't this a snippet?
$endgroup$
– attinat
Mar 28 at 22:43
add a comment |
$begingroup$
Perl 5 -pl
, 42 40 bytes
$a$_=~s/.*/$=$&if++$<$&;$+3/e}improve this answer
$endgroup$
$begingroup$
Cut it down to 35 using-p
and reworking the substitution: Try it online!
$endgroup$
– Xcali
Mar 22 at 21:09
$begingroup$
@Xcali That gives nothing for empty input, but I got to 39
$endgroup$
– wastl
Mar 22 at 21:31
1
$begingroup$
Doesn't seem to work for 1,1,1.
$endgroup$
– nwellnhof
Mar 23 at 15:01
$begingroup$
@nwellnhof Fixed
$endgroup$
– wastl
Mar 24 at 22:23
add a comment
Try it online!
Unpacked code looks as follows:
function f(a)
var c = 0;
for (var i = 0; i < a.length; i++, c++)
if (a[i - 1] == a[i])
c+=2;
else if (a[i - 2] == a[i])
c++,a[i-1]=undefined;
return c;
My first ever code-golf attempt, can probably be optimized a lot by shrinking the array and passing it recursively.
$endgroup$
$begingroup$
Welcome to PPCG! This is a pretty great first post!
$endgroup$
– Rɪᴋᴇʀ
Mar 25 at 23:45
add a comment |
$begingroup$
Zsh, 66 60 bytes
-6 bytes from implicit "$@"
for j
((i=$a[(I)$j]))&&a=
a=("$a[-1]" $j)
((x+=i+1))
<<<$x
Try it online! I highly recommend adding set -x
to the start so you can follow along.
for j # Implicit "$@"
# Use '' '' instead of 'do' 'done'
(( i=$a[(I)$j] )) # (see below)
&& a= # if the previous returned true, empty a
a=( "$a[-1]" $j ) # set the array to its last element and the new job
(( x += i + 1 )) # add number of slots we advanced
<<<$x # echo back our total
((i=$a[(I)$j]))
$a[ ] # Array lookup
(I)$j # Get highest index matched by $j, or 0 if not found
i= # Set to i
(( )) # If i was set nonzero, return true
a
always contains the last two jobs, so if the lookup finds a matching job in a[2]
, we increment by three (since the job slots will be [... 3 _ _ 3 ...]
).
If a
is unset, the lookup will fail and the arithmetic expansion will return an error, but that only happens on the first job and isn't fatal.
We can save one more byte if we use $[x+=i+1]
instead, and there are no commands on the users system comprised entirely of digits.
$endgroup$
add a comment |
$begingroup$
K (ngn/k), 27 bytes
#2_``2-x?y;`],x/x
Try it online!
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182010%2fcalculating-total-slots%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
26 Answers
26
active
oldest
votes
26 Answers
26
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
$endgroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
answered Mar 22 at 18:19
Erik the OutgolferErik the Outgolfer
33.3k429106
33.3k429106
add a comment |
add a comment |
$begingroup$
05AB1E, 22 bytes
v¯R¬yQiõˆ}2£yåiˆ}yˆ}¯g
Try it online or verify all test cases.
Explanation:
v # Loop over the integers `y` of the (implicit) input-list:
¯R # Push the global_array, and reverse it
¬ # Get the first item (without popping the reversed global_array itself)
yQi } # If it's equal to the integer `y`:
õˆ # Add an empty string to the global_array
2£ # Then only leave the first 2 items of the reversed global_array
yåi } # If the integer `y` is in these first 2 items:
ˆ # Add the (implicit) input-list to the global_array
yˆ # And push the integer `y` itself to the global_array
}¯g # After the loop: push the global array, and then pop and push its length
# (which is output implicitly as result)
$endgroup$
$begingroup$
What is the global areay? Is it empty on start of the program?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:09
$begingroup$
@EmbodimentofIgnorance Yes, it's a single array to which I can add something, which I can push, and which I can clear. And it indeed starts empty initially.
$endgroup$
– Kevin Cruijssen
Mar 23 at 8:59
add a comment |
$begingroup$
05AB1E, 22 bytes
v¯R¬yQiõˆ}2£yåiˆ}yˆ}¯g
Try it online or verify all test cases.
Explanation:
v # Loop over the integers `y` of the (implicit) input-list:
¯R # Push the global_array, and reverse it
¬ # Get the first item (without popping the reversed global_array itself)
yQi } # If it's equal to the integer `y`:
õˆ # Add an empty string to the global_array
2£ # Then only leave the first 2 items of the reversed global_array
yåi } # If the integer `y` is in these first 2 items:
ˆ # Add the (implicit) input-list to the global_array
yˆ # And push the integer `y` itself to the global_array
}¯g # After the loop: push the global array, and then pop and push its length
# (which is output implicitly as result)
$endgroup$
$begingroup$
What is the global areay? Is it empty on start of the program?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:09
$begingroup$
@EmbodimentofIgnorance Yes, it's a single array to which I can add something, which I can push, and which I can clear. And it indeed starts empty initially.
$endgroup$
– Kevin Cruijssen
Mar 23 at 8:59
add a comment |
$begingroup$
05AB1E, 22 bytes
v¯R¬yQiõˆ}2£yåiˆ}yˆ}¯g
Try it online or verify all test cases.
Explanation:
v # Loop over the integers `y` of the (implicit) input-list:
¯R # Push the global_array, and reverse it
¬ # Get the first item (without popping the reversed global_array itself)
yQi } # If it's equal to the integer `y`:
õˆ # Add an empty string to the global_array
2£ # Then only leave the first 2 items of the reversed global_array
yåi } # If the integer `y` is in these first 2 items:
ˆ # Add the (implicit) input-list to the global_array
yˆ # And push the integer `y` itself to the global_array
}¯g # After the loop: push the global array, and then pop and push its length
# (which is output implicitly as result)
$endgroup$
05AB1E, 22 bytes
v¯R¬yQiõˆ}2£yåiˆ}yˆ}¯g
Try it online or verify all test cases.
Explanation:
v # Loop over the integers `y` of the (implicit) input-list:
¯R # Push the global_array, and reverse it
¬ # Get the first item (without popping the reversed global_array itself)
yQi } # If it's equal to the integer `y`:
õˆ # Add an empty string to the global_array
2£ # Then only leave the first 2 items of the reversed global_array
yåi } # If the integer `y` is in these first 2 items:
ˆ # Add the (implicit) input-list to the global_array
yˆ # And push the integer `y` itself to the global_array
}¯g # After the loop: push the global array, and then pop and push its length
# (which is output implicitly as result)
edited Mar 22 at 11:59
answered Mar 22 at 11:05
Kevin CruijssenKevin Cruijssen
43.8k573223
43.8k573223
$begingroup$
What is the global areay? Is it empty on start of the program?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:09
$begingroup$
@EmbodimentofIgnorance Yes, it's a single array to which I can add something, which I can push, and which I can clear. And it indeed starts empty initially.
$endgroup$
– Kevin Cruijssen
Mar 23 at 8:59
add a comment |
$begingroup$
What is the global areay? Is it empty on start of the program?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:09
$begingroup$
@EmbodimentofIgnorance Yes, it's a single array to which I can add something, which I can push, and which I can clear. And it indeed starts empty initially.
$endgroup$
– Kevin Cruijssen
Mar 23 at 8:59
$begingroup$
What is the global areay? Is it empty on start of the program?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:09
$begingroup$
What is the global areay? Is it empty on start of the program?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:09
$begingroup$
@EmbodimentofIgnorance Yes, it's a single array to which I can add something, which I can push, and which I can clear. And it indeed starts empty initially.
$endgroup$
– Kevin Cruijssen
Mar 23 at 8:59
$begingroup$
@EmbodimentofIgnorance Yes, it's a single array to which I can add something, which I can push, and which I can clear. And it indeed starts empty initially.
$endgroup$
– Kevin Cruijssen
Mar 23 at 8:59
add a comment |
$begingroup$
Brachylog, 10 bytes
It's always nice to see problem where Brachylog performs best
⊆Is₃ᶠ≠ᵐ∧Il
Explanation
⊆I # Find the minimal ordered superset of the input (and store in I) where:
s₃ᶠ # each substring of length 3
≠ᵐ # has only distinct numbers
∧Il # and output the length of that superset
Try it online!
$endgroup$
add a comment |
$begingroup$
Brachylog, 10 bytes
It's always nice to see problem where Brachylog performs best
⊆Is₃ᶠ≠ᵐ∧Il
Explanation
⊆I # Find the minimal ordered superset of the input (and store in I) where:
s₃ᶠ # each substring of length 3
≠ᵐ # has only distinct numbers
∧Il # and output the length of that superset
Try it online!
$endgroup$
add a comment |
$begingroup$
Brachylog, 10 bytes
It's always nice to see problem where Brachylog performs best
⊆Is₃ᶠ≠ᵐ∧Il
Explanation
⊆I # Find the minimal ordered superset of the input (and store in I) where:
s₃ᶠ # each substring of length 3
≠ᵐ # has only distinct numbers
∧Il # and output the length of that superset
Try it online!
$endgroup$
Brachylog, 10 bytes
It's always nice to see problem where Brachylog performs best
⊆Is₃ᶠ≠ᵐ∧Il
Explanation
⊆I # Find the minimal ordered superset of the input (and store in I) where:
s₃ᶠ # each substring of length 3
≠ᵐ # has only distinct numbers
∧Il # and output the length of that superset
Try it online!
answered Mar 24 at 12:56
KroppebKroppeb
1,406310
1,406310
add a comment |
add a comment |
$begingroup$
R, 123 bytes
`-`=nchar;x=scan(,'');while(x!=(y=gsub("([^,]+),(([^,]*,)0,1)\1(,|$)","\1,\2,\1\4",x)))x=y;-gsub("[^,]","",y)+(-y>1)
Try it online - single program!
Try it online - multiple examples!
A full program that reads a comma-separated list of integers as the input, and outputs the slots needed. I’m sure this could be golfed some more, and implementing this regex-based solution in some other languages would be more efficient in bytes.
Note on the second TIO I’ve wrapped it in a function to permit multiple examples to be shown. This function also shows the final list, but this is not output my the main program if run in isolation.
$endgroup$
add a comment |
$begingroup$
R, 123 bytes
`-`=nchar;x=scan(,'');while(x!=(y=gsub("([^,]+),(([^,]*,)0,1)\1(,|$)","\1,\2,\1\4",x)))x=y;-gsub("[^,]","",y)+(-y>1)
Try it online - single program!
Try it online - multiple examples!
A full program that reads a comma-separated list of integers as the input, and outputs the slots needed. I’m sure this could be golfed some more, and implementing this regex-based solution in some other languages would be more efficient in bytes.
Note on the second TIO I’ve wrapped it in a function to permit multiple examples to be shown. This function also shows the final list, but this is not output my the main program if run in isolation.
$endgroup$
add a comment |
$begingroup$
R, 123 bytes
`-`=nchar;x=scan(,'');while(x!=(y=gsub("([^,]+),(([^,]*,)0,1)\1(,|$)","\1,\2,\1\4",x)))x=y;-gsub("[^,]","",y)+(-y>1)
Try it online - single program!
Try it online - multiple examples!
A full program that reads a comma-separated list of integers as the input, and outputs the slots needed. I’m sure this could be golfed some more, and implementing this regex-based solution in some other languages would be more efficient in bytes.
Note on the second TIO I’ve wrapped it in a function to permit multiple examples to be shown. This function also shows the final list, but this is not output my the main program if run in isolation.
$endgroup$
R, 123 bytes
`-`=nchar;x=scan(,'');while(x!=(y=gsub("([^,]+),(([^,]*,)0,1)\1(,|$)","\1,\2,\1\4",x)))x=y;-gsub("[^,]","",y)+(-y>1)
Try it online - single program!
Try it online - multiple examples!
A full program that reads a comma-separated list of integers as the input, and outputs the slots needed. I’m sure this could be golfed some more, and implementing this regex-based solution in some other languages would be more efficient in bytes.
Note on the second TIO I’ve wrapped it in a function to permit multiple examples to be shown. This function also shows the final list, but this is not output my the main program if run in isolation.
edited Mar 22 at 10:22
answered Mar 22 at 9:58
Nick KennedyNick Kennedy
1,92159
1,92159
add a comment |
add a comment |
$begingroup$
TSQL query, 158 bytes
Input data as a table.
The query is recursive so
OPTION(MAXRECURSION 0)
is necessary, because the list of numbers can exceed 100 although it can only go handle 32,767 recursions - is the limitation really needed in this task ?
DECLARE @ table(a int, r int identity(1,1))
INSERT @ VALUES(3),(3),(4),(4);
WITH k as(SELECT null b,null c,1p
UNION ALL
SELECT iif(a in(b,c),null,a),b,p+iif(a in(b,c),0,1)FROM @,k
WHERE p=r)SELECT sum(1)-1FROM k
OPTION(MAXRECURSION 0)
Try it online
$endgroup$
add a comment |
$begingroup$
TSQL query, 158 bytes
Input data as a table.
The query is recursive so
OPTION(MAXRECURSION 0)
is necessary, because the list of numbers can exceed 100 although it can only go handle 32,767 recursions - is the limitation really needed in this task ?
DECLARE @ table(a int, r int identity(1,1))
INSERT @ VALUES(3),(3),(4),(4);
WITH k as(SELECT null b,null c,1p
UNION ALL
SELECT iif(a in(b,c),null,a),b,p+iif(a in(b,c),0,1)FROM @,k
WHERE p=r)SELECT sum(1)-1FROM k
OPTION(MAXRECURSION 0)
Try it online
$endgroup$
add a comment |
$begingroup$
TSQL query, 158 bytes
Input data as a table.
The query is recursive so
OPTION(MAXRECURSION 0)
is necessary, because the list of numbers can exceed 100 although it can only go handle 32,767 recursions - is the limitation really needed in this task ?
DECLARE @ table(a int, r int identity(1,1))
INSERT @ VALUES(3),(3),(4),(4);
WITH k as(SELECT null b,null c,1p
UNION ALL
SELECT iif(a in(b,c),null,a),b,p+iif(a in(b,c),0,1)FROM @,k
WHERE p=r)SELECT sum(1)-1FROM k
OPTION(MAXRECURSION 0)
Try it online
$endgroup$
TSQL query, 158 bytes
Input data as a table.
The query is recursive so
OPTION(MAXRECURSION 0)
is necessary, because the list of numbers can exceed 100 although it can only go handle 32,767 recursions - is the limitation really needed in this task ?
DECLARE @ table(a int, r int identity(1,1))
INSERT @ VALUES(3),(3),(4),(4);
WITH k as(SELECT null b,null c,1p
UNION ALL
SELECT iif(a in(b,c),null,a),b,p+iif(a in(b,c),0,1)FROM @,k
WHERE p=r)SELECT sum(1)-1FROM k
OPTION(MAXRECURSION 0)
Try it online
edited Mar 22 at 12:41
answered Mar 22 at 12:34
t-clausen.dkt-clausen.dk
2,164514
2,164514
add a comment |
add a comment |
$begingroup$
R, 81 70 bytes
sum(l<-rle(s<-scan())$l*3-3,1-l%/%6,((r=rle(diff(s,2)))$l+1)%/%2*!r$v)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least it works now...
First, we evaluate the lengths of consecutive runs of the same job. E.g. for 3, 3, 4, 3
this gives:
Run Length Encoding
lengths: int [1:3] 2 1 1
values : num [1:3] 3 4 3
Each of these runs produces (len - 1) * 3 + 1
steps (+ 1
is handled separately).
Next, we process occurrences of the same job 2 places apart, like: x, y, x
, by using diff(s, lag=2)
. The resulting vector is also chunked into consecutive runs (r
) by rle
function. Now, because of various interleaved alternations we need to add ceiling(r$len/2)
steps for all runs of zeroes. E.g.:
x y x
(length 1) and x y x y
(length 2) both need 1 extra step: x y _ x (y)
x y x y x
(length 3) and x y x y x y
(length 4) both need 2 extra steps: x y _ x y _ x (y)
Finally, we need to compensate for occurrences of these alternations in the middle of a long run of the same job: x, x, x, x...
, hence 1-l%/%6
instead of simply 1
.
$endgroup$
$begingroup$
I was in the middle of commenting about usingdiff(s,lag=2)
to detect proximity! Now you're a byte shorter than my solution...
$endgroup$
– Giuseppe
Mar 22 at 19:51
$begingroup$
Yeah, not giving up yet :) Now trying to get rid of some parentheses...
$endgroup$
– Kirill L.
Mar 22 at 19:52
add a comment |
$begingroup$
R, 81 70 bytes
sum(l<-rle(s<-scan())$l*3-3,1-l%/%6,((r=rle(diff(s,2)))$l+1)%/%2*!r$v)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least it works now...
First, we evaluate the lengths of consecutive runs of the same job. E.g. for 3, 3, 4, 3
this gives:
Run Length Encoding
lengths: int [1:3] 2 1 1
values : num [1:3] 3 4 3
Each of these runs produces (len - 1) * 3 + 1
steps (+ 1
is handled separately).
Next, we process occurrences of the same job 2 places apart, like: x, y, x
, by using diff(s, lag=2)
. The resulting vector is also chunked into consecutive runs (r
) by rle
function. Now, because of various interleaved alternations we need to add ceiling(r$len/2)
steps for all runs of zeroes. E.g.:
x y x
(length 1) and x y x y
(length 2) both need 1 extra step: x y _ x (y)
x y x y x
(length 3) and x y x y x y
(length 4) both need 2 extra steps: x y _ x y _ x (y)
Finally, we need to compensate for occurrences of these alternations in the middle of a long run of the same job: x, x, x, x...
, hence 1-l%/%6
instead of simply 1
.
$endgroup$
$begingroup$
I was in the middle of commenting about usingdiff(s,lag=2)
to detect proximity! Now you're a byte shorter than my solution...
$endgroup$
– Giuseppe
Mar 22 at 19:51
$begingroup$
Yeah, not giving up yet :) Now trying to get rid of some parentheses...
$endgroup$
– Kirill L.
Mar 22 at 19:52
add a comment |
$begingroup$
R, 81 70 bytes
sum(l<-rle(s<-scan())$l*3-3,1-l%/%6,((r=rle(diff(s,2)))$l+1)%/%2*!r$v)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least it works now...
First, we evaluate the lengths of consecutive runs of the same job. E.g. for 3, 3, 4, 3
this gives:
Run Length Encoding
lengths: int [1:3] 2 1 1
values : num [1:3] 3 4 3
Each of these runs produces (len - 1) * 3 + 1
steps (+ 1
is handled separately).
Next, we process occurrences of the same job 2 places apart, like: x, y, x
, by using diff(s, lag=2)
. The resulting vector is also chunked into consecutive runs (r
) by rle
function. Now, because of various interleaved alternations we need to add ceiling(r$len/2)
steps for all runs of zeroes. E.g.:
x y x
(length 1) and x y x y
(length 2) both need 1 extra step: x y _ x (y)
x y x y x
(length 3) and x y x y x y
(length 4) both need 2 extra steps: x y _ x y _ x (y)
Finally, we need to compensate for occurrences of these alternations in the middle of a long run of the same job: x, x, x, x...
, hence 1-l%/%6
instead of simply 1
.
$endgroup$
R, 81 70 bytes
sum(l<-rle(s<-scan())$l*3-3,1-l%/%6,((r=rle(diff(s,2)))$l+1)%/%2*!r$v)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least it works now...
First, we evaluate the lengths of consecutive runs of the same job. E.g. for 3, 3, 4, 3
this gives:
Run Length Encoding
lengths: int [1:3] 2 1 1
values : num [1:3] 3 4 3
Each of these runs produces (len - 1) * 3 + 1
steps (+ 1
is handled separately).
Next, we process occurrences of the same job 2 places apart, like: x, y, x
, by using diff(s, lag=2)
. The resulting vector is also chunked into consecutive runs (r
) by rle
function. Now, because of various interleaved alternations we need to add ceiling(r$len/2)
steps for all runs of zeroes. E.g.:
x y x
(length 1) and x y x y
(length 2) both need 1 extra step: x y _ x (y)
x y x y x
(length 3) and x y x y x y
(length 4) both need 2 extra steps: x y _ x y _ x (y)
Finally, we need to compensate for occurrences of these alternations in the middle of a long run of the same job: x, x, x, x...
, hence 1-l%/%6
instead of simply 1
.
edited Mar 22 at 20:03
answered Mar 22 at 11:01
Kirill L.Kirill L.
6,3581529
6,3581529
$begingroup$
I was in the middle of commenting about usingdiff(s,lag=2)
to detect proximity! Now you're a byte shorter than my solution...
$endgroup$
– Giuseppe
Mar 22 at 19:51
$begingroup$
Yeah, not giving up yet :) Now trying to get rid of some parentheses...
$endgroup$
– Kirill L.
Mar 22 at 19:52
add a comment |
$begingroup$
I was in the middle of commenting about usingdiff(s,lag=2)
to detect proximity! Now you're a byte shorter than my solution...
$endgroup$
– Giuseppe
Mar 22 at 19:51
$begingroup$
Yeah, not giving up yet :) Now trying to get rid of some parentheses...
$endgroup$
– Kirill L.
Mar 22 at 19:52
$begingroup$
I was in the middle of commenting about using
diff(s,lag=2)
to detect proximity! Now you're a byte shorter than my solution...$endgroup$
– Giuseppe
Mar 22 at 19:51
$begingroup$
I was in the middle of commenting about using
diff(s,lag=2)
to detect proximity! Now you're a byte shorter than my solution...$endgroup$
– Giuseppe
Mar 22 at 19:51
$begingroup$
Yeah, not giving up yet :) Now trying to get rid of some parentheses...
$endgroup$
– Kirill L.
Mar 22 at 19:52
$begingroup$
Yeah, not giving up yet :) Now trying to get rid of some parentheses...
$endgroup$
– Kirill L.
Mar 22 at 19:52
add a comment |
$begingroup$
Python 2, 67 bytes
r=[]
for x in input():
while x in r[-2:]:r+=r,
r+=x,
print len(r)
Try it online!
Implements the challenge pretty literally. Uses copies of the list itself as "blanks", since these can't equal any number.
$endgroup$
add a comment |
$begingroup$
Python 2, 67 bytes
r=[]
for x in input():
while x in r[-2:]:r+=r,
r+=x,
print len(r)
Try it online!
Implements the challenge pretty literally. Uses copies of the list itself as "blanks", since these can't equal any number.
$endgroup$
add a comment |
$begingroup$
Python 2, 67 bytes
r=[]
for x in input():
while x in r[-2:]:r+=r,
r+=x,
print len(r)
Try it online!
Implements the challenge pretty literally. Uses copies of the list itself as "blanks", since these can't equal any number.
$endgroup$
Python 2, 67 bytes
r=[]
for x in input():
while x in r[-2:]:r+=r,
r+=x,
print len(r)
Try it online!
Implements the challenge pretty literally. Uses copies of the list itself as "blanks", since these can't equal any number.
answered Mar 23 at 3:47
xnorxnor
94.7k18195454
94.7k18195454
add a comment |
add a comment |
$begingroup$
Charcoal, 27 23 bytes
Fθ«W№✂υ±²¦¦¦ι⊞υω⊞υι»ILυ
Try it online! Link is to verbose version of code. Explanation:
Fθ«
Loop over the jobs.
W№✂υ±²¦¦¦ι⊞υω
Add cooling off spots while the job is one of the last two in the result.
⊞υι»
Add the current job to the result.
ILυ
Print the number of spots.
$endgroup$
add a comment |
$begingroup$
Charcoal, 27 23 bytes
Fθ«W№✂υ±²¦¦¦ι⊞υω⊞υι»ILυ
Try it online! Link is to verbose version of code. Explanation:
Fθ«
Loop over the jobs.
W№✂υ±²¦¦¦ι⊞υω
Add cooling off spots while the job is one of the last two in the result.
⊞υι»
Add the current job to the result.
ILυ
Print the number of spots.
$endgroup$
add a comment |
$begingroup$
Charcoal, 27 23 bytes
Fθ«W№✂υ±²¦¦¦ι⊞υω⊞υι»ILυ
Try it online! Link is to verbose version of code. Explanation:
Fθ«
Loop over the jobs.
W№✂υ±²¦¦¦ι⊞υω
Add cooling off spots while the job is one of the last two in the result.
⊞υι»
Add the current job to the result.
ILυ
Print the number of spots.
$endgroup$
Charcoal, 27 23 bytes
Fθ«W№✂υ±²¦¦¦ι⊞υω⊞υι»ILυ
Try it online! Link is to verbose version of code. Explanation:
Fθ«
Loop over the jobs.
W№✂υ±²¦¦¦ι⊞υω
Add cooling off spots while the job is one of the last two in the result.
⊞υι»
Add the current job to the result.
ILυ
Print the number of spots.
edited Mar 23 at 11:30
answered Mar 22 at 10:30
NeilNeil
83.3k745179
83.3k745179
add a comment |
add a comment |
$begingroup$
R, 74 68 bytes
length(Reduce(function(x,y)c(y,rep("",match(y,x[2:1],0)),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter longer than Kirill L.'s answer, so sometimes, the naive approach is pretty good. EDIT: shorter again! I also borrowed Kirill's test template.
-6 bytes replacing max(0,which(y==x[2:1]))
with match(y,x,0)
.
$endgroup$
$begingroup$
@Giuspeppe what does thec
function do?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 22:50
$begingroup$
@EmbodimentofIgnorance --c
stands forcombine
, althoughconcatenate
might be better; it combines its arguments into a single list.
$endgroup$
– Giuseppe
Mar 22 at 23:01
$begingroup$
Thanks, I thought it was weird that a language not designed for golfing would have one letter functions
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:04
add a comment |
$begingroup$
R, 74 68 bytes
length(Reduce(function(x,y)c(y,rep("",match(y,x[2:1],0)),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter longer than Kirill L.'s answer, so sometimes, the naive approach is pretty good. EDIT: shorter again! I also borrowed Kirill's test template.
-6 bytes replacing max(0,which(y==x[2:1]))
with match(y,x,0)
.
$endgroup$
$begingroup$
@Giuspeppe what does thec
function do?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 22:50
$begingroup$
@EmbodimentofIgnorance --c
stands forcombine
, althoughconcatenate
might be better; it combines its arguments into a single list.
$endgroup$
– Giuseppe
Mar 22 at 23:01
$begingroup$
Thanks, I thought it was weird that a language not designed for golfing would have one letter functions
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:04
add a comment |
$begingroup$
R, 74 68 bytes
length(Reduce(function(x,y)c(y,rep("",match(y,x[2:1],0)),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter longer than Kirill L.'s answer, so sometimes, the naive approach is pretty good. EDIT: shorter again! I also borrowed Kirill's test template.
-6 bytes replacing max(0,which(y==x[2:1]))
with match(y,x,0)
.
$endgroup$
R, 74 68 bytes
length(Reduce(function(x,y)c(y,rep("",match(y,x[2:1],0)),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter longer than Kirill L.'s answer, so sometimes, the naive approach is pretty good. EDIT: shorter again! I also borrowed Kirill's test template.
-6 bytes replacing max(0,which(y==x[2:1]))
with match(y,x,0)
.
edited Mar 25 at 18:51
answered Mar 22 at 19:05
GiuseppeGiuseppe
18k31155
18k31155
$begingroup$
@Giuspeppe what does thec
function do?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 22:50
$begingroup$
@EmbodimentofIgnorance --c
stands forcombine
, althoughconcatenate
might be better; it combines its arguments into a single list.
$endgroup$
– Giuseppe
Mar 22 at 23:01
$begingroup$
Thanks, I thought it was weird that a language not designed for golfing would have one letter functions
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:04
add a comment |
$begingroup$
@Giuspeppe what does thec
function do?
$endgroup$
– Embodiment of Ignorance
Mar 22 at 22:50
$begingroup$
@EmbodimentofIgnorance --c
stands forcombine
, althoughconcatenate
might be better; it combines its arguments into a single list.
$endgroup$
– Giuseppe
Mar 22 at 23:01
$begingroup$
Thanks, I thought it was weird that a language not designed for golfing would have one letter functions
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:04
$begingroup$
@Giuspeppe what does the
c
function do?$endgroup$
– Embodiment of Ignorance
Mar 22 at 22:50
$begingroup$
@Giuspeppe what does the
c
function do?$endgroup$
– Embodiment of Ignorance
Mar 22 at 22:50
$begingroup$
@EmbodimentofIgnorance --
c
stands for combine
, although concatenate
might be better; it combines its arguments into a single list.$endgroup$
– Giuseppe
Mar 22 at 23:01
$begingroup$
@EmbodimentofIgnorance --
c
stands for combine
, although concatenate
might be better; it combines its arguments into a single list.$endgroup$
– Giuseppe
Mar 22 at 23:01
$begingroup$
Thanks, I thought it was weird that a language not designed for golfing would have one letter functions
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:04
$begingroup$
Thanks, I thought it was weird that a language not designed for golfing would have one letter functions
$endgroup$
– Embodiment of Ignorance
Mar 22 at 23:04
add a comment |
$begingroup$
Perl 6, 98 bytes
$_ Z$_ Z .[1..*+1])>>.repeated.squish(:with($+^=[*] $! ne$^a ne$^b,$b==($!=$a))).sum+$_
Try it online!
Blergh, there's got to be a better way of doing this. I'm not 100% sure this is fully correct, though it passes all the edge cases I could think of.
Basically, this starts by grouping all the triplets of the input list, with padding to either side. For example, [1,2,1,2]
becomes (Any,1,2), (1,2,1), (2,1,2), (1,2,Nil)
. We get the repeated
elements in each triplet, becoming (), (1), (2), ()
.
It then squish
es consecutive elements that are not the same list, but are the same size (to not squish something like [1,1,1]
), and the first element is not equal to the element before it (because we can't merge the hours in [1,1,2,2]
), and finally the element before hasn't also been squished ([1,2,1,2,1,2]
). So (1), (2)
in the above example above would be squished together.
Finally, we get the sum
of all lengths of this list, which represent our inserted hours, and add the length of the original list.
For example:
(1,1,1) => (Any,1,1),(1,1,1),(1,1,Nil) => (1),(1,1),(1) => (no squishes) => 4+3 = 7
(1,2,1,2,1,2) => (Any,1,2), (1,2,1), (2,1,2), (1,2,1), (2,1,2), (1,2,Nil) => (),(1),(2),(1),(2),() => squish (1),(2) and (1),(2) => 2+6 = 8
$endgroup$
add a comment |
$begingroup$
Perl 6, 98 bytes
$_ Z$_ Z .[1..*+1])>>.repeated.squish(:with($+^=[*] $! ne$^a ne$^b,$b==($!=$a))).sum+$_
Try it online!
Blergh, there's got to be a better way of doing this. I'm not 100% sure this is fully correct, though it passes all the edge cases I could think of.
Basically, this starts by grouping all the triplets of the input list, with padding to either side. For example, [1,2,1,2]
becomes (Any,1,2), (1,2,1), (2,1,2), (1,2,Nil)
. We get the repeated
elements in each triplet, becoming (), (1), (2), ()
.
It then squish
es consecutive elements that are not the same list, but are the same size (to not squish something like [1,1,1]
), and the first element is not equal to the element before it (because we can't merge the hours in [1,1,2,2]
), and finally the element before hasn't also been squished ([1,2,1,2,1,2]
). So (1), (2)
in the above example above would be squished together.
Finally, we get the sum
of all lengths of this list, which represent our inserted hours, and add the length of the original list.
For example:
(1,1,1) => (Any,1,1),(1,1,1),(1,1,Nil) => (1),(1,1),(1) => (no squishes) => 4+3 = 7
(1,2,1,2,1,2) => (Any,1,2), (1,2,1), (2,1,2), (1,2,1), (2,1,2), (1,2,Nil) => (),(1),(2),(1),(2),() => squish (1),(2) and (1),(2) => 2+6 = 8
$endgroup$
add a comment |
$begingroup$
Perl 6, 98 bytes
$_ Z$_ Z .[1..*+1])>>.repeated.squish(:with($+^=[*] $! ne$^a ne$^b,$b==($!=$a))).sum+$_
Try it online!
Blergh, there's got to be a better way of doing this. I'm not 100% sure this is fully correct, though it passes all the edge cases I could think of.
Basically, this starts by grouping all the triplets of the input list, with padding to either side. For example, [1,2,1,2]
becomes (Any,1,2), (1,2,1), (2,1,2), (1,2,Nil)
. We get the repeated
elements in each triplet, becoming (), (1), (2), ()
.
It then squish
es consecutive elements that are not the same list, but are the same size (to not squish something like [1,1,1]
), and the first element is not equal to the element before it (because we can't merge the hours in [1,1,2,2]
), and finally the element before hasn't also been squished ([1,2,1,2,1,2]
). So (1), (2)
in the above example above would be squished together.
Finally, we get the sum
of all lengths of this list, which represent our inserted hours, and add the length of the original list.
For example:
(1,1,1) => (Any,1,1),(1,1,1),(1,1,Nil) => (1),(1,1),(1) => (no squishes) => 4+3 = 7
(1,2,1,2,1,2) => (Any,1,2), (1,2,1), (2,1,2), (1,2,1), (2,1,2), (1,2,Nil) => (),(1),(2),(1),(2),() => squish (1),(2) and (1),(2) => 2+6 = 8
$endgroup$
Perl 6, 98 bytes
$_ Z$_ Z .[1..*+1])>>.repeated.squish(:with($+^=[*] $! ne$^a ne$^b,$b==($!=$a))).sum+$_
Try it online!
Blergh, there's got to be a better way of doing this. I'm not 100% sure this is fully correct, though it passes all the edge cases I could think of.
Basically, this starts by grouping all the triplets of the input list, with padding to either side. For example, [1,2,1,2]
becomes (Any,1,2), (1,2,1), (2,1,2), (1,2,Nil)
. We get the repeated
elements in each triplet, becoming (), (1), (2), ()
.
It then squish
es consecutive elements that are not the same list, but are the same size (to not squish something like [1,1,1]
), and the first element is not equal to the element before it (because we can't merge the hours in [1,1,2,2]
), and finally the element before hasn't also been squished ([1,2,1,2,1,2]
). So (1), (2)
in the above example above would be squished together.
Finally, we get the sum
of all lengths of this list, which represent our inserted hours, and add the length of the original list.
For example:
(1,1,1) => (Any,1,1),(1,1,1),(1,1,Nil) => (1),(1,1),(1) => (no squishes) => 4+3 = 7
(1,2,1,2,1,2) => (Any,1,2), (1,2,1), (2,1,2), (1,2,1), (2,1,2), (1,2,Nil) => (),(1),(2),(1),(2),() => squish (1),(2) and (1),(2) => 2+6 = 8
edited Mar 22 at 12:58
answered Mar 22 at 12:33
Jo KingJo King
27.8k366134
27.8k366134
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 57 bytes
f=([x,...a],p,q)=>1/x?1+f(x!=p&x!=q?a:[x,...a,x=f],x,p):0
Try it online!
Commented
f = ( // f is a recursive function taking:
[x, // x = next job
...a], // a[] = array of remaining jobs
p, // p = previous job, initially undefined
q // q = penultimate job, initially undefined
) => //
1 / x ? // if x is defined and numeric:
1 + // add 1 to the grand total
f( // and do a recursive call to f:
x != p & // if x is different from the previous job
x != q ? // and different from the penultimate job:
a // just pass the remaining jobs
: // else:
[ x, // pass x, which can't be assigned yet
...a, // pass the remaining jobs
x = f // set x to a non-numeric value
], //
x, // previous job = x
p // penultimate job = previous job
) // end of recursive call
: // else:
0 // stop recursion
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 57 bytes
f=([x,...a],p,q)=>1/x?1+f(x!=p&x!=q?a:[x,...a,x=f],x,p):0
Try it online!
Commented
f = ( // f is a recursive function taking:
[x, // x = next job
...a], // a[] = array of remaining jobs
p, // p = previous job, initially undefined
q // q = penultimate job, initially undefined
) => //
1 / x ? // if x is defined and numeric:
1 + // add 1 to the grand total
f( // and do a recursive call to f:
x != p & // if x is different from the previous job
x != q ? // and different from the penultimate job:
a // just pass the remaining jobs
: // else:
[ x, // pass x, which can't be assigned yet
...a, // pass the remaining jobs
x = f // set x to a non-numeric value
], //
x, // previous job = x
p // penultimate job = previous job
) // end of recursive call
: // else:
0 // stop recursion
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 57 bytes
f=([x,...a],p,q)=>1/x?1+f(x!=p&x!=q?a:[x,...a,x=f],x,p):0
Try it online!
Commented
f = ( // f is a recursive function taking:
[x, // x = next job
...a], // a[] = array of remaining jobs
p, // p = previous job, initially undefined
q // q = penultimate job, initially undefined
) => //
1 / x ? // if x is defined and numeric:
1 + // add 1 to the grand total
f( // and do a recursive call to f:
x != p & // if x is different from the previous job
x != q ? // and different from the penultimate job:
a // just pass the remaining jobs
: // else:
[ x, // pass x, which can't be assigned yet
...a, // pass the remaining jobs
x = f // set x to a non-numeric value
], //
x, // previous job = x
p // penultimate job = previous job
) // end of recursive call
: // else:
0 // stop recursion
$endgroup$
JavaScript (ES6), 57 bytes
f=([x,...a],p,q)=>1/x?1+f(x!=p&x!=q?a:[x,...a,x=f],x,p):0
Try it online!
Commented
f = ( // f is a recursive function taking:
[x, // x = next job
...a], // a[] = array of remaining jobs
p, // p = previous job, initially undefined
q // q = penultimate job, initially undefined
) => //
1 / x ? // if x is defined and numeric:
1 + // add 1 to the grand total
f( // and do a recursive call to f:
x != p & // if x is different from the previous job
x != q ? // and different from the penultimate job:
a // just pass the remaining jobs
: // else:
[ x, // pass x, which can't be assigned yet
...a, // pass the remaining jobs
x = f // set x to a non-numeric value
], //
x, // previous job = x
p // penultimate job = previous job
) // end of recursive call
: // else:
0 // stop recursion
edited Mar 22 at 16:34
answered Mar 22 at 16:13
ArnauldArnauld
82.4k798339
82.4k798339
add a comment |
add a comment |
$begingroup$
C (gcc), 69 bytes
f(j,l)int*j;j=l>1?(*j-*++j?j[-1]==j[l>2]?j++,l--,3:1:3)+f(j,l-1):l;
Try it online!
Straightforward recursion.
f(j,l)int*j; //Jobs, (array) Length
j=l>1 //if l > 1, do a recursion:
? (*j-*++j // check if first and second elements are equal (j++)
? j[-1]== // 1st!=2nd; check if first and third are equal
j[l>2] // (first and second if l==2, but we already know 1st!=2nd)
? j++,l--,3 // 1st==3rd (j++,l--) return 3+f(j+2,l-2)
: 1 // 1st!=3rd (or l==2) return 1+f(j+1,l-1)
: 3 // 1st==2nd return 3+f(j+1,l-1)
)+f(j,l-1) // j and l were modified as needed
: l; // nothing more needed return l
$endgroup$
add a comment |
$begingroup$
C (gcc), 69 bytes
f(j,l)int*j;j=l>1?(*j-*++j?j[-1]==j[l>2]?j++,l--,3:1:3)+f(j,l-1):l;
Try it online!
Straightforward recursion.
f(j,l)int*j; //Jobs, (array) Length
j=l>1 //if l > 1, do a recursion:
? (*j-*++j // check if first and second elements are equal (j++)
? j[-1]== // 1st!=2nd; check if first and third are equal
j[l>2] // (first and second if l==2, but we already know 1st!=2nd)
? j++,l--,3 // 1st==3rd (j++,l--) return 3+f(j+2,l-2)
: 1 // 1st!=3rd (or l==2) return 1+f(j+1,l-1)
: 3 // 1st==2nd return 3+f(j+1,l-1)
)+f(j,l-1) // j and l were modified as needed
: l; // nothing more needed return l
$endgroup$
add a comment |
$begingroup$
C (gcc), 69 bytes
f(j,l)int*j;j=l>1?(*j-*++j?j[-1]==j[l>2]?j++,l--,3:1:3)+f(j,l-1):l;
Try it online!
Straightforward recursion.
f(j,l)int*j; //Jobs, (array) Length
j=l>1 //if l > 1, do a recursion:
? (*j-*++j // check if first and second elements are equal (j++)
? j[-1]== // 1st!=2nd; check if first and third are equal
j[l>2] // (first and second if l==2, but we already know 1st!=2nd)
? j++,l--,3 // 1st==3rd (j++,l--) return 3+f(j+2,l-2)
: 1 // 1st!=3rd (or l==2) return 1+f(j+1,l-1)
: 3 // 1st==2nd return 3+f(j+1,l-1)
)+f(j,l-1) // j and l were modified as needed
: l; // nothing more needed return l
$endgroup$
C (gcc), 69 bytes
f(j,l)int*j;j=l>1?(*j-*++j?j[-1]==j[l>2]?j++,l--,3:1:3)+f(j,l-1):l;
Try it online!
Straightforward recursion.
f(j,l)int*j; //Jobs, (array) Length
j=l>1 //if l > 1, do a recursion:
? (*j-*++j // check if first and second elements are equal (j++)
? j[-1]== // 1st!=2nd; check if first and third are equal
j[l>2] // (first and second if l==2, but we already know 1st!=2nd)
? j++,l--,3 // 1st==3rd (j++,l--) return 3+f(j+2,l-2)
: 1 // 1st!=3rd (or l==2) return 1+f(j+1,l-1)
: 3 // 1st==2nd return 3+f(j+1,l-1)
)+f(j,l-1) // j and l were modified as needed
: l; // nothing more needed return l
answered Mar 23 at 6:35
attinatattinat
63917
63917
add a comment |
add a comment |
$begingroup$
Perl 6, 48 bytes
$!=0;.map:$_=($!=$!+1 max$_)+3((%)$_);$!
Try it online!
45 bytes if the list has at least two elements:
+*.reduce:(*xx 3-(
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 48 bytes
$!=0;.map:$_=($!=$!+1 max$_)+3((%)$_);$!
Try it online!
45 bytes if the list has at least two elements:
+*.reduce:(*xx 3-(
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 48 bytes
$!=0;.map:$_=($!=$!+1 max$_)+3((%)$_);$!
Try it online!
45 bytes if the list has at least two elements:
+*.reduce:(*xx 3-(
Try it online!
$endgroup$
Perl 6, 48 bytes
$!=0;.map:$_=($!=$!+1 max$_)+3((%)$_);$!
Try it online!
45 bytes if the list has at least two elements:
+*.reduce:(*xx 3-(
Try it online!
edited Mar 23 at 15:04
answered Mar 23 at 11:04
nwellnhofnwellnhof
7,56011128
7,56011128
add a comment |
add a comment |
$begingroup$
Smalltalk, 125 bytes
c:=0.n:=q size.1to:n-2do:[:i|(j:=q at:i)=(k:=q at:i+1)ifTrue:[c:=c+2].j=(m:=q at:i+2)ifTrue:[c:=c+1]].k=m ifTrue:[c:=c+1].c+n
Explanation
c : accumulator of proximity penalty
q : input array.
n := q length
i : iteration index from 1 to: n-2 (arrays are 1-based in Smalltalk).
j := memory for element i, saves some few bytes when reused
k := similar to j but for i+1.
m := similar to k but for i+2.
$endgroup$
$begingroup$
Isn't this a snippet?
$endgroup$
– attinat
Mar 28 at 22:43
add a comment |
$begingroup$
Smalltalk, 125 bytes
c:=0.n:=q size.1to:n-2do:[:i|(j:=q at:i)=(k:=q at:i+1)ifTrue:[c:=c+2].j=(m:=q at:i+2)ifTrue:[c:=c+1]].k=m ifTrue:[c:=c+1].c+n
Explanation
c : accumulator of proximity penalty
q : input array.
n := q length
i : iteration index from 1 to: n-2 (arrays are 1-based in Smalltalk).
j := memory for element i, saves some few bytes when reused
k := similar to j but for i+1.
m := similar to k but for i+2.
$endgroup$
$begingroup$
Isn't this a snippet?
$endgroup$
– attinat
Mar 28 at 22:43
add a comment |
$begingroup$
Smalltalk, 125 bytes
c:=0.n:=q size.1to:n-2do:[:i|(j:=q at:i)=(k:=q at:i+1)ifTrue:[c:=c+2].j=(m:=q at:i+2)ifTrue:[c:=c+1]].k=m ifTrue:[c:=c+1].c+n
Explanation
c : accumulator of proximity penalty
q : input array.
n := q length
i : iteration index from 1 to: n-2 (arrays are 1-based in Smalltalk).
j := memory for element i, saves some few bytes when reused
k := similar to j but for i+1.
m := similar to k but for i+2.
$endgroup$
Smalltalk, 125 bytes
c:=0.n:=q size.1to:n-2do:[:i|(j:=q at:i)=(k:=q at:i+1)ifTrue:[c:=c+2].j=(m:=q at:i+2)ifTrue:[c:=c+1]].k=m ifTrue:[c:=c+1].c+n
Explanation
c : accumulator of proximity penalty
q : input array.
n := q length
i : iteration index from 1 to: n-2 (arrays are 1-based in Smalltalk).
j := memory for element i, saves some few bytes when reused
k := similar to j but for i+1.
m := similar to k but for i+2.
answered Mar 23 at 18:59
Leandro CanigliaLeandro Caniglia
1813
1813
$begingroup$
Isn't this a snippet?
$endgroup$
– attinat
Mar 28 at 22:43
add a comment |
$begingroup$
Isn't this a snippet?
$endgroup$
– attinat
Mar 28 at 22:43
$begingroup$
Isn't this a snippet?
$endgroup$
– attinat
Mar 28 at 22:43
$begingroup$
Isn't this a snippet?
$endgroup$
– attinat
Mar 28 at 22:43
add a comment |
$begingroup$
Perl 5 -pl
, 42 40 bytes
$a$_=~s/.*/$=$&if++$<$&;$+3/e}improve this answer
$endgroup$
$begingroup$
Cut it down to 35 using-p
and reworking the substitution: Try it online!
$endgroup$
– Xcali
Mar 22 at 21:09
$begingroup$
@Xcali That gives nothing for empty input, but I got to 39
$endgroup$
– wastl
Mar 22 at 21:31
1
$begingroup$
Doesn't seem to work for 1,1,1.
$endgroup$
– nwellnhof
Mar 23 at 15:01
$begingroup$
@nwellnhof Fixed
$endgroup$
– wastl
Mar 24 at 22:23
add a comment
$begingroup$
Perl 5 -pl
, 42 40 bytes
$a$_=~s/.*/$=$&if++$<$&;$+3/e$_=0
Try it online!
$endgroup$
$begingroup$
Welcome to PPCG! This is a pretty great first post!
$endgroup$
– Rɪᴋᴇʀ
Mar 25 at 23:45
add a comment |
$begingroup$
JavaScript (V8), 101 bytes
f=a=>for(var c=0,i=0;i<a.length;i++,c++)a[i-1]==a[i]?c+=2:a[i-2]==a[i]&&(c++,a[i-1]=void 0)
return c}
Try it online!
Unpacked code looks as follows:
function f(a)
var c = 0;
for (var i = 0; i < a.length; i++, c++)
if (a[i - 1] == a[i])
c+=2;
else if (a[i - 2] == a[i])
c++,a[i-1]=undefined;
return c;
My first ever code-golf attempt, can probably be optimized a lot by shrinking the array and passing it recursively.
$endgroup$
$begingroup$
Welcome to PPCG! This is a pretty great first post!
$endgroup$
– Rɪᴋᴇʀ
Mar 25 at 23:45
add a comment |
$begingroup$
JavaScript (V8), 101 bytes
f=a=>for(var c=0,i=0;i<a.length;i++,c++)a[i-1]==a[i]?c+=2:a[i-2]==a[i]&&(c++,a[i-1]=void 0)
return c}
Try it online!
Unpacked code looks as follows:
function f(a)
var c = 0;
for (var i = 0; i < a.length; i++, c++)
if (a[i - 1] == a[i])
c+=2;
else if (a[i - 2] == a[i])
c++,a[i-1]=undefined;
return c;
My first ever code-golf attempt, can probably be optimized a lot by shrinking the array and passing it recursively.
$endgroup$
JavaScript (V8), 101 bytes
f=a=>for(var c=0,i=0;i<a.length;i++,c++)a[i-1]==a[i]?c+=2:a[i-2]==a[i]&&(c++,a[i-1]=void 0)
return c}
Try it online!
Unpacked code looks as follows:
function f(a)
var c = 0;
for (var i = 0; i < a.length; i++, c++)
if (a[i - 1] == a[i])
c+=2;
else if (a[i - 2] == a[i])
c++,a[i-1]=undefined;
return c;
My first ever code-golf attempt, can probably be optimized a lot by shrinking the array and passing it recursively.
answered Mar 25 at 23:29
BroxzierBroxzier
1011
1011
$begingroup$
Welcome to PPCG! This is a pretty great first post!
$endgroup$
– Rɪᴋᴇʀ
Mar 25 at 23:45
add a comment |
$begingroup$
Welcome to PPCG! This is a pretty great first post!
$endgroup$
– Rɪᴋᴇʀ
Mar 25 at 23:45
$begingroup$
Welcome to PPCG! This is a pretty great first post!
$endgroup$
– Rɪᴋᴇʀ
Mar 25 at 23:45
$begingroup$
Welcome to PPCG! This is a pretty great first post!
$endgroup$
– Rɪᴋᴇʀ
Mar 25 at 23:45
add a comment |
$begingroup$
Zsh, 66 60 bytes
-6 bytes from implicit "$@"
for j
((i=$a[(I)$j]))&&a=
a=("$a[-1]" $j)
((x+=i+1))
<<<$x
Try it online! I highly recommend adding set -x
to the start so you can follow along.
for j # Implicit "$@"
# Use '' '' instead of 'do' 'done'
(( i=$a[(I)$j] )) # (see below)
&& a= # if the previous returned true, empty a
a=( "$a[-1]" $j ) # set the array to its last element and the new job
(( x += i + 1 )) # add number of slots we advanced
<<<$x # echo back our total
((i=$a[(I)$j]))
$a[ ] # Array lookup
(I)$j # Get highest index matched by $j, or 0 if not found
i= # Set to i
(( )) # If i was set nonzero, return true
a
always contains the last two jobs, so if the lookup finds a matching job in a[2]
, we increment by three (since the job slots will be [... 3 _ _ 3 ...]
).
If a
is unset, the lookup will fail and the arithmetic expansion will return an error, but that only happens on the first job and isn't fatal.
We can save one more byte if we use $[x+=i+1]
instead, and there are no commands on the users system comprised entirely of digits.
$endgroup$
add a comment |
$begingroup$
Zsh, 66 60 bytes
-6 bytes from implicit "$@"
for j
((i=$a[(I)$j]))&&a=
a=("$a[-1]" $j)
((x+=i+1))
<<<$x
Try it online! I highly recommend adding set -x
to the start so you can follow along.
for j # Implicit "$@"
# Use '' '' instead of 'do' 'done'
(( i=$a[(I)$j] )) # (see below)
&& a= # if the previous returned true, empty a
a=( "$a[-1]" $j ) # set the array to its last element and the new job
(( x += i + 1 )) # add number of slots we advanced
<<<$x # echo back our total
((i=$a[(I)$j]))
$a[ ] # Array lookup
(I)$j # Get highest index matched by $j, or 0 if not found
i= # Set to i
(( )) # If i was set nonzero, return true
a
always contains the last two jobs, so if the lookup finds a matching job in a[2]
, we increment by three (since the job slots will be [... 3 _ _ 3 ...]
).
If a
is unset, the lookup will fail and the arithmetic expansion will return an error, but that only happens on the first job and isn't fatal.
We can save one more byte if we use $[x+=i+1]
instead, and there are no commands on the users system comprised entirely of digits.
$endgroup$
add a comment |
$begingroup$
Zsh, 66 60 bytes
-6 bytes from implicit "$@"
for j
((i=$a[(I)$j]))&&a=
a=("$a[-1]" $j)
((x+=i+1))
<<<$x
Try it online! I highly recommend adding set -x
to the start so you can follow along.
for j # Implicit "$@"
# Use '' '' instead of 'do' 'done'
(( i=$a[(I)$j] )) # (see below)
&& a= # if the previous returned true, empty a
a=( "$a[-1]" $j ) # set the array to its last element and the new job
(( x += i + 1 )) # add number of slots we advanced
<<<$x # echo back our total
((i=$a[(I)$j]))
$a[ ] # Array lookup
(I)$j # Get highest index matched by $j, or 0 if not found
i= # Set to i
(( )) # If i was set nonzero, return true
a
always contains the last two jobs, so if the lookup finds a matching job in a[2]
, we increment by three (since the job slots will be [... 3 _ _ 3 ...]
).
If a
is unset, the lookup will fail and the arithmetic expansion will return an error, but that only happens on the first job and isn't fatal.
We can save one more byte if we use $[x+=i+1]
instead, and there are no commands on the users system comprised entirely of digits.
$endgroup$
Zsh, 66 60 bytes
-6 bytes from implicit "$@"
for j
((i=$a[(I)$j]))&&a=
a=("$a[-1]" $j)
((x+=i+1))
<<<$x
Try it online! I highly recommend adding set -x
to the start so you can follow along.
for j # Implicit "$@"
# Use '' '' instead of 'do' 'done'
(( i=$a[(I)$j] )) # (see below)
&& a= # if the previous returned true, empty a
a=( "$a[-1]" $j ) # set the array to its last element and the new job
(( x += i + 1 )) # add number of slots we advanced
<<<$x # echo back our total
((i=$a[(I)$j]))
$a[ ] # Array lookup
(I)$j # Get highest index matched by $j, or 0 if not found
i= # Set to i
(( )) # If i was set nonzero, return true
a
always contains the last two jobs, so if the lookup finds a matching job in a[2]
, we increment by three (since the job slots will be [... 3 _ _ 3 ...]
).
If a
is unset, the lookup will fail and the arithmetic expansion will return an error, but that only happens on the first job and isn't fatal.
We can save one more byte if we use $[x+=i+1]
instead, and there are no commands on the users system comprised entirely of digits.
edited Mar 26 at 18:53
answered Mar 24 at 9:29
GammaFunctionGammaFunction
2816
2816
add a comment |
add a comment |
$begingroup$
K (ngn/k), 27 bytes
#2_``2-x?y;`],x/x
Try it online!
$endgroup$
add a comment |
$begingroup$
K (ngn/k), 27 bytes
#2_``2-x?y;`],x/x
Try it online!
$endgroup$
add a comment |
$begingroup$
K (ngn/k), 27 bytes
#2_``2-x?y;`],x/x
Try it online!
$endgroup$
K (ngn/k), 27 bytes
#2_``2-x?y;`],x/x
Try it online!
answered Mar 28 at 22:10
ngnngn
7,47612660
7,47612660
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182010%2fcalculating-total-slots%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Is it ok if we output nothing instead of 0 for
[]
?$endgroup$
– wastl
Mar 22 at 18:50
8
$begingroup$
Isn’t it a bit early to accept an answer?
$endgroup$
– Nick Kennedy
Mar 22 at 21:10
7
$begingroup$
As @NickKennedy said, that's far, far too soon to be accepting a solution. Some even recommend never accepting a solution.
$endgroup$
– Shaggy
Mar 22 at 23:41