Não deixe de ver o outro desafio, o mapa de caracteres ASCII reverso !
O conjunto de caracteres ASCII (Código Padrão Americano para Intercâmbio de Informações) é o padrão de codificação de caracteres mais utilizado. Os códigos ASCII representam texto em computadores, equipamentos de telecomunicações e outros dispositivos.
Desafio
Seu desafio é imprimir um mapeamento do conjunto de caracteres ASCII conforme o usuário os insere. GIF:
Depois que o usuário digita todos os caracteres ASCII, a saída deve ficar assim:
Mapeamento
Cada caractere tem uma posição atribuída em uma grade lógica de 16x6, começando com o caractere de espaço na posição superior esquerda e agrupando de forma que o dígito 0 apareça abaixo dele.
Quando a entrada ASCII imprimível for recebida, imprima esse caractere ASCII no local designado da tela sem excluir nenhum caractere atualmente na tela.
Regras
- Seu programa só precisa mapear os caracteres ASCII imprimíveis
0x20
para0x7E
. - Seu programa não deve terminar e continuar a mapear caracteres para a tela até que todos os caracteres ASCII imprimíveis tenham sido inseridos. A partir daqui, seu programa pode terminar ou sair para Neverland.
- Seu programa pode mapear caracteres da maneira que desejar, por exemplo, para uma planilha, tabela, janela do console ou uma janela gráfica.
- Não importa como você exiba o mapeamento, ele deve ser atualizado em tempo real (assim que receber a entrada do usuário).
- Se o seu programa não lê a entrada silenciosamente, ele deve afastar o cursor, para que o texto não atrapalhe o mapa.
Socorro
Aqui está o algoritmo de pseudocódigo que eu usei para gerar o GIF:
loop forever
c = input
y_coord = c / 16
x_coord = c - y * 16
if c is printable
print c at (x_coord * 2 + 1, y_coord + 1)
end if
end loop
Pode haver outra maneira de obter a saída necessária. Você pode optar por usar o meu algoritmo ou o seu, mas a saída deve ser a mesma, independentemente.
Aqui está uma referência de tabela ASCII útil.
Pontuação
A resposta com menos bytes em cada idioma vence. Diverta-se!
Respostas:
QBIC,
5357 bytesAdded 4 bytes for the spacing.
QBIC began development as a shorthand for QBasic, so I thought translating my QBasic answer would demonstrate this nicely. We've saved some 40% in the byte-count for a functionally identical program - and that's even when
LOCATE
,ASC
, andCHR
have no QBIC-functions yet. Fortunately, QBIC can pass code directly to QBasic to compensate for this. A side-by-side:fonte
JavaScript (ES6) + HTML, 114 + 16 = 130 bytes
Saved 16 bytes thanks to @Shaggy
It's so unbelievably satisfying to just mash the keyboard...
fonte
prompt()
within a loop? It will save you from all the event handling and HTML. OP seems to allow it. See the Mathematica post's comments.onkeypress
by itself, allowing you to drop thebody
tag. Also, thepre
tag can be shortened to just<pre id=O
. Although, you'll need to include the closing>
in order for it to work in a Snippet.QBasic 4.5,
8185 bytesAdded 4 bytes to comply with the spacing-rule.
And the output will look like this (NOTE: Old screenshot, now every character is separated by a space):
QBasic has the
LOCATE
command, which comes in handy here. A breakdown of this code:fonte
Java 8, 143 bytes
Uses the ANSI control code
CSI n ; m f
to set the cursor position andConsole.readPassword()
to read the user input silently. Output of some characters:fonte
readPassword()
used like that. Oh, and you seem to be missing a semi-colon after the println. Also, isn't is possible to useSystem.out.printf
somehow instead ofSystem.out.println(String.format(
? And you can change()->
too->
by using an unused empty parameter.BrainFuck, 355 Bytes
BrainFuck's options are pretty limited, so output is in the terminal and the screen is "cleared" with 20 newlines. Input should be the ASCII characters, separated by newlines.
Try it online!
Formatted and Documented
These are the debug notes I used to write the program. I used my interpreter which can optionally print the state of the tape at every '~' character for debugging.
fonte
Mathematica, 108 bytes
Try it online at https://sandbox.open.wolframcloud.com/
When you paste code and press
Shift+Enter
, a dialog box will pop up, you enter"a"
for example for charactera
. The program runs forever.Note: On Wolfram sandbox, the font is formatted differently from that in Mathematica in my computer. So the line/column spacing may looks weird.
fonte
OK
of an input box, another input box appear for you to enter input.Python 2, 115 bytes
Try it online!
Requires quotation marks (single or double) around the inputted characters (the TIO version does not).
fonte
raw_input
toinput
as it is community census that you can assume the input has quotes around it if needed.{
without a matching}
.str, noncompeting, 18 bytes
Presenting my new semi-esoteric language.
fonte
Haskell, 133 bytes
Requires a terminal that understands ANSI escape sequences.
It's shorter to keep a list of all keys pressed so far and clearing the screen before printing all of them each round than turning off the echo in the terminal session. The latter needs
import System.IO
andhSetEcho stdin(2<1)
which costs too many bytes.fonte
C, 101 bytes
This was the program I used to make the graphics. Output is as shown in the GIF. ;)
fonte
QBasic,
6258 bytesTested with QB64. Should work fine on regular QBasic, too, although you may want to modify it to do a
CLS
on the first run.Similar to steenbergh's answer, but uses
INPUT$(1)
to read characters one at a time. This approach is shorter and also displays no prompt. It also usesRUN
for the infinite loop, since we don't have to store any state between iterations except the state of the screen.fonte
input$()
. I like the TIPS-topic too btw.Pascal, 112 chars
As my Mathematica solution takes many bytes in
div
,mod
andToCharacterCode[Input[]]
, I try making another answer with Pascal. But withoutClrScr
my compiler (FPC) left some compile information on the screen.ClrScr;
takes 7 bytes.The
*2
used for proper spacing takes another 2 bytes.fonte
LOGO, 90 bytes
Try it on FMSLogo.
After all, my Logo solution is the shortest, compared with my Mathematica and Pascal answer.
Add 3 bytes if the turtle is required to be hidden.
fonte
6502 machine code + Apple //e ROM, 31 bytes
Hex dump:
Commented assembly:
If the cursor invalidates it, here's a 36-byte version without a cursor:
fonte
Ruby,
797571 + 13 = 84 bytes+13 bytes for
-rio/console
flag.Ungolfed
fonte
SmileBASIC 3, 82 bytes
In the SmileBASIC character set,
¥
is located where\
normally would be; hopefully this doesn't invalidate this answer completely.fonte
Applesoft BASIC, 134 bytes
This is a golfed version of the Apple ][ keyboard test, the program that inspired the challenge.
fonte