25
\$\begingroup\$

Task

Given an \$m\times n\$ binary ascii "photo", return the negative of the photo

Example:

  #  
 # # 
#   #
 # # 
  # 
->
## ##
# # #
 ### 
# # #
## ##

General rules:

  • This is , so the shortest answer in bytes wins
  • Standard rules and default I/O rules apply
  • Your output characters must be the same as your input characters (e.g. ["#"," "] -> [" ","#"]), which you are free to choose (but must be printable characters)
  • Your output photo must have the same \$m\times n\$ dimensions as the input
  • Trailing whitespace is allowed

Test cases

Input 1:

  #  
 # # 
#   #
 # # 
  # 

Output 1:

## ##
# # #
 ### 
# # #
## ##

Input 2:

    # #
  #####
  # #  
#####  
# #    

Output 2:

#### # 
##     
## # ##
     ##
 # ####

Input 3:

##############################################################################################
#  ###  ##       ##  #######  #######       ######  ###  ##       ##       ##  #######      ##
#  ###  ##  #######  #######  #######  ###  ######  ###  ##  ###  ##  ###  ##  #######  ###  #
#       ##     ####  #######  #######  ###  ######  # #  ##  ###  ##       ##  #######  ###  #
#  ###  ##  #######  #######  #######  ###  ######   #   ##  ###  ##  #  ####  #######  ###  #
#  ###  ##       ##       ##       ##       ######  ###  ##       ##  ###  ##       ##      ##
##############################################################################################

Output 3:

 ##   ##  #######  ##       ##       #######      ##   ##  #######  #######  ##       ######  
 ##   ##  ##       ##       ##       ##   ##      ##   ##  ##   ##  ##   ##  ##       ##   ## 
 #######  #####    ##       ##       ##   ##      ## # ##  ##   ##  #######  ##       ##   ## 
 ##   ##  ##       ##       ##       ##   ##      ### ###  ##   ##  ## ##    ##       ##   ## 
 ##   ##  #######  #######  #######  #######      ##   ##  #######  ##   ##  #######  ######  
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Duplicate, not sure which one \$\endgroup\$
    – Fmbalbuena
    Mar 15, 2022 at 20:57
  • 30
    \$\begingroup\$ @Fmbalbuena if you can't find the duplicate then surely it isn't a duplicate? \$\endgroup\$
    – jezza_99
    Mar 15, 2022 at 21:06
  • 3
    \$\begingroup\$ No, but I need help of someone to find the duplicate \$\endgroup\$
    – Fmbalbuena
    Mar 15, 2022 at 21:11
  • 2
    \$\begingroup\$ @Fmbalbuena yeah I did have a look, but couldn't find anything \$\endgroup\$
    – jezza_99
    Mar 15, 2022 at 21:13
  • 2
    \$\begingroup\$ I think rules are kind of blurry as any input is allowed and any input characters is valid? so that makes a 01 matrix valid input? leaving it to |m-1|? \$\endgroup\$ Mar 18, 2022 at 12:12

38 Answers 38

1
2
1
\$\begingroup\$

Husk, 6 bytes

†?_aD¶

Try it online!

Input & output as single multiline string; 'pixels' are 'A' and 'a' (or any other pair of uppercase & lowercase letters).

Split on newlines (), then, for each () character, if (?) it's uppercase (D) convert to lowercase (_), otherwise convert to uppercase (a).

If input is already list of strings, we can drop the for 5 bytes. If input is a list of integers (1 and zero) we can have 2 bytes.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ After running some tests, I can definitively say its all just yelling at me. \$\endgroup\$ Mar 16, 2022 at 16:12
1
\$\begingroup\$

///, 55 bytes

/
 /
> //
#/
>#// #/ >#//# /#> //>#/->//> /#>//-/ //>//

Man, the underscore on the /// link looks bad. Ah, well.

Basically, we first put a cursor (>) before every run of hashes or spaces, then flip each character after a >. We need to use an intermediate character (-) to avoid flipping things twice.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Rust, 70 bytes

|s:&str|->String{s.replace("#","?").replace(" ","#").replace("?"," ")}

This replaces all occurrences of # with ?, replaces all with #, and then all ? with . It's quite an effective algorithm.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Regex (PCRE2), 19 bytes

s/(#)| /${1:+ :#}/g

Try it online!
Try it on regex101!

This is a single regex substitution, to be applied once.

In this explanation, indicates a space:

s/         # Match the following:
    (#)    # \1 = match one "#"
|          # or
    ␣      # match one space
/          # Replace with the following:
${1:+␣:#}  # If \1 is set, replace with a space, else replace with "#"
/g         # Enable Global replacement flag

Regex (Boost), 15 bytes

s/(#)| /?1 :#/g

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Thunno, \$ 2 \log_{256}(96) \approx \$ 1.65 bytes

ZX

Attempt This Online! or see it with # and

Port of Seggan's Vyxal answer. Uses a and A as the two characters.

Thunno N, \$ 10 \log_{256}(96) \approx \$ 8.23 bytes

A\Ziedi1_J

Attempt This Online! or see it with # and

Uses 0 and 1 as the two characters.

Explanation

ZX  # Implicit input
ZX  # Swap case of input
    # Implicit output
A\Ziedi1_J  # Implicit input
A\          # Push a newline
  Zi        # Split the input on newlines
    e       # Map over this list:
     di     #  Convert each character to an integer
       1_   #  Subtract each from one
         J  #  Join the resulting list
            # N flag joins on newlines
            # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Japt -m, 3 bytes

I/O as an array of binary strings.

mèT

Try it

\$\endgroup\$
1
\$\begingroup\$

Scala, 63 bytes

Golfed version. Try it online!

def f(s:String)=s.map(c=>if(c=='#')' 'else if(c==' ')'#'else c)

Ungolfed version. Try it online!

def f(s: String): String = s.map {
  case '#' => ' '
  case ' ' => '#'
  case c   => c
}
\$\endgroup\$
0
\$\begingroup\$

J, 11 bytes

'# '{~'#'&=

Attempt This Online!

'# '{~'#'&=
      '#'&=  NB. vectorized equality check, returns boolean matrix
    {~       NB. index into
'# '         NB. inverted char list
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.