Attacking the HydraReturn of the Hydra SlayerBecome the Hydra SlayerCalculate probability of getting half as many heads as coin tosses.Golfing: How many unit-length squares in a list of 2d coordinates?Become the Hydra SlayerReturn of the Hydra SlayerMutually Attacking QueensMagic: The Gathering Combat with AbilitiesCount the corners, edges and faces of a cut cubeBorderless tableInvert Some Switches on a Switchboard
Is there a way to proportionalize fixed costs in a MILP?
How to gracefully leave a company you helped start?
Cycle of actions and voice signals on a multipitch climb
Identifying My Main Water Shutoff Valve / Setup
Are there examples in Tanach of 3 or more parties having an ongoing conversation?
Finding the shaded region
graphs in latex
Cases with long math equation
Do I have to cite common CS algorithms?
(A room / an office) where an artist works
What is the hottest thing in the universe?
Why did IBM make the PC BIOS source code public?
Are employers legally allowed to pay employees in goods and services equal to or greater than the minimum wage?
Shifting tenses in the middle of narration
Is there a fallacy about "appeal to 'big words'"?
Word for an event that will likely never happen again
Installing Windows to flash UEFI/ BIOS, then reinstalling Ubuntu
Global BGP Routing only by only importing supernet prefixes
What unique challenges/limitations will I face if I start a career as a pilot at 45 years old?
Is it really Security Misconfiguration to show a version number?
Escape Velocity - Won't the orbital path just become larger with higher initial velocity?
How can I find an old paper when the usual methods fail?
How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?
How do I ask for 2-3 days per week remote work in a job interview?
Attacking the Hydra
Return of the Hydra SlayerBecome the Hydra SlayerCalculate probability of getting half as many heads as coin tosses.Golfing: How many unit-length squares in a list of 2d coordinates?Become the Hydra SlayerReturn of the Hydra SlayerMutually Attacking QueensMagic: The Gathering Combat with AbilitiesCount the corners, edges and faces of a cut cubeBorderless tableInvert Some Switches on a Switchboard
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Instead of being a skillful warrior capable of slaying Hydras (see here and here), this time you are a warrior that has no prior knowledge on how to kill one or which weapons to use against the creature.
In this problem, whenever you cut a single head off, two will grow in the same place. Since you don't have the mechanism to cut many heads off simultaneously, the number of heads will only grow. In this case, our Hydra can start with N
(N ⩾ 1) heads. Let's call the first encounter a generation and we will represent the heads from the first generation as 0, the heads created after the first blow as 1, and so on.
Input
You will be given an integer N
representing how many heads the Hydra initially have and a list of size N
containing in which index (in the examples I will use 0-indexed format) you will cut a head off. You can always assume the indexes given are valid - remember that the list (i.e.: the heads) will grow as you cut heads off.
Example
Input: N = 4
and [0,4,2,5]
Generation 0 - Attack index 0
0 0 0 0 => 1 1 0 0 0
^ ^ ^
Generation 1 - Attack index 4
1 1 0 0 0 => 1 1 0 0 2 2
^ ^ ^
Generation 2 - Attack index 2
1 1 0 0 2 2 => 1 1 3 3 0 2 2
^ ^ ^
Generation 3 - Attack index 5
1 1 3 3 0 2 2 => 1 1 3 3 0 4 4 2
^ ^ ^
Last generation
1 1 3 3 0 4 4 2
As you can see, the indexes given are related to the list of the previous generation.
Output
You are required to output the last generation.
Test cases
N = 1 and [0] => [1,1]
N = 2 and [0,0] => [2,2,1,0]
N = 2 and [0,1] => [1,2,2,0]
N = 2 and [1,0] => [2,2,1,1]
N = 2 and [1,1] => [0,2,2,1]
N = 4 and [0,4,2,5] => [1,1,3,3,0,4,4,2]
N = 6 and [0,0,0,0,0,0] => [6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0]
N = 6 and [5,6,7,8,9,10] => [0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 6]
N = 10 and [1,7,3,12,9,0,15,2,2,10] => [6, 6, 9, 9, 8, 1, 3, 3, 0, 0, 10, 10, 2, 5, 5, 0, 0, 4, 7, 7]
This is code-golf so shortest answer in bytes wins!
code-golf
New contributor
$endgroup$
add a comment |
$begingroup$
Instead of being a skillful warrior capable of slaying Hydras (see here and here), this time you are a warrior that has no prior knowledge on how to kill one or which weapons to use against the creature.
In this problem, whenever you cut a single head off, two will grow in the same place. Since you don't have the mechanism to cut many heads off simultaneously, the number of heads will only grow. In this case, our Hydra can start with N
(N ⩾ 1) heads. Let's call the first encounter a generation and we will represent the heads from the first generation as 0, the heads created after the first blow as 1, and so on.
Input
You will be given an integer N
representing how many heads the Hydra initially have and a list of size N
containing in which index (in the examples I will use 0-indexed format) you will cut a head off. You can always assume the indexes given are valid - remember that the list (i.e.: the heads) will grow as you cut heads off.
Example
Input: N = 4
and [0,4,2,5]
Generation 0 - Attack index 0
0 0 0 0 => 1 1 0 0 0
^ ^ ^
Generation 1 - Attack index 4
1 1 0 0 0 => 1 1 0 0 2 2
^ ^ ^
Generation 2 - Attack index 2
1 1 0 0 2 2 => 1 1 3 3 0 2 2
^ ^ ^
Generation 3 - Attack index 5
1 1 3 3 0 2 2 => 1 1 3 3 0 4 4 2
^ ^ ^
Last generation
1 1 3 3 0 4 4 2
As you can see, the indexes given are related to the list of the previous generation.
Output
You are required to output the last generation.
Test cases
N = 1 and [0] => [1,1]
N = 2 and [0,0] => [2,2,1,0]
N = 2 and [0,1] => [1,2,2,0]
N = 2 and [1,0] => [2,2,1,1]
N = 2 and [1,1] => [0,2,2,1]
N = 4 and [0,4,2,5] => [1,1,3,3,0,4,4,2]
N = 6 and [0,0,0,0,0,0] => [6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0]
N = 6 and [5,6,7,8,9,10] => [0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 6]
N = 10 and [1,7,3,12,9,0,15,2,2,10] => [6, 6, 9, 9, 8, 1, 3, 3, 0, 0, 10, 10, 2, 5, 5, 0, 0, 4, 7, 7]
This is code-golf so shortest answer in bytes wins!
code-golf
New contributor
$endgroup$
$begingroup$
Sandbox link
$endgroup$
– ihavenoidea
9 hours ago
$begingroup$
Needs a test case where the initial number of heads is greater than the number of heads cut off. I think I see at least one current answer which would fail that case.
$endgroup$
– Xcali
8 hours ago
$begingroup$
@Xcali The number of heads to cut off is actually guaranteed to be equal to the initial number of heads: You will be given an integerN
(...) and a list of sizeN
(But I missed that part as well when I first read the challenge.) Therefore,N
is simply useless.
$endgroup$
– Arnauld
7 hours ago
$begingroup$
Ah. You're right, I missed it.
$endgroup$
– Xcali
7 hours ago
2
$begingroup$
I thought about actually removingN
from the input since it is "implicitly" given as the array's size. However, I thought the solutions would save bytes by givingN
instead of them relying onarray.size()
or similar.
$endgroup$
– ihavenoidea
7 hours ago
add a comment |
$begingroup$
Instead of being a skillful warrior capable of slaying Hydras (see here and here), this time you are a warrior that has no prior knowledge on how to kill one or which weapons to use against the creature.
In this problem, whenever you cut a single head off, two will grow in the same place. Since you don't have the mechanism to cut many heads off simultaneously, the number of heads will only grow. In this case, our Hydra can start with N
(N ⩾ 1) heads. Let's call the first encounter a generation and we will represent the heads from the first generation as 0, the heads created after the first blow as 1, and so on.
Input
You will be given an integer N
representing how many heads the Hydra initially have and a list of size N
containing in which index (in the examples I will use 0-indexed format) you will cut a head off. You can always assume the indexes given are valid - remember that the list (i.e.: the heads) will grow as you cut heads off.
Example
Input: N = 4
and [0,4,2,5]
Generation 0 - Attack index 0
0 0 0 0 => 1 1 0 0 0
^ ^ ^
Generation 1 - Attack index 4
1 1 0 0 0 => 1 1 0 0 2 2
^ ^ ^
Generation 2 - Attack index 2
1 1 0 0 2 2 => 1 1 3 3 0 2 2
^ ^ ^
Generation 3 - Attack index 5
1 1 3 3 0 2 2 => 1 1 3 3 0 4 4 2
^ ^ ^
Last generation
1 1 3 3 0 4 4 2
As you can see, the indexes given are related to the list of the previous generation.
Output
You are required to output the last generation.
Test cases
N = 1 and [0] => [1,1]
N = 2 and [0,0] => [2,2,1,0]
N = 2 and [0,1] => [1,2,2,0]
N = 2 and [1,0] => [2,2,1,1]
N = 2 and [1,1] => [0,2,2,1]
N = 4 and [0,4,2,5] => [1,1,3,3,0,4,4,2]
N = 6 and [0,0,0,0,0,0] => [6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0]
N = 6 and [5,6,7,8,9,10] => [0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 6]
N = 10 and [1,7,3,12,9,0,15,2,2,10] => [6, 6, 9, 9, 8, 1, 3, 3, 0, 0, 10, 10, 2, 5, 5, 0, 0, 4, 7, 7]
This is code-golf so shortest answer in bytes wins!
code-golf
New contributor
$endgroup$
Instead of being a skillful warrior capable of slaying Hydras (see here and here), this time you are a warrior that has no prior knowledge on how to kill one or which weapons to use against the creature.
In this problem, whenever you cut a single head off, two will grow in the same place. Since you don't have the mechanism to cut many heads off simultaneously, the number of heads will only grow. In this case, our Hydra can start with N
(N ⩾ 1) heads. Let's call the first encounter a generation and we will represent the heads from the first generation as 0, the heads created after the first blow as 1, and so on.
Input
You will be given an integer N
representing how many heads the Hydra initially have and a list of size N
containing in which index (in the examples I will use 0-indexed format) you will cut a head off. You can always assume the indexes given are valid - remember that the list (i.e.: the heads) will grow as you cut heads off.
Example
Input: N = 4
and [0,4,2,5]
Generation 0 - Attack index 0
0 0 0 0 => 1 1 0 0 0
^ ^ ^
Generation 1 - Attack index 4
1 1 0 0 0 => 1 1 0 0 2 2
^ ^ ^
Generation 2 - Attack index 2
1 1 0 0 2 2 => 1 1 3 3 0 2 2
^ ^ ^
Generation 3 - Attack index 5
1 1 3 3 0 2 2 => 1 1 3 3 0 4 4 2
^ ^ ^
Last generation
1 1 3 3 0 4 4 2
As you can see, the indexes given are related to the list of the previous generation.
Output
You are required to output the last generation.
Test cases
N = 1 and [0] => [1,1]
N = 2 and [0,0] => [2,2,1,0]
N = 2 and [0,1] => [1,2,2,0]
N = 2 and [1,0] => [2,2,1,1]
N = 2 and [1,1] => [0,2,2,1]
N = 4 and [0,4,2,5] => [1,1,3,3,0,4,4,2]
N = 6 and [0,0,0,0,0,0] => [6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0]
N = 6 and [5,6,7,8,9,10] => [0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 6]
N = 10 and [1,7,3,12,9,0,15,2,2,10] => [6, 6, 9, 9, 8, 1, 3, 3, 0, 0, 10, 10, 2, 5, 5, 0, 0, 4, 7, 7]
This is code-golf so shortest answer in bytes wins!
code-golf
code-golf
New contributor
New contributor
New contributor
asked 9 hours ago
ihavenoideaihavenoidea
1313 bronze badges
1313 bronze badges
New contributor
New contributor
$begingroup$
Sandbox link
$endgroup$
– ihavenoidea
9 hours ago
$begingroup$
Needs a test case where the initial number of heads is greater than the number of heads cut off. I think I see at least one current answer which would fail that case.
$endgroup$
– Xcali
8 hours ago
$begingroup$
@Xcali The number of heads to cut off is actually guaranteed to be equal to the initial number of heads: You will be given an integerN
(...) and a list of sizeN
(But I missed that part as well when I first read the challenge.) Therefore,N
is simply useless.
$endgroup$
– Arnauld
7 hours ago
$begingroup$
Ah. You're right, I missed it.
$endgroup$
– Xcali
7 hours ago
2
$begingroup$
I thought about actually removingN
from the input since it is "implicitly" given as the array's size. However, I thought the solutions would save bytes by givingN
instead of them relying onarray.size()
or similar.
$endgroup$
– ihavenoidea
7 hours ago
add a comment |
$begingroup$
Sandbox link
$endgroup$
– ihavenoidea
9 hours ago
$begingroup$
Needs a test case where the initial number of heads is greater than the number of heads cut off. I think I see at least one current answer which would fail that case.
$endgroup$
– Xcali
8 hours ago
$begingroup$
@Xcali The number of heads to cut off is actually guaranteed to be equal to the initial number of heads: You will be given an integerN
(...) and a list of sizeN
(But I missed that part as well when I first read the challenge.) Therefore,N
is simply useless.
$endgroup$
– Arnauld
7 hours ago
$begingroup$
Ah. You're right, I missed it.
$endgroup$
– Xcali
7 hours ago
2
$begingroup$
I thought about actually removingN
from the input since it is "implicitly" given as the array's size. However, I thought the solutions would save bytes by givingN
instead of them relying onarray.size()
or similar.
$endgroup$
– ihavenoidea
7 hours ago
$begingroup$
Sandbox link
$endgroup$
– ihavenoidea
9 hours ago
$begingroup$
Sandbox link
$endgroup$
– ihavenoidea
9 hours ago
$begingroup$
Needs a test case where the initial number of heads is greater than the number of heads cut off. I think I see at least one current answer which would fail that case.
$endgroup$
– Xcali
8 hours ago
$begingroup$
Needs a test case where the initial number of heads is greater than the number of heads cut off. I think I see at least one current answer which would fail that case.
$endgroup$
– Xcali
8 hours ago
$begingroup$
@Xcali The number of heads to cut off is actually guaranteed to be equal to the initial number of heads: You will be given an integer
N
(...) and a list of size N
(But I missed that part as well when I first read the challenge.) Therefore, N
is simply useless.$endgroup$
– Arnauld
7 hours ago
$begingroup$
@Xcali The number of heads to cut off is actually guaranteed to be equal to the initial number of heads: You will be given an integer
N
(...) and a list of size N
(But I missed that part as well when I first read the challenge.) Therefore, N
is simply useless.$endgroup$
– Arnauld
7 hours ago
$begingroup$
Ah. You're right, I missed it.
$endgroup$
– Xcali
7 hours ago
$begingroup$
Ah. You're right, I missed it.
$endgroup$
– Xcali
7 hours ago
2
2
$begingroup$
I thought about actually removing
N
from the input since it is "implicitly" given as the array's size. However, I thought the solutions would save bytes by giving N
instead of them relying on array.size()
or similar.$endgroup$
– ihavenoidea
7 hours ago
$begingroup$
I thought about actually removing
N
from the input since it is "implicitly" given as the array's size. However, I thought the solutions would save bytes by giving N
instead of them relying on array.size()
or similar.$endgroup$
– ihavenoidea
7 hours ago
add a comment |
10 Answers
10
active
oldest
votes
$begingroup$
Stax, 12 11 bytes
î╓≡╧▄#¥oWä)A
Run and debug it at staxlang.xyz!
Thanks to recursive for one byte of savings!
Unpacked (13 bytes) and explanation:
z),{i^c&:fFm
z) Push initial array of zeroes to stack
, Push array of attacks to stack
$begingroup$
Japt, 14 bytes
rÈhY[°TT] cUî
Try it
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 61 59 51 bytes
Thanks to @Shaggy for point out that n
is useless, saving 8 bytes in both versions
Expects the array in 0-indexed format. Ignores n
.
a=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
Try it online!
JavaScript (Node.js), 64 56 bytes
Using reduce()
and flat()
:
a=>a.reduce((b,i,g)=>b.flat(1,b[i]=[++g,g]),a.map(_=>0))
Try it online!
$endgroup$
$begingroup$
Woulda=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
work, without takingn
?
$endgroup$
– Shaggy
8 hours ago
$begingroup$
@Shaggy Oops. I missed that part: and a list of sizeN
. So, yeah, it seems liken
IS useless.
$endgroup$
– Arnauld
8 hours ago
add a comment |
$begingroup$
Python 2, 60 bytes
H,a=input()
H=[0]*H
for i in a:H[i:i+1]=[max(H)+1]*2
print H
Try it online!
$endgroup$
1
$begingroup$
Dang it. Beat me by 52 seconds...
$endgroup$
– TFeld
7 hours ago
$begingroup$
@TFeld LOL you tried to avoid an extra variable for the generation too! :D
$endgroup$
– Erik the Outgolfer
7 hours ago
add a comment |
$begingroup$
Python 2, 60 bytes
n,a=input()
h=[0]*n
for c in a:h[c:c+1]=[max(h)+1]*2
print h
Try it online!
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 69 bytes
d+
$*_
r`_G
,0
+`^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
$2$4$.1,$.1
^,+
Try it online! Link includes test cases. 1-indexed. Takes input as ...list,N
. Does not require the list to be of length N
. Explanation:
d+
$*_
Convert all of the inputs to unary, but using _
, so that it doesn't get confused with later uses of the digit 1
. (Retina 1 would do this automatically for a 2-byte saving.)
r`_G
,0
Replace N
with an array of N
zeros, but don't alter the list.
+`
Process all elements of the list.
^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
Find the next element of the list and the equivalent position in the array. $1
= current generation (as a length), $2
= commas from previous generations, $3
= current index - 1, $4
= first $3
heads.
$2$4$.1,$.1
Replace the head at the current index with two copies of the current generation in decimal.
$endgroup$
add a comment |
$begingroup$
Haskell, 63 bytes
foldl g.(0<$)<*>zip[1..];g y(x,n)|(a,_:b)<-splitAt n y=a++x:x:b
Try it online!
$endgroup$
add a comment |
$begingroup$
PHP, 101 bytes
function h($n,$a)$h=array_fill(0,$n,0);foreach($a as$b)array_splice($h,$b,0,$h[$b]=++$x);return $h;
Try it online!
New contributor
$endgroup$
$begingroup$
Welcome! Consider adding an explanation and a link to an online interpreter, such as TIO. Code-only answers are usually automatically marked as low-quality. See existing answers for details.
$endgroup$
– mbomb007
5 hours ago
$begingroup$
I've added a TIO link
$endgroup$
– XMark
4 hours ago
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
);
);
ihavenoidea is a new contributor. Be nice, and check out our Code of Conduct.
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%2f189999%2fattacking-the-hydra%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Stax, 12 11 bytes
î╓≡╧▄#¥oWä)A
Run and debug it at staxlang.xyz!
Thanks to recursive for one byte of savings!
Unpacked (13 bytes) and explanation:
z),{i^c&:fFm
z) Push initial array of zeroes to stack
, Push array of attacks to stack
{ F For each attack, push it and then:
i^c Push [x,x], where x is the generation number
& Set the head at the attack index to this new array
:f Flatten
m Print the last generation
The challenge explicitly says "you are required to output the last generation," so my guess is this consensus doesn't hold here. If it does, though, ten bytes can be managed by leaving the result on an otherwise-empty stack:
z),Fi^c&:f
$endgroup$
1
$begingroup$
0]*
can be replaced withz)
. Edit: Apparently this is undocumented behavior, but pad-left takes its operands in either order. (npm lol)
$endgroup$
– recursive
5 hours ago
$begingroup$
@recursive Undocumented behavior is the best kind of behavior :)
$endgroup$
– Khuldraeseth na'Barya
5 hours ago
add a comment |
$begingroup$
Stax, 12 11 bytes
î╓≡╧▄#¥oWä)A
Run and debug it at staxlang.xyz!
Thanks to recursive for one byte of savings!
Unpacked (13 bytes) and explanation:
z),{i^c&:fFm
z) Push initial array of zeroes to stack
, Push array of attacks to stack
{ F For each attack, push it and then:
i^c Push [x,x], where x is the generation number
& Set the head at the attack index to this new array
:f Flatten
m Print the last generation
The challenge explicitly says "you are required to output the last generation," so my guess is this consensus doesn't hold here. If it does, though, ten bytes can be managed by leaving the result on an otherwise-empty stack:
z),Fi^c&:f
$endgroup$
1
$begingroup$
0]*
can be replaced withz)
. Edit: Apparently this is undocumented behavior, but pad-left takes its operands in either order. (npm lol)
$endgroup$
– recursive
5 hours ago
$begingroup$
@recursive Undocumented behavior is the best kind of behavior :)
$endgroup$
– Khuldraeseth na'Barya
5 hours ago
add a comment |
$begingroup$
Stax, 12 11 bytes
î╓≡╧▄#¥oWä)A
Run and debug it at staxlang.xyz!
Thanks to recursive for one byte of savings!
Unpacked (13 bytes) and explanation:
z),improve this answer
$endgroup$
Japt, 14 bytes
rÈhY[°TT] cUî
Try it
answered 8 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
4,9661 silver badge30 bronze badges
4,9661 silver badge30 bronze badges
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 61 59 51 bytes
Thanks to @Shaggy for point out that n
is useless, saving 8 bytes in both versions
Expects the array in 0-indexed format. Ignores n
.
a=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
Try it online!
JavaScript (Node.js), 64 56 bytes
Using reduce()
and flat()
:
a=>a.reduce((b,i,g)=>b.flat(1,b[i]=[++g,g]),a.map(_=>0))
Try it online!
$endgroup$
$begingroup$
Woulda=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
work, without takingn
?
$endgroup$
– Shaggy
8 hours ago
$begingroup$
@Shaggy Oops. I missed that part: and a list of sizeN
. So, yeah, it seems liken
IS useless.
$endgroup$
– Arnauld
8 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 61 59 51 bytes
Thanks to @Shaggy for point out that n
is useless, saving 8 bytes in both versions
Expects the array in 0-indexed format. Ignores n
.
a=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
Try it online!
JavaScript (Node.js), 64 56 bytes
Using reduce()
and flat()
:
a=>a.reduce((b,i,g)=>b.flat(1,b[i]=[++g,g]),a.map(_=>0))
Try it online!
$endgroup$
$begingroup$
Woulda=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
work, without takingn
?
$endgroup$
– Shaggy
8 hours ago
$begingroup$
@Shaggy Oops. I missed that part: and a list of sizeN
. So, yeah, it seems liken
IS useless.
$endgroup$
– Arnauld
8 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 61 59 51 bytes
Thanks to @Shaggy for point out that n
is useless, saving 8 bytes in both versions
Expects the array in 0-indexed format. Ignores n
.
a=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
Try it online!
JavaScript (Node.js), 64 56 bytes
Using reduce()
and flat()
:
a=>a.reduce((b,i,g)=>b.flat(1,b[i]=[++g,g]),a.map(_=>0))
Try it online!
$endgroup$
JavaScript (ES6), 61 59 51 bytes
Thanks to @Shaggy for point out that n
is useless, saving 8 bytes in both versions
Expects the array in 0-indexed format. Ignores n
.
a=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
Try it online!
JavaScript (Node.js), 64 56 bytes
Using reduce()
and flat()
:
a=>a.reduce((b,i,g)=>b.flat(1,b[i]=[++g,g]),a.map(_=>0))
Try it online!
edited 8 hours ago
answered 9 hours ago
ArnauldArnauld
89.9k7 gold badges104 silver badges366 bronze badges
89.9k7 gold badges104 silver badges366 bronze badges
$begingroup$
Woulda=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
work, without takingn
?
$endgroup$
– Shaggy
8 hours ago
$begingroup$
@Shaggy Oops. I missed that part: and a list of sizeN
. So, yeah, it seems liken
IS useless.
$endgroup$
– Arnauld
8 hours ago
add a comment |
$begingroup$
Woulda=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
work, without takingn
?
$endgroup$
– Shaggy
8 hours ago
$begingroup$
@Shaggy Oops. I missed that part: and a list of sizeN
. So, yeah, it seems liken
IS useless.
$endgroup$
– Arnauld
8 hours ago
$begingroup$
Would
a=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
work, without taking n
?$endgroup$
– Shaggy
8 hours ago
$begingroup$
Would
a=>a.map(i=>b.splice(i,1,++g,g),b=a.map(_=>g=0))&&b
work, without taking n
?$endgroup$
– Shaggy
8 hours ago
$begingroup$
@Shaggy Oops. I missed that part: and a list of size
N
. So, yeah, it seems like n
IS useless.$endgroup$
– Arnauld
8 hours ago
$begingroup$
@Shaggy Oops. I missed that part: and a list of size
N
. So, yeah, it seems like n
IS useless.$endgroup$
– Arnauld
8 hours ago
add a comment |
$begingroup$
Python 2, 60 bytes
H,a=input()
H=[0]*H
for i in a:H[i:i+1]=[max(H)+1]*2
print H
Try it online!
$endgroup$
1
$begingroup$
Dang it. Beat me by 52 seconds...
$endgroup$
– TFeld
7 hours ago
$begingroup$
@TFeld LOL you tried to avoid an extra variable for the generation too! :D
$endgroup$
– Erik the Outgolfer
7 hours ago
add a comment |
$begingroup$
Python 2, 60 bytes
H,a=input()
H=[0]*H
for i in a:H[i:i+1]=[max(H)+1]*2
print H
Try it online!
$endgroup$
1
$begingroup$
Dang it. Beat me by 52 seconds...
$endgroup$
– TFeld
7 hours ago
$begingroup$
@TFeld LOL you tried to avoid an extra variable for the generation too! :D
$endgroup$
– Erik the Outgolfer
7 hours ago
add a comment |
$begingroup$
Python 2, 60 bytes
H,a=input()
H=[0]*H
for i in a:H[i:i+1]=[max(H)+1]*2
print H
Try it online!
$endgroup$
Python 2, 60 bytes
H,a=input()
H=[0]*H
for i in a:H[i:i+1]=[max(H)+1]*2
print H
Try it online!
answered 7 hours ago
Erik the OutgolferErik the Outgolfer
35.5k4 gold badges30 silver badges110 bronze badges
35.5k4 gold badges30 silver badges110 bronze badges
1
$begingroup$
Dang it. Beat me by 52 seconds...
$endgroup$
– TFeld
7 hours ago
$begingroup$
@TFeld LOL you tried to avoid an extra variable for the generation too! :D
$endgroup$
– Erik the Outgolfer
7 hours ago
add a comment |
1
$begingroup$
Dang it. Beat me by 52 seconds...
$endgroup$
– TFeld
7 hours ago
$begingroup$
@TFeld LOL you tried to avoid an extra variable for the generation too! :D
$endgroup$
– Erik the Outgolfer
7 hours ago
1
1
$begingroup$
Dang it. Beat me by 52 seconds...
$endgroup$
– TFeld
7 hours ago
$begingroup$
Dang it. Beat me by 52 seconds...
$endgroup$
– TFeld
7 hours ago
$begingroup$
@TFeld LOL you tried to avoid an extra variable for the generation too! :D
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@TFeld LOL you tried to avoid an extra variable for the generation too! :D
$endgroup$
– Erik the Outgolfer
7 hours ago
add a comment |
$begingroup$
Python 2, 60 bytes
n,a=input()
h=[0]*n
for c in a:h[c:c+1]=[max(h)+1]*2
print h
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 60 bytes
n,a=input()
h=[0]*n
for c in a:h[c:c+1]=[max(h)+1]*2
print h
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 60 bytes
n,a=input()
h=[0]*n
for c in a:h[c:c+1]=[max(h)+1]*2
print h
Try it online!
$endgroup$
Python 2, 60 bytes
n,a=input()
h=[0]*n
for c in a:h[c:c+1]=[max(h)+1]*2
print h
Try it online!
answered 7 hours ago
TFeldTFeld
18.2k3 gold badges14 silver badges57 bronze badges
18.2k3 gold badges14 silver badges57 bronze badges
add a comment |
add a comment |
$begingroup$
Retina 0.8.2, 69 bytes
d+
$*_
r`_G
,0
+`^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
$2$4$.1,$.1
^,+
Try it online! Link includes test cases. 1-indexed. Takes input as ...list,N
. Does not require the list to be of length N
. Explanation:
d+
$*_
Convert all of the inputs to unary, but using _
, so that it doesn't get confused with later uses of the digit 1
. (Retina 1 would do this automatically for a 2-byte saving.)
r`_G
,0
Replace N
with an array of N
zeros, but don't alter the list.
+`
Process all elements of the list.
^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
Find the next element of the list and the equivalent position in the array. $1
= current generation (as a length), $2
= commas from previous generations, $3
= current index - 1, $4
= first $3
heads.
$2$4$.1,$.1
Replace the head at the current index with two copies of the current generation in decimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 69 bytes
d+
$*_
r`_G
,0
+`^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
$2$4$.1,$.1
^,+
Try it online! Link includes test cases. 1-indexed. Takes input as ...list,N
. Does not require the list to be of length N
. Explanation:
d+
$*_
Convert all of the inputs to unary, but using _
, so that it doesn't get confused with later uses of the digit 1
. (Retina 1 would do this automatically for a 2-byte saving.)
r`_G
,0
Replace N
with an array of N
zeros, but don't alter the list.
+`
Process all elements of the list.
^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
Find the next element of the list and the equivalent position in the array. $1
= current generation (as a length), $2
= commas from previous generations, $3
= current index - 1, $4
= first $3
heads.
$2$4$.1,$.1
Replace the head at the current index with two copies of the current generation in decimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 69 bytes
d+
$*_
r`_G
,0
+`^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
$2$4$.1,$.1
^,+
Try it online! Link includes test cases. 1-indexed. Takes input as ...list,N
. Does not require the list to be of length N
. Explanation:
d+
$*_
Convert all of the inputs to unary, but using _
, so that it doesn't get confused with later uses of the digit 1
. (Retina 1 would do this automatically for a 2-byte saving.)
r`_G
,0
Replace N
with an array of N
zeros, but don't alter the list.
+`
Process all elements of the list.
^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
Find the next element of the list and the equivalent position in the array. $1
= current generation (as a length), $2
= commas from previous generations, $3
= current index - 1, $4
= first $3
heads.
$2$4$.1,$.1
Replace the head at the current index with two copies of the current generation in decimal.
$endgroup$
Retina 0.8.2, 69 bytes
d+
$*_
r`_G
,0
+`^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
$2$4$.1,$.1
^,+
Try it online! Link includes test cases. 1-indexed. Takes input as ...list,N
. Does not require the list to be of length N
. Explanation:
d+
$*_
Convert all of the inputs to unary, but using _
, so that it doesn't get confused with later uses of the digit 1
. (Retina 1 would do this automatically for a 2-byte saving.)
r`_G
,0
Replace N
with an array of N
zeros, but don't alter the list.
+`
Process all elements of the list.
^((,*)_)(_)*(.*,,(?<-3>d+,)*)d+
Find the next element of the list and the equivalent position in the array. $1
= current generation (as a length), $2
= commas from previous generations, $3
= current index - 1, $4
= first $3
heads.
$2$4$.1,$.1
Replace the head at the current index with two copies of the current generation in decimal.
answered 6 hours ago
NeilNeil
87.2k8 gold badges46 silver badges183 bronze badges
87.2k8 gold badges46 silver badges183 bronze badges
add a comment |
add a comment |
$begingroup$
Haskell, 63 bytes
foldl g.(0<$)<*>zip[1..];g y(x,n)|(a,_:b)<-splitAt n y=a++x:x:b
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 63 bytes
foldl g.(0<$)<*>zip[1..];g y(x,n)|(a,_:b)<-splitAt n y=a++x:x:b
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 63 bytes
foldl g.(0<$)<*>zip[1..];g y(x,n)|(a,_:b)<-splitAt n y=a++x:x:b
Try it online!
$endgroup$
Haskell, 63 bytes
foldl g.(0<$)<*>zip[1..];g y(x,n)|(a,_:b)<-splitAt n y=a++x:x:b
Try it online!
answered 5 hours ago
B. MehtaB. Mehta
6832 silver badges9 bronze badges
6832 silver badges9 bronze badges
add a comment |
add a comment |
$begingroup$
PHP, 101 bytes
function h($n,$a)$h=array_fill(0,$n,0);foreach($a as$b)array_splice($h,$b,0,$h[$b]=++$x);return $h;
Try it online!
New contributor
$endgroup$
$begingroup$
Welcome! Consider adding an explanation and a link to an online interpreter, such as TIO. Code-only answers are usually automatically marked as low-quality. See existing answers for details.
$endgroup$
– mbomb007
5 hours ago
$begingroup$
I've added a TIO link
$endgroup$
– XMark
4 hours ago
add a comment |
$begingroup$
PHP, 101 bytes
function h($n,$a)$h=array_fill(0,$n,0);foreach($a as$b)array_splice($h,$b,0,$h[$b]=++$x);return $h;
Try it online!
New contributor
$endgroup$
$begingroup$
Welcome! Consider adding an explanation and a link to an online interpreter, such as TIO. Code-only answers are usually automatically marked as low-quality. See existing answers for details.
$endgroup$
– mbomb007
5 hours ago
$begingroup$
I've added a TIO link
$endgroup$
– XMark
4 hours ago
add a comment |
$begingroup$
PHP, 101 bytes
function h($n,$a)$h=array_fill(0,$n,0);foreach($a as$b)array_splice($h,$b,0,$h[$b]=++$x);return $h;
Try it online!
New contributor
$endgroup$
PHP, 101 bytes
function h($n,$a)$h=array_fill(0,$n,0);foreach($a as$b)array_splice($h,$b,0,$h[$b]=++$x);return $h;
Try it online!
New contributor
edited 4 hours ago
New contributor
answered 5 hours ago
XMarkXMark
112 bronze badges
112 bronze badges
New contributor
New contributor
$begingroup$
Welcome! Consider adding an explanation and a link to an online interpreter, such as TIO. Code-only answers are usually automatically marked as low-quality. See existing answers for details.
$endgroup$
– mbomb007
5 hours ago
$begingroup$
I've added a TIO link
$endgroup$
– XMark
4 hours ago
add a comment |
$begingroup$
Welcome! Consider adding an explanation and a link to an online interpreter, such as TIO. Code-only answers are usually automatically marked as low-quality. See existing answers for details.
$endgroup$
– mbomb007
5 hours ago
$begingroup$
I've added a TIO link
$endgroup$
– XMark
4 hours ago
$begingroup$
Welcome! Consider adding an explanation and a link to an online interpreter, such as TIO. Code-only answers are usually automatically marked as low-quality. See existing answers for details.
$endgroup$
– mbomb007
5 hours ago
$begingroup$
Welcome! Consider adding an explanation and a link to an online interpreter, such as TIO. Code-only answers are usually automatically marked as low-quality. See existing answers for details.
$endgroup$
– mbomb007
5 hours ago
$begingroup$
I've added a TIO link
$endgroup$
– XMark
4 hours ago
$begingroup$
I've added a TIO link
$endgroup$
– XMark
4 hours ago
add a comment |
ihavenoidea is a new contributor. Be nice, and check out our Code of Conduct.
ihavenoidea is a new contributor. Be nice, and check out our Code of Conduct.
ihavenoidea is a new contributor. Be nice, and check out our Code of Conduct.
ihavenoidea is a new contributor. Be nice, and check out our Code of Conduct.
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%2f189999%2fattacking-the-hydra%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$
Sandbox link
$endgroup$
– ihavenoidea
9 hours ago
$begingroup$
Needs a test case where the initial number of heads is greater than the number of heads cut off. I think I see at least one current answer which would fail that case.
$endgroup$
– Xcali
8 hours ago
$begingroup$
@Xcali The number of heads to cut off is actually guaranteed to be equal to the initial number of heads: You will be given an integer
N
(...) and a list of sizeN
(But I missed that part as well when I first read the challenge.) Therefore,N
is simply useless.$endgroup$
– Arnauld
7 hours ago
$begingroup$
Ah. You're right, I missed it.
$endgroup$
– Xcali
7 hours ago
2
$begingroup$
I thought about actually removing
N
from the input since it is "implicitly" given as the array's size. However, I thought the solutions would save bytes by givingN
instead of them relying onarray.size()
or similar.$endgroup$
– ihavenoidea
7 hours ago