26
\$\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
    Commented Mar 15, 2022 at 20:57
  • 32
    \$\begingroup\$ @Fmbalbuena if you can't find the duplicate then surely it isn't a duplicate? \$\endgroup\$
    – jezza_99
    Commented Mar 15, 2022 at 21:06
  • 3
    \$\begingroup\$ No, but I need help of someone to find the duplicate \$\endgroup\$
    – Fmbalbuena
    Commented Mar 15, 2022 at 21:11
  • 2
    \$\begingroup\$ @Fmbalbuena yeah I did have a look, but couldn't find anything \$\endgroup\$
    – jezza_99
    Commented 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\$ Commented Mar 18, 2022 at 12:12

43 Answers 43

1
2
2
\$\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\$
2
\$\begingroup\$

Uiua SBCS, 6 bytes

⍜(⊛♭)¬

Try it!

What it does is flatten the input, convert it to binary , logical not ¬ it, unbinary it back to characters, then assume the original shape. Under is the modifier that handles all the function inverses.

Or, using Jordan's method,

Uiua SBCS, 1 byte

¯

Just swapcase, but unlike the first answer the letters used must be lower/upper case of the same letter.

Try it!

\$\endgroup\$
2
\$\begingroup\$

J-uby, 9 bytes

Uses A and a (or any letter and its swapped-case counterpart) as the “pixels.”

:swapcase

Attempt This Online!

\$\endgroup\$
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\$ Commented 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\$

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\$
1
\$\begingroup\$

J, 11 bytes

'# '{~'#'&=

Attempt This Online!

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

Perl 5 + -p, 7 bytes

y; #;# 

Try it online!

Explanation

Uses the transliteration syntax (y///) but using the implicit ; from -p (-n: while (<STDIN>) {...;}).

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

K (ngn/k), 7 bytes

" #"@^:

Try it online!

  • ^: check which characters in the input are nulls (i.e. spaces/" ")
  • " #"@ flip the characters by indexing into " #" (0's become s, 1's become #s)
\$\endgroup\$
0
\$\begingroup\$

Swift 5.9, 38 bytes

let f={($0+[]).map{($0+[]).map{1-$0}}}

f(_:)'s type is ([[Int]]) -> [[Int]]. The two "characters" are 1 and 0.

\$\endgroup\$
1
2

Your Answer

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

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