Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Expand a greyscale/colour hex code

+2
−0

Consider a type of hexadecimal colour code that supports shorthand for both greyscale and colour:

  • A 6 digit code is interpreted as 2 digits for red, followed by 2 for green, then 2 for blue.
  • A 2 digit code is greyscale. The 2 digits are used for each of red, green, and blue.
  • A 3 digit code is shorthand for a 6 digit code with repeated digits for red, green, and blue.
  • A 1 digit code is shorthand for a 2 digit greyscale code with the same 1 digit repeated.

Input

  • A colour code in the format "#" followed by 1, 2, 3, or 6 hexadecimal digits.
  • You may take input as either a string or any ordered sequence data type.

Output

  • A colour code in the format "#" followed by 6 hexadecimal digits.
  • The output after the "#" depends on the number of hexadecimal digits in the input as follows:
    • Input with 1 digit: output the 1 digit 6 times.
    • Input with 2 digits: output the 2 digit string 3 times.
    • Input with 3 digits: output each digit twice (rather than the 3 digit string twice).
    • Input with 6 digits: output the same 6 digits.
  • You may output as either a string or any ordered sequence data type.

Test cases

Test cases are in the format Input : Output.

#0 : #000000
#A : #AAAAAA
#2E : #2E2E2E
#44 : #444444
#ACF : #AACCFF
#989 : #998899
#777 : #777777
#012345 : #012345
#BBBBBB : #BBBBBB

Scoring

This is a code golf challenge. Your score is the number of bytes in your code.

Explanations are optional, but I'm more likely to upvote answers that have one.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

5 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+2
−0

x86-64 machine code, 32 bytes

A4 FF CA 6A 06 58 F6 F2 50 3C 03 59 51 74 08 AC F3 AA FF CA 75 F5 F3 66 AD 66 F3 AB 58 B0 07 C3

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), takes the address and length of the input string (in ASCII) in RSI and EDX respectively, and writes the output string to an address given in RDI and returns its length in EAX.

In assembly:

f:	movsb     # Put the initial '#' from the input string
	          #  into the output string, advancing both pointers.
	dec edx   # Subtract 1 from the length in EDX.
	push 6; pop rax # Set EAX to 6.
	div dl    # Divide AH:AL (EAX's low 2 bytes) by the low byte of EDX.
	          # The quotient goes into AL and the remainder (0) into AX.
	          # EAX now equals the quotient: the number of repetitions.
	push rax  # Save that number onto the stack.
	cmp al, 3 # Compare AL to 3.
r:	pop rcx; push rcx # Put the no. of repetitions into RCX from the stack.
	je s      # Jump if AL equaled 3 (special case for 2 digits);
	          #  in later iterations, no jump (ZF=0 from the DEC).
	lodsb     # Load a byte from the input string into AL, advancing RSI.
	rep stosb # Write AL to the output string RCX times,
	          #  advancing RDI and counting down RCX to 0.
	dec edx   # Decrease EDX by 1.
	jnz r     # Jump back, to repeat, if it is nonzero.
	rep       # As RCX=0, this prefix makes the following LODSW do nothing.
s:	lodsw     # Load 2 bytes from the input string into AX, advancing RSI.
	rep stosw # Write AX to the output string RCX times,
	          #  advancing RDI and counting down RCX to 0.
	pop rax   # Take the number of repetitions off the stack into RAX.
	mov al, 7 # Set the low byte of EAX to 7, which makes EAX 7.
	ret       # Return.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Japt, 13 12 bytes

Î+6îUÅË+pUÊv

Try it (includes all test cases)

Î+6îUÅË+pUÊv     :Implicit input of string U
Î                :First character
 +               :Append
  6î             :  Mold to length 6
    UÅ           :    Slice the first character off U
      Ë          :    Map each D
       +p        :      Append D repeated
         UÊ      :        Length of U
           v     :        Is divisible by 2 (returns 0 or 1)
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Q/KDB+, 39 bytes

{"#",?[4=count x;x 1 1 2 2 3 3;6#1_x]}

Testing the function:

q)cc:{"#",?[4=count x;x 1 1 2 2 3 3;6#1_x]}
q)cc "#ABCDEF"
"#ABCDEF"
q)cc "#A"
"#AAAAAA"
q)cc "#AB"
"#ABABAB"
q)cc "#ABC"
"#AABBCC"

q) is the prompt, other lines are the outputs. Instead of = the assignment operator is : in Q.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Haskell + hgl, 32 24 bytes

tlM$tk6<cy<fiI(rl2~<)eL3

Explanation

  • tlM map a over the tail of the input ...
  • fiI if ...
  • el3 the length is 3
  • (rl2~<) repeat each letter in place twice.
  • cy cycle the list indefinitely
  • tk6 take the first 6 characters.

Reflection

hgl is supposed to be good at structural manipulations like this, so this answer feels a little too long. I think a big part of the issue is that it involves a lot of conditional behavior which is not so robust in hgl.

So I have the following suggestions:

  • fiI, and its partner lii, should have infix variants. This wouldn't actually save any bytes here, but it's a good idea.
  • Odd length (od<l) and even length (ev<l) should have shortcuts. This would have saved an impressive 3 bytes on an earlier version of this answer, but using the odd length shortcut on this answer would tie.
  • There should be a flip of cz, and it should probably be two bytes. If this existed I could do the extremely weird:
    tlM$cZ W6<fiI(rl2~<)eL3
    
    This reshapes the string to the size of the built-in string "aeiouy" as a substitute for tk6<cy. It would save 1 byte.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

APL (Dyalog Classic), 31 bytes

'#',{4=≢⍵:⍵[2 2 3 3 4 4]⋄6⍴1↓⍵}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Is assignment part of the code? (3 comments)

Sign up to answer this question »