| Bytes | Lang | Time | Link |
|---|---|---|---|
| 035 | AWK | 241106T150315Z | xrs |
| 006 | K ngn/k | 240315T144745Z | coltim |
| 007 | Perl 5 + p | 240315T131019Z | Dom Hast |
| 038 | Swift 5.9 | 240315T121711Z | macOSist |
| 009 | Juby | 240315T005428Z | Jordan |
| 006 | Uiua SBCS | 240315T001402Z | chunes |
| 011 | J | 230429T015624Z | south |
| 063 | Scala | 230429T011656Z | 138 Aspe |
| 003 | Japt m | 230403T161214Z | Shaggy |
| nan | 230401T094715Z | The Thon | |
| 019 | Regex PCRE2 | 220814T220536Z | Deadcode |
| 004 | MATL | 220814T174504Z | Luis Men |
| 070 | Rust | 220322T200858Z | Sylveste |
| 001 | Vyxal | 220322T154140Z | Seggan |
| 003 | Vim | 220318T221336Z | Pablocha |
| 007 | Gema | 220318T170350Z | manatwor |
| 021 | Python 3 | 220318T162927Z | EphraimR |
| 055 | /// | 220318T161112Z | Spitemas |
| 019 | Charcoal | 220315T233856Z | jixperso |
| 014 | R | 220318T123639Z | Merijn v |
| 022 | brainfuck | 220318T092902Z | l4m2 |
| 030 | R | 220316T165020Z | Dominic |
| 013 | Octave | 220317T154032Z | Tom Carp |
| 006 | Husk | 220316T155314Z | Dominic |
| 001 | 05AB1E | 220316T082821Z | Kevin Cr |
| 018 | Ruby | 220316T193652Z | AZTECCO |
| 028 | Haskell | 220316T153142Z | lynn |
| 032 | Haskell | 220316T120027Z | AZTECCO |
| 001 | Vyxal | 220315T225403Z | lyxal |
| 057 | Elisp + s.el | 220316T010641Z | jixperso |
| 031 | Python 3 | 220315T220016Z | loopy wa |
| 025 | JavaScript V8 | 220315T230755Z | Matthew |
| 004 | Jelly | 220315T221804Z | Jonathan |
| 060 | Batch | 220315T224722Z | Neil |
| 008 | Charcoal | 220315T224226Z | Neil |
| 003 | Pip | 220315T224035Z | DLosc |
| 006 | Retina 0.8.2 | 220315T223744Z | Neil |
| 033 | C tcc | 220315T222836Z | ovs |
| 025 | Factor + pairrocket | 220315T223504Z | chunes |
| 032 | APL+WIN | 220315T222312Z | Graham |
| 013 | Pyth | 220315T221219Z | sinvec |
| 068 | Lua | 220315T214932Z | twentysi |
| 008 | sed | 220315T210625Z | user3604 |
| 012 | Bash + coreutils | 220315T212855Z | Digital |
K (ngn/k), 7 6 bytes
-1 byte from @algorithmshark
"# "2!
2!categorize each character of the input (" "becomes 0,"#"1)"# "index into this string so 0's become#s and 1's becomes
Perl 5 + -p, 7 bytes
y; #;#
Explanation
Uses the transliteration syntax (y///) but using the implicit ; from -p (-n: while (<STDIN>) {...;}).
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.
J-uby, 9 bytes
Uses A and a (or any letter and its swapped-case counterpart) as the “pixels.”
:swapcase
Uiua SBCS, 6 bytes
⍜(⊛♭)¬
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.
J, 11 bytes
'# '{~'#'&=
'# '{~'#'&=
'#'&= NB. vectorized equality check, returns boolean matrix
{~ NB. index into
'# ' NB. inverted char list
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
}
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
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
MATL, 4 bytes
5\Zc
Input is a char matrix containing and #.
How it works
% Implicit input
5\ % Modulo 5 of (codepoints) of the input chars. This gives 2 for space, 0 for '#'
Zc % Convert nonzeros to '#' and zeros to char(0)
% Implicit display. char(0) is displayed as a space
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.
Vyxal, 1 byte
N
A different Vyxal solution. Expects uppercase letters for # and lowercase letters for or vice versa. N is the swap case element. The header and footer converts the test case format into the format that the program expects and vice versa.
Vim, 3 bytes
VG~
Your output characters must be the same as your input characters (e.g. ["#"," "] -> [" ","#"]), which you are free to choose (but must be printable characters)
We will use the upper case and lower case version of any alphabetic character.
Explanation
V: Select the whole lineG: Go to the end of the document~: Swap case of the characters
Try it online! (Using: ["q","Q"] -> ["Q","q"])
Try it online! (Adding some replaces to fit the caracters in the examples)
Gema, 7 characters
%=.;.=%
For this version choose the % and . characters.
Gema, 10 characters
\#=\ ;\ =#
This uses the original # and characters. Unfortunately this needs escaping because
#is metacharacter similar to*(.*in regex), but recursiveis metacharacter and means one or more whitespace characters (\s+in regex)
Python 3, 21 bytes
lambda s:s.swapcase()
Takes a multiline string consisting of a for spaces and A for #'s and outputs the string after swapping the case of each letter. Not quite as nice-looking (or interesting) as loopy walt's wonderful answer, but I'm happy to finally write an answer to one of these :)
///, 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.
R, 14 bytes
-5 bytes by l4m2, using 1-m instead of abs(m-1) and +11 for providing the required function call.
function(m)1-m
I do personally dislike the freedom of I/O in this challenge and allowing any other input characters than " " and "#". I learned something new from Dominic van Essen's answer actually as I was never aware of that function. But bending the rules to make life easier, nevertheless my answer. Input a 0-1 matrix and just reverse the values.
R, 38 30 bytes
Or 23 bytes by exchanging function for \ using R≥4.1: Attempt This Online!
function(x)chartr("# "," #",x)
Octave, 13 bytes
@(x)['' 67-x]
Takes the input as an array of '# ' characters.
The sum of '#' and ' ' is 67, so all we need to do is subtract the input string from 67 and it will "negate" the characters.
Unfortunately we then have to spend a few bytes forcing the output back to being a character array rather than an integer, but ho hum.
Husk, 6 bytes
†?_aD¶
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.
05AB1E, 1 byte
Each line is a separated potential program:
_
≠
È
I/O as a matrix of 1s and 0s.
Try it online or verify all test cases.
Using the actual # and spaces as I/O, it would be 5 bytes instead:
„# ‡
I/O as multiline strings with #s/spaces and potential newlines.
Try it online or verify all test cases.
Explanation:
# Transform each 1 to 0 and each 0 to 1 in the (implicit) input-matrix,
_ # using an ==0 check
≠ # or !=1 check
È # or %2==0 (is_even) check
# (after which the modified matrix is output implicitly)
„# # Push string "# "
 # Bifurcate it, short for Duplicate & Reverse copy
‡ # Transliterate all "#" to " " and vice-versa in the (implicit) input
# (after which the modified string is output implicitly)
Haskell, 28 bytes
map g
g ' '='#'
g c=min ' 'c
Acts on a string with newlines in it.
By the same rules-lawyering as this Pip answer I guess map$map(1-) is a valid answer, but I don't really like it.
Vyxal, 1 byte
⌐
Same method as DLosc's 3 byte Pip answer. Takes a list of lists of either 0 or 1.
Simply computes 1 - n for each digit. Alternatively,
Vyxal, 3 bytes
₀S*
Takes a multiline string of 0s and 1s. The flag, header and footer are for allowing the test cases to be directly pasted in without having to change them.
Explained
₀S*
₀S # The string "10"
* # Ring translate the input according to that - change 0 to 1, 1 to 0 and leave newlines as they are.
Python 3, 31 bytes
lambda s:s.translate(9*"# \n ")
How
The 9*"# \n " is a string that has a "\n" at position 10 which matches its own code point, and at positions 32 and 35 it has copies of "#" and " " so these two are at each other's code point. str.translate uses this string as a lookup table replacing each character of s by the value associated with its code point. It will leave newlines in place and swap hashes and spaces.
JavaScript (V8), 25 bytes
s=>s.replace(/./g,i=>i^1)
Takes input as string of 0's, 1's, and newlines.
Explanation:
s => s.replace(
/./g, // '.' matches anything but newline, ie. 0 or 1 only.
// 'g' marks regex as global, to replace everything
i => i ^ 1 // JavaScript's aggressive type casting forces '0' or '1' string into a number
// boolean XOR (^) with 1 transforms 0 -> 1 and 1 -> 0
)
Jelly, 4 bytes
O^1Ọ
A monadic Link that accepts a list of lists of characters (from and !) and yields a list of lists of characters with these characters swapped.
How?
O^1Ọ - Link: list of lists of characters
O - ordinal (vectorises) : ' ' -> 32 and '!' -> 33
1 - one : 1
^ - XOR (vectorises) : 32 -> 33 and 33 -> 32
Ọ - character (vectorises) : 33 -> '!' and 32 -> ' '
If integers are allowed in place of characters as I/O then the one byte Link ¬ would suffice (vectorising loginal NOT).
Batch, 60 bytes
@set/ps=
@set s=%s: =$%
@set s=%s:#= %
@echo(%s:$=#%
@%0
Swaps spaces with #s. Doesn't work well with file input as it needs to be interrupted with Ctrl+C to exit. Explanation:
@set/ps=
Read in the next line.
@set s=%s: =$%
Replace the spaces with $s.
@set s=%s:#= %
Replace the #s with spaces.
@echo(%s:$=#%
Replace the $s with #s and output the result.
@%0
Rinse and repeat.
Charcoal, 8 bytes
WS⟦⭆ι¬Σκ
Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings of 0s and 1s. Explanation:
WS
While there are more strings in the input...
⟦⭆ι¬Σκ
... logically invert each character's decimal value and output the result on its own line.
Pip, 3 bytes
1-_
Maybe this is too cheaty?
- The two characters are
0and1 - Submissions may use list of strings instead of multi-line strings, so the input can be a list of rows, each of which is a string containing
0s and1s - But Lists of decimal digits and strings that match
^[0-9]*$are interchangeable, so each row can be a list of digits
The solution is a function that takes and returns a list of lists of 0s and 1s. Attempt This Online!
Explanation: subtract each number from 1.
Here's a 6-byte version that's on much safer ground rules-wise. It's also a function, and it also uses 0 and 1, but it takes and returns a multiline string:
_TRt01
Explanation: Transliterate the characters in 10 (the t builtin) to the characters in 01.
Retina 0.8.2, 6 bytes
T`10`d
Try it online! Swaps 0 with 1 but link is to test suite that converts from # to 01 and back. Explanation: d is shorthand for 0-9 or 0123456789 so this translates 1 to 0 and 0 to 1 (the extra target characters are ignored).
APL+WIN, 32 bytes
Prompts for m x n matrix
n←(⍴m←⎕)⍴' '⋄((,m=' ')/,n)←'#'⋄n
Lua, 68 bytes
a=io.read('*a')b,d=a.gsub,' 'print((b(b(b(a,'#','!'),d,'#'),'!',d)))

