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
How to create table with 2D function values?
Multiplicative persistence
Is this toilet slogan correct usage of the English language?
It grows, but water kills it
What is going on with 'gets(stdin)' on the site coderbyte?
How much character growth crosses the line into breaking the character
Quoting Keynes in a lecture
Angel of Condemnation - Exile creature with second ability
Are these expressions not equal? Mathematica output is ambiguous
Add big quotation marks inside my colorbox
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
How to get directions in deep space?
Bridge building with irregular planks
What if a revenant (monster) gains fire resistance?
Why does a simple loop result in ASYNC_NETWORK_IO waits?
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
What if you are holding an Iron Flask with a demon inside and walk into Antimagic Field?
Why is short-wave infrared portion of electromagnetic spectrum so sensitive to fire?
Why is the "ls" command showing permissions of files in a FAT32 partition?
Temporarily disable WLAN internet access for children, but allow it for adults
How should I address a possible mistake to co-authors in a submitted paper
How should you respond if you lied about your education and your company finds out through background check?
Why is it that I can sometimes guess the next note?
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$
|
show 13 more comments
$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$
3
$begingroup$
I'm sorry, but I don't understand what is going on at all. Separately you also haven't specified a winning criterion.
$endgroup$
– FryAmTheEggman
18 hours ago
2
$begingroup$
I think you should state more clearly that the 2-hour break is always between same jobs. Is this code-golf?
$endgroup$
– J42161217
18 hours ago
1
$begingroup$
@UnrelatedString I edited! Thanks :D
$endgroup$
– jayko03
17 hours ago
3
$begingroup$
Why does[3,4,4,3]result in 4? Shouldn't it be 6 ([3,4,_,_,4,3])? Or is it a typo and one of the two4in the input should be a number that isn't3nor4(i.e.[3,4,5,3])?
$endgroup$
– Kevin Cruijssen
11 hours ago
1
$begingroup$
@Neil [3,4,3,4] is already there
$endgroup$
– J42161217
9 hours ago
|
show 13 more comments
$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 9 hours ago
Kevin Cruijssen
41.2k567212
41.2k567212
asked 18 hours ago
jayko03jayko03
2688
2688
3
$begingroup$
I'm sorry, but I don't understand what is going on at all. Separately you also haven't specified a winning criterion.
$endgroup$
– FryAmTheEggman
18 hours ago
2
$begingroup$
I think you should state more clearly that the 2-hour break is always between same jobs. Is this code-golf?
$endgroup$
– J42161217
18 hours ago
1
$begingroup$
@UnrelatedString I edited! Thanks :D
$endgroup$
– jayko03
17 hours ago
3
$begingroup$
Why does[3,4,4,3]result in 4? Shouldn't it be 6 ([3,4,_,_,4,3])? Or is it a typo and one of the two4in the input should be a number that isn't3nor4(i.e.[3,4,5,3])?
$endgroup$
– Kevin Cruijssen
11 hours ago
1
$begingroup$
@Neil [3,4,3,4] is already there
$endgroup$
– J42161217
9 hours ago
|
show 13 more comments
3
$begingroup$
I'm sorry, but I don't understand what is going on at all. Separately you also haven't specified a winning criterion.
$endgroup$
– FryAmTheEggman
18 hours ago
2
$begingroup$
I think you should state more clearly that the 2-hour break is always between same jobs. Is this code-golf?
$endgroup$
– J42161217
18 hours ago
1
$begingroup$
@UnrelatedString I edited! Thanks :D
$endgroup$
– jayko03
17 hours ago
3
$begingroup$
Why does[3,4,4,3]result in 4? Shouldn't it be 6 ([3,4,_,_,4,3])? Or is it a typo and one of the two4in the input should be a number that isn't3nor4(i.e.[3,4,5,3])?
$endgroup$
– Kevin Cruijssen
11 hours ago
1
$begingroup$
@Neil [3,4,3,4] is already there
$endgroup$
– J42161217
9 hours ago
3
3
$begingroup$
I'm sorry, but I don't understand what is going on at all. Separately you also haven't specified a winning criterion.
$endgroup$
– FryAmTheEggman
18 hours ago
$begingroup$
I'm sorry, but I don't understand what is going on at all. Separately you also haven't specified a winning criterion.
$endgroup$
– FryAmTheEggman
18 hours ago
2
2
$begingroup$
I think you should state more clearly that the 2-hour break is always between same jobs. Is this code-golf?
$endgroup$
– J42161217
18 hours ago
$begingroup$
I think you should state more clearly that the 2-hour break is always between same jobs. Is this code-golf?
$endgroup$
– J42161217
18 hours ago
1
1
$begingroup$
@UnrelatedString I edited! Thanks :D
$endgroup$
– jayko03
17 hours ago
$begingroup$
@UnrelatedString I edited! Thanks :D
$endgroup$
– jayko03
17 hours ago
3
3
$begingroup$
Why does
[3,4,4,3] result in 4? Shouldn't it be 6 ([3,4,_,_,4,3])? Or is it a typo and one of the two 4 in the input should be a number that isn't 3 nor 4 (i.e. [3,4,5,3])?$endgroup$
– Kevin Cruijssen
11 hours ago
$begingroup$
Why does
[3,4,4,3] result in 4? Shouldn't it be 6 ([3,4,_,_,4,3])? Or is it a typo and one of the two 4 in the input should be a number that isn't 3 nor 4 (i.e. [3,4,5,3])?$endgroup$
– Kevin Cruijssen
11 hours ago
1
1
$begingroup$
@Neil [3,4,3,4] is already there
$endgroup$
– J42161217
9 hours ago
$begingroup$
@Neil [3,4,3,4] is already there
$endgroup$
– J42161217
9 hours ago
|
show 13 more comments
13 Answers
13
active
oldest
votes
$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$
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$
Charcoal, 27 bytes
⊞υωFθ«F²F⁼ι§υ±⊕κ⊞υω⊞υι»I⊖Lυ
Try it online! Link is to verbose version of code. Explanation:
⊞υω
Push a dummy cooling off spot to the predefined empty list.
Fθ«
Loop over the jobs.
F²F⁼ι§υ±⊕κ⊞υω
Add cooling off spots to ensure that the job is not one of the last two in the result.
⊞υι»
Add the current job to the result.
I⊖Lυ
Print the number of spots adjusted for the initial extra cooling off spot.
$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 bytes
sum((l=rle(s<-scan())$l-1)*3,1-l%/%2,(r=rle(s==c(s[-1:-2],.1,.1)))$v*(r$l+1)%/%2)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least I hope 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. To detect these, we compare each element of our input sequence s with a subsequence starting from the 3rd element (1-indexed) padded by impossible values (floats) to prevent R from recycling the sequence from the beginning. This produces a vector of booleans, which we also chunk into consecutive runs (r) by rle function. Now, because of various interleaved alternations we need to add ceiling(r$len/2) steps for all truthy runs. 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%/%2 instead of simply 1.
$endgroup$
add a comment |
$begingroup$
Perl 6, 98 bytes
($!=$,
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 squishes 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# (Visual C# Interactive Compiler), 75 62 bytes
n=>n.Select((a,b)=>b<1?1:n[b-1]==a?3:b>1&&n[b-2]==a?2:1).Sum()
Very simple, maps each element to 3 if the element behind it is the same as itself, 2 if the element 2 spaces behind it is the same as itself, and 1 otherwise, then sums it all up.
Try it online!
$endgroup$
$begingroup$
Unfortunately, not so simple:3,4,3,4is 5, not 6.
$endgroup$
– Kirill L.
48 mins ago
add a comment |
$begingroup$
Batch, 184 bytes
@echo off
@set l=-
@set p=-
@set n=0
@for %%j in (%*)do @call:c %%j
@exit/b%n%
:c
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
@set p=%l%&set l=%1&set/an+=1
Input is via command-line arguments and output is via exit code. Explanation:
@set l=-
@set p=-
Track the last two jobs.
@set n=0
Initialise the count.
@for %%j in (%*)do @call:c %%j
Process each job.
@exit/b%n%
Output the final count.
:c
For each job:
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
If we processed the job recently, add in an appropriate number of cooling off spots. Additionally, clear the last job so that the next job only triggers cooling off if it's the same as this job.
@set p=%l%&set l=%1&set/an+=1
Update the last two jobs and allocate a spot to this job.
$endgroup$
add a comment |
$begingroup$
Python 3, 79 bytes
f=lambda a,b=[]:a and(a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b))or len(b)
Try it online!
$endgroup$
$begingroup$
a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b)can becomef(*[a[1:],a,[a[0]]+b,['']+b][a[0]in b[:2]::2])to save 2 bytes.
$endgroup$
– mypetlion
1 hour ago
$begingroup$
[a[0]]+bcan becomea[:1]+bto save 1 byte.
$endgroup$
– mypetlion
1 hour ago
add a comment |
$begingroup$
Swift, 114 bytes
func t(a:[Int])
var s=1
for i in 1...a.count-1s = a[i-1]==a[i] ? s+3:i>1&&a[i-2]==a[i] ? s+2:s+1
print("(s)")
Try it online!
$endgroup$
$begingroup$
Fails for3,4,3,4, should bet 5, not 6.
$endgroup$
– Kirill L.
42 mins ago
add a comment |
$begingroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 74 bytes
length(Reduce(function(x,y)c(y,rep("",max(0,which(y==x[2:1]))),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter than Kirill L.'s answer, so sometimes, the naive approach really works.
Before posting, one of my earlier revisions was similar but much longer, which I include just because.
R, 86 bytes
length(Reduce(function(x,y)c(y,rep("",min(2,1:2%*%(y==x[1:2]))),x),scan(),c("","")))-2
Try it online!
The major difference is using which() to avoid having to set init=c("","") in Reduce to avoid results of NA in the x[2:1] when the array is empty.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
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
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
$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$
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$
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 7 hours ago
answered 8 hours ago
Kevin CruijssenKevin Cruijssen
41.2k567212
41.2k567212
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 9 hours ago
answered 9 hours ago
Nick KennedyNick Kennedy
89137
89137
add a comment |
add a comment |
$begingroup$
Charcoal, 27 bytes
⊞υωFθ«F²F⁼ι§υ±⊕κ⊞υω⊞υι»I⊖Lυ
Try it online! Link is to verbose version of code. Explanation:
⊞υω
Push a dummy cooling off spot to the predefined empty list.
Fθ«
Loop over the jobs.
F²F⁼ι§υ±⊕κ⊞υω
Add cooling off spots to ensure that the job is not one of the last two in the result.
⊞υι»
Add the current job to the result.
I⊖Lυ
Print the number of spots adjusted for the initial extra cooling off spot.
$endgroup$
add a comment |
$begingroup$
Charcoal, 27 bytes
⊞υωFθ«F²F⁼ι§υ±⊕κ⊞υω⊞υι»I⊖Lυ
Try it online! Link is to verbose version of code. Explanation:
⊞υω
Push a dummy cooling off spot to the predefined empty list.
Fθ«
Loop over the jobs.
F²F⁼ι§υ±⊕κ⊞υω
Add cooling off spots to ensure that the job is not one of the last two in the result.
⊞υι»
Add the current job to the result.
I⊖Lυ
Print the number of spots adjusted for the initial extra cooling off spot.
$endgroup$
add a comment |
$begingroup$
Charcoal, 27 bytes
⊞υωFθ«F²F⁼ι§υ±⊕κ⊞υω⊞υι»I⊖Lυ
Try it online! Link is to verbose version of code. Explanation:
⊞υω
Push a dummy cooling off spot to the predefined empty list.
Fθ«
Loop over the jobs.
F²F⁼ι§υ±⊕κ⊞υω
Add cooling off spots to ensure that the job is not one of the last two in the result.
⊞υι»
Add the current job to the result.
I⊖Lυ
Print the number of spots adjusted for the initial extra cooling off spot.
$endgroup$
Charcoal, 27 bytes
⊞υωFθ«F²F⁼ι§υ±⊕κ⊞υω⊞υι»I⊖Lυ
Try it online! Link is to verbose version of code. Explanation:
⊞υω
Push a dummy cooling off spot to the predefined empty list.
Fθ«
Loop over the jobs.
F²F⁼ι§υ±⊕κ⊞υω
Add cooling off spots to ensure that the job is not one of the last two in the result.
⊞υι»
Add the current job to the result.
I⊖Lυ
Print the number of spots adjusted for the initial extra cooling off spot.
answered 8 hours ago
NeilNeil
82k745178
82k745178
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 6 hours ago
answered 6 hours ago
t-clausen.dkt-clausen.dk
1,984314
1,984314
add a comment |
add a comment |
$begingroup$
R, 81 bytes
sum((l=rle(s<-scan())$l-1)*3,1-l%/%2,(r=rle(s==c(s[-1:-2],.1,.1)))$v*(r$l+1)%/%2)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least I hope 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. To detect these, we compare each element of our input sequence s with a subsequence starting from the 3rd element (1-indexed) padded by impossible values (floats) to prevent R from recycling the sequence from the beginning. This produces a vector of booleans, which we also chunk into consecutive runs (r) by rle function. Now, because of various interleaved alternations we need to add ceiling(r$len/2) steps for all truthy runs. 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%/%2 instead of simply 1.
$endgroup$
add a comment |
$begingroup$
R, 81 bytes
sum((l=rle(s<-scan())$l-1)*3,1-l%/%2,(r=rle(s==c(s[-1:-2],.1,.1)))$v*(r$l+1)%/%2)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least I hope 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. To detect these, we compare each element of our input sequence s with a subsequence starting from the 3rd element (1-indexed) padded by impossible values (floats) to prevent R from recycling the sequence from the beginning. This produces a vector of booleans, which we also chunk into consecutive runs (r) by rle function. Now, because of various interleaved alternations we need to add ceiling(r$len/2) steps for all truthy runs. 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%/%2 instead of simply 1.
$endgroup$
add a comment |
$begingroup$
R, 81 bytes
sum((l=rle(s<-scan())$l-1)*3,1-l%/%2,(r=rle(s==c(s[-1:-2],.1,.1)))$v*(r$l+1)%/%2)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least I hope 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. To detect these, we compare each element of our input sequence s with a subsequence starting from the 3rd element (1-indexed) padded by impossible values (floats) to prevent R from recycling the sequence from the beginning. This produces a vector of booleans, which we also chunk into consecutive runs (r) by rle function. Now, because of various interleaved alternations we need to add ceiling(r$len/2) steps for all truthy runs. 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%/%2 instead of simply 1.
$endgroup$
R, 81 bytes
sum((l=rle(s<-scan())$l-1)*3,1-l%/%2,(r=rle(s==c(s[-1:-2],.1,.1)))$v*(r$l+1)%/%2)
Try it online!
After several unsuccessful attempts, the code turned rather ugly and not so short, but at least I hope 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. To detect these, we compare each element of our input sequence s with a subsequence starting from the 3rd element (1-indexed) padded by impossible values (floats) to prevent R from recycling the sequence from the beginning. This produces a vector of booleans, which we also chunk into consecutive runs (r) by rle function. Now, because of various interleaved alternations we need to add ceiling(r$len/2) steps for all truthy runs. 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%/%2 instead of simply 1.
edited 6 hours ago
answered 8 hours ago
Kirill L.Kirill L.
5,6831525
5,6831525
add a comment |
add a comment |
$begingroup$
Perl 6, 98 bytes
($!=$,
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 squishes 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
($!=$,
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 squishes 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
($!=$,
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 squishes 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
($!=$,
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 squishes 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 6 hours ago
answered 6 hours ago
Jo KingJo King
25.2k360129
25.2k360129
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 2 hours ago
answered 3 hours ago
ArnauldArnauld
79.5k796330
79.5k796330
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 75 62 bytes
n=>n.Select((a,b)=>b<1?1:n[b-1]==a?3:b>1&&n[b-2]==a?2:1).Sum()
Very simple, maps each element to 3 if the element behind it is the same as itself, 2 if the element 2 spaces behind it is the same as itself, and 1 otherwise, then sums it all up.
Try it online!
$endgroup$
$begingroup$
Unfortunately, not so simple:3,4,3,4is 5, not 6.
$endgroup$
– Kirill L.
48 mins ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 75 62 bytes
n=>n.Select((a,b)=>b<1?1:n[b-1]==a?3:b>1&&n[b-2]==a?2:1).Sum()
Very simple, maps each element to 3 if the element behind it is the same as itself, 2 if the element 2 spaces behind it is the same as itself, and 1 otherwise, then sums it all up.
Try it online!
$endgroup$
$begingroup$
Unfortunately, not so simple:3,4,3,4is 5, not 6.
$endgroup$
– Kirill L.
48 mins ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 75 62 bytes
n=>n.Select((a,b)=>b<1?1:n[b-1]==a?3:b>1&&n[b-2]==a?2:1).Sum()
Very simple, maps each element to 3 if the element behind it is the same as itself, 2 if the element 2 spaces behind it is the same as itself, and 1 otherwise, then sums it all up.
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 75 62 bytes
n=>n.Select((a,b)=>b<1?1:n[b-1]==a?3:b>1&&n[b-2]==a?2:1).Sum()
Very simple, maps each element to 3 if the element behind it is the same as itself, 2 if the element 2 spaces behind it is the same as itself, and 1 otherwise, then sums it all up.
Try it online!
edited 2 hours ago
answered 2 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
2,128125
2,128125
$begingroup$
Unfortunately, not so simple:3,4,3,4is 5, not 6.
$endgroup$
– Kirill L.
48 mins ago
add a comment |
$begingroup$
Unfortunately, not so simple:3,4,3,4is 5, not 6.
$endgroup$
– Kirill L.
48 mins ago
$begingroup$
Unfortunately, not so simple:
3,4,3,4 is 5, not 6.$endgroup$
– Kirill L.
48 mins ago
$begingroup$
Unfortunately, not so simple:
3,4,3,4 is 5, not 6.$endgroup$
– Kirill L.
48 mins ago
add a comment |
$begingroup$
Batch, 184 bytes
@echo off
@set l=-
@set p=-
@set n=0
@for %%j in (%*)do @call:c %%j
@exit/b%n%
:c
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
@set p=%l%&set l=%1&set/an+=1
Input is via command-line arguments and output is via exit code. Explanation:
@set l=-
@set p=-
Track the last two jobs.
@set n=0
Initialise the count.
@for %%j in (%*)do @call:c %%j
Process each job.
@exit/b%n%
Output the final count.
:c
For each job:
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
If we processed the job recently, add in an appropriate number of cooling off spots. Additionally, clear the last job so that the next job only triggers cooling off if it's the same as this job.
@set p=%l%&set l=%1&set/an+=1
Update the last two jobs and allocate a spot to this job.
$endgroup$
add a comment |
$begingroup$
Batch, 184 bytes
@echo off
@set l=-
@set p=-
@set n=0
@for %%j in (%*)do @call:c %%j
@exit/b%n%
:c
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
@set p=%l%&set l=%1&set/an+=1
Input is via command-line arguments and output is via exit code. Explanation:
@set l=-
@set p=-
Track the last two jobs.
@set n=0
Initialise the count.
@for %%j in (%*)do @call:c %%j
Process each job.
@exit/b%n%
Output the final count.
:c
For each job:
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
If we processed the job recently, add in an appropriate number of cooling off spots. Additionally, clear the last job so that the next job only triggers cooling off if it's the same as this job.
@set p=%l%&set l=%1&set/an+=1
Update the last two jobs and allocate a spot to this job.
$endgroup$
add a comment |
$begingroup$
Batch, 184 bytes
@echo off
@set l=-
@set p=-
@set n=0
@for %%j in (%*)do @call:c %%j
@exit/b%n%
:c
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
@set p=%l%&set l=%1&set/an+=1
Input is via command-line arguments and output is via exit code. Explanation:
@set l=-
@set p=-
Track the last two jobs.
@set n=0
Initialise the count.
@for %%j in (%*)do @call:c %%j
Process each job.
@exit/b%n%
Output the final count.
:c
For each job:
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
If we processed the job recently, add in an appropriate number of cooling off spots. Additionally, clear the last job so that the next job only triggers cooling off if it's the same as this job.
@set p=%l%&set l=%1&set/an+=1
Update the last two jobs and allocate a spot to this job.
$endgroup$
Batch, 184 bytes
@echo off
@set l=-
@set p=-
@set n=0
@for %%j in (%*)do @call:c %%j
@exit/b%n%
:c
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
@set p=%l%&set l=%1&set/an+=1
Input is via command-line arguments and output is via exit code. Explanation:
@set l=-
@set p=-
Track the last two jobs.
@set n=0
Initialise the count.
@for %%j in (%*)do @call:c %%j
Process each job.
@exit/b%n%
Output the final count.
:c
For each job:
@if %1==%l% (set l=-&set/an+=2)else if %1==%p% set l=-&set/an+=1
If we processed the job recently, add in an appropriate number of cooling off spots. Additionally, clear the last job so that the next job only triggers cooling off if it's the same as this job.
@set p=%l%&set l=%1&set/an+=1
Update the last two jobs and allocate a spot to this job.
answered 9 hours ago
NeilNeil
82k745178
82k745178
add a comment |
add a comment |
$begingroup$
Python 3, 79 bytes
f=lambda a,b=[]:a and(a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b))or len(b)
Try it online!
$endgroup$
$begingroup$
a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b)can becomef(*[a[1:],a,[a[0]]+b,['']+b][a[0]in b[:2]::2])to save 2 bytes.
$endgroup$
– mypetlion
1 hour ago
$begingroup$
[a[0]]+bcan becomea[:1]+bto save 1 byte.
$endgroup$
– mypetlion
1 hour ago
add a comment |
$begingroup$
Python 3, 79 bytes
f=lambda a,b=[]:a and(a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b))or len(b)
Try it online!
$endgroup$
$begingroup$
a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b)can becomef(*[a[1:],a,[a[0]]+b,['']+b][a[0]in b[:2]::2])to save 2 bytes.
$endgroup$
– mypetlion
1 hour ago
$begingroup$
[a[0]]+bcan becomea[:1]+bto save 1 byte.
$endgroup$
– mypetlion
1 hour ago
add a comment |
$begingroup$
Python 3, 79 bytes
f=lambda a,b=[]:a and(a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b))or len(b)
Try it online!
$endgroup$
Python 3, 79 bytes
f=lambda a,b=[]:a and(a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b))or len(b)
Try it online!
answered 4 hours ago
Jonas AuseviciusJonas Ausevicius
1413
1413
$begingroup$
a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b)can becomef(*[a[1:],a,[a[0]]+b,['']+b][a[0]in b[:2]::2])to save 2 bytes.
$endgroup$
– mypetlion
1 hour ago
$begingroup$
[a[0]]+bcan becomea[:1]+bto save 1 byte.
$endgroup$
– mypetlion
1 hour ago
add a comment |
$begingroup$
a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b)can becomef(*[a[1:],a,[a[0]]+b,['']+b][a[0]in b[:2]::2])to save 2 bytes.
$endgroup$
– mypetlion
1 hour ago
$begingroup$
[a[0]]+bcan becomea[:1]+bto save 1 byte.
$endgroup$
– mypetlion
1 hour ago
$begingroup$
a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b) can become f(*[a[1:],a,[a[0]]+b,['']+b][a[0]in b[:2]::2]) to save 2 bytes.$endgroup$
– mypetlion
1 hour ago
$begingroup$
a[0]in b[:2]and f(a,['']+b)or f(a[1:],[a[0]]+b) can become f(*[a[1:],a,[a[0]]+b,['']+b][a[0]in b[:2]::2]) to save 2 bytes.$endgroup$
– mypetlion
1 hour ago
$begingroup$
[a[0]]+b can become a[:1]+b to save 1 byte.$endgroup$
– mypetlion
1 hour ago
$begingroup$
[a[0]]+b can become a[:1]+b to save 1 byte.$endgroup$
– mypetlion
1 hour ago
add a comment |
$begingroup$
Swift, 114 bytes
func t(a:[Int])
var s=1
for i in 1...a.count-1s = a[i-1]==a[i] ? s+3:i>1&&a[i-2]==a[i] ? s+2:s+1
print("(s)")
Try it online!
$endgroup$
$begingroup$
Fails for3,4,3,4, should bet 5, not 6.
$endgroup$
– Kirill L.
42 mins ago
add a comment |
$begingroup$
Swift, 114 bytes
func t(a:[Int])
var s=1
for i in 1...a.count-1s = a[i-1]==a[i] ? s+3:i>1&&a[i-2]==a[i] ? s+2:s+1
print("(s)")
Try it online!
$endgroup$
$begingroup$
Fails for3,4,3,4, should bet 5, not 6.
$endgroup$
– Kirill L.
42 mins ago
add a comment |
$begingroup$
Swift, 114 bytes
func t(a:[Int])
var s=1
for i in 1...a.count-1s = a[i-1]==a[i] ? s+3:i>1&&a[i-2]==a[i] ? s+2:s+1
print("(s)")
Try it online!
$endgroup$
Swift, 114 bytes
func t(a:[Int])
var s=1
for i in 1...a.count-1s = a[i-1]==a[i] ? s+3:i>1&&a[i-2]==a[i] ? s+2:s+1
print("(s)")
Try it online!
answered 1 hour ago
onnowebonnoweb
1512
1512
$begingroup$
Fails for3,4,3,4, should bet 5, not 6.
$endgroup$
– Kirill L.
42 mins ago
add a comment |
$begingroup$
Fails for3,4,3,4, should bet 5, not 6.
$endgroup$
– Kirill L.
42 mins ago
$begingroup$
Fails for
3,4,3,4, should bet 5, not 6.$endgroup$
– Kirill L.
42 mins ago
$begingroup$
Fails for
3,4,3,4, should bet 5, not 6.$endgroup$
– Kirill L.
42 mins ago
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$
add a comment |
$begingroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
$endgroup$
Jelly, 14 bytes
ṫ-i⁹⁶ẋ⁸;;µƒ⁶L’
Try it online!
answered 1 hour ago
Erik the OutgolferErik the Outgolfer
32.7k429105
32.7k429105
add a comment |
add a comment |
$begingroup$
R, 74 bytes
length(Reduce(function(x,y)c(y,rep("",max(0,which(y==x[2:1]))),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter than Kirill L.'s answer, so sometimes, the naive approach really works.
Before posting, one of my earlier revisions was similar but much longer, which I include just because.
R, 86 bytes
length(Reduce(function(x,y)c(y,rep("",min(2,1:2%*%(y==x[1:2]))),x),scan(),c("","")))-2
Try it online!
The major difference is using which() to avoid having to set init=c("","") in Reduce to avoid results of NA in the x[2:1] when the array is empty.
$endgroup$
add a comment |
$begingroup$
R, 74 bytes
length(Reduce(function(x,y)c(y,rep("",max(0,which(y==x[2:1]))),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter than Kirill L.'s answer, so sometimes, the naive approach really works.
Before posting, one of my earlier revisions was similar but much longer, which I include just because.
R, 86 bytes
length(Reduce(function(x,y)c(y,rep("",min(2,1:2%*%(y==x[1:2]))),x),scan(),c("","")))-2
Try it online!
The major difference is using which() to avoid having to set init=c("","") in Reduce to avoid results of NA in the x[2:1] when the array is empty.
$endgroup$
add a comment |
$begingroup$
R, 74 bytes
length(Reduce(function(x,y)c(y,rep("",max(0,which(y==x[2:1]))),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter than Kirill L.'s answer, so sometimes, the naive approach really works.
Before posting, one of my earlier revisions was similar but much longer, which I include just because.
R, 86 bytes
length(Reduce(function(x,y)c(y,rep("",min(2,1:2%*%(y==x[1:2]))),x),scan(),c("","")))-2
Try it online!
The major difference is using which() to avoid having to set init=c("","") in Reduce to avoid results of NA in the x[2:1] when the array is empty.
$endgroup$
R, 74 bytes
length(Reduce(function(x,y)c(y,rep("",max(0,which(y==x[2:1]))),x),scan()))
Try it online!
Constructs the work array (in reverse), then takes the length. Just a bit shorter than Kirill L.'s answer, so sometimes, the naive approach really works.
Before posting, one of my earlier revisions was similar but much longer, which I include just because.
R, 86 bytes
length(Reduce(function(x,y)c(y,rep("",min(2,1:2%*%(y==x[1:2]))),x),scan(),c("","")))-2
Try it online!
The major difference is using which() to avoid having to set init=c("","") in Reduce to avoid results of NA in the x[2:1] when the array is empty.
answered 21 mins ago
GiuseppeGiuseppe
17k31052
17k31052
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
3
$begingroup$
I'm sorry, but I don't understand what is going on at all. Separately you also haven't specified a winning criterion.
$endgroup$
– FryAmTheEggman
18 hours ago
2
$begingroup$
I think you should state more clearly that the 2-hour break is always between same jobs. Is this code-golf?
$endgroup$
– J42161217
18 hours ago
1
$begingroup$
@UnrelatedString I edited! Thanks :D
$endgroup$
– jayko03
17 hours ago
3
$begingroup$
Why does
[3,4,4,3]result in 4? Shouldn't it be 6 ([3,4,_,_,4,3])? Or is it a typo and one of the two4in the input should be a number that isn't3nor4(i.e.[3,4,5,3])?$endgroup$
– Kevin Cruijssen
11 hours ago
1
$begingroup$
@Neil [3,4,3,4] is already there
$endgroup$
– J42161217
9 hours ago