title: String to ASCII Art Text

text:
# Question
Title: String to ASCII Art Text
# The Task

You are to write some ASCII Art Text as seen from this website for generating ASCII art!

# Rules

Input will only be alphabetical text and spaces, and will be input as a single line. It is also case-insensitive.

You needn't fix if the output text is longer than the terminal width. As long as it would be correct if the terminal window was infinitely stretchable, you'll be fine.

Aside from the standard-loopholes, the only other rule is that you mayn't use built-ins - although if your language *has* a built in for this (which would be *amazing*), if you include it as a side-note in your answer, take a 15% bonus off of your code-golfed solution!

### Example:

Input:

```
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
```

Output:

```
          _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____            _____                    _____                   _______                   _____                   _______                   _____                    _____                _____                    _____                    _____                    _____                                        _____                    _____                            _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____                    _____            _____                    _____                   _______                   _____                   _______                   _____                    _____                _____                    _____                    _____                    _____                                        _____                    _____
         /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \          /\    \                  /\    \                 /::\    \                 /\    \                 /::\    \                 /\    \                  /\    \              /\    \                  /\    \                  /\    \                  /\    \                 ______               |\    \                  /\    \                          /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \                  /\    \          /\    \                  /\    \                 /::\    \                 /\    \                 /::\    \                 /\    \                  /\    \              /\    \                  /\    \                  /\    \                  /\    \                 ______               |\    \                  /\    \
        /::\    \                /::\    \                /::\    \                /::\    \                /::\    \                /::\    \                /::\    \                /::\____\                /::\    \                /::\    \                /::\____\                /::\____\        /::\____\                /::\____\               /::::\    \               /::\    \               /::::\    \               /::\    \                /::\    \            /::\    \                /::\____\                /::\____\                /::\____\               |::|   |              |:\____\                /::\    \                        /::\    \                /::\    \                /::\    \                /::\    \                /::\    \                /::\    \                /::\    \                /::\____\                /::\    \                /::\    \                /::\____\                /::\____\        /::\____\                /::\____\               /::::\    \               /::\    \               /::::\    \               /::\    \                /::\    \            /::\    \                /::\____\                /::\____\                /::\____\               |::|   |              |:\____\                /::\    \
       /::::\    \              /::::\    \              /::::\    \              /::::\    \              /::::\    \              /::::\    \              /::::\    \              /:::/    /                \:::\    \               \:::\    \              /:::/    /               /:::/    /       /::::|   |               /::::|   |              /::::::\    \             /::::\    \             /::::::\    \             /::::\    \              /::::\    \           \:::\    \              /:::/    /               /:::/    /               /:::/    /               |::|   |              |::|   |                \:::\    \                      /::::\    \              /::::\    \              /::::\    \              /::::\    \              /::::\    \              /::::\    \              /::::\    \              /:::/    /                \:::\    \               \:::\    \              /:::/    /               /:::/    /       /::::|   |               /::::|   |              /::::::\    \             /::::\    \             /::::::\    \             /::::\    \              /::::\    \           \:::\    \              /:::/    /               /:::/    /               /:::/    /               |::|   |              |::|   |                \:::\    \
      /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /:::/    /                  \:::\    \               \:::\    \            /:::/    /               /:::/    /       /:::::|   |              /:::::|   |             /::::::::\    \           /::::::\    \           /::::::::\    \           /::::::\    \            /::::::\    \           \:::\    \            /:::/    /               /:::/    /               /:::/   _/___             |::|   |              |::|   |                 \:::\    \                    /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /::::::\    \            /:::/    /                  \:::\    \               \:::\    \            /:::/    /               /:::/    /       /:::::|   |              /:::::|   |             /::::::::\    \           /::::::\    \           /::::::::\    \           /::::::\    \            /::::::\    \           \:::\    \            /:::/    /               /:::/    /               /:::/   _/___             |::|   |              |::|   |                 \:::\    \
     /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/    /                    \:::\    \               \:::\    \          /:::/    /               /:::/    /       /::::::|   |             /::::::|   |            /:::/~~\:::\    \         /:::/\:::\    \         /:::/~~\:::\    \         /:::/\:::\    \          /:::/\:::\    \           \:::\    \          /:::/    /               /:::/    /               /:::/   /\    \            |::|   |              |::|   |                  \:::\    \                  /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/\:::\    \          /:::/    /                    \:::\    \               \:::\    \          /:::/    /               /:::/    /       /::::::|   |             /::::::|   |            /:::/~~\:::\    \         /:::/\:::\    \         /:::/~~\:::\    \         /:::/\:::\    \          /:::/\:::\    \           \:::\    \          /:::/    /               /:::/    /               /:::/   /\    \            |::|   |              |::|   |                  \:::\    \
    /:::/__\:::\    \        /:::/__\:::\    \        /:::/  \:::\    \        /:::/  \:::\    \        /:::/__\:::\    \        /:::/__\:::\    \        /:::/  \:::\    \        /:::/____/                      \:::\    \               \:::\    \        /:::/____/               /:::/    /       /:::/|::|   |            /:::/|::|   |           /:::/    \:::\    \       /:::/__\:::\    \       /:::/    \:::\    \       /:::/__\:::\    \        /:::/__\:::\    \           \:::\    \        /:::/    /               /:::/____/               /:::/   /::\____\           |::|   |              |::|   |                   \:::\    \                /:::/__\:::\    \        /:::/__\:::\    \        /:::/  \:::\    \        /:::/  \:::\    \        /:::/__\:::\    \        /:::/__\:::\    \        /:::/  \:::\    \        /:::/____/                      \:::\    \               \:::\    \        /:::/____/               /:::/    /       /:::/|::|   |            /:::/|::|   |           /:::/    \:::\    \       /:::/__\:::\    \       /:::/    \:::\    \       /:::/__\:::\    \        /:::/__\:::\    \           \:::\    \        /:::/    /               /:::/____/               /:::/   /::\____\           |::|   |              |::|   |                   \:::\    \
   /::::\   \:::\    \      /::::\   \:::\    \      /:::/    \:::\    \      /:::/    \:::\    \      /::::\   \:::\    \      /::::\   \:::\    \      /:::/    \:::\    \      /::::\    \                      /::::\    \              /::::\    \      /::::\    \              /:::/    /       /:::/ |::|   |           /:::/ |::|   |          /:::/    / \:::\    \     /::::\   \:::\    \     /:::/    / \:::\    \     /::::\   \:::\    \       \:::\   \:::\    \          /::::\    \      /:::/    /                |::|    |               /:::/   /:::/    /           |::|   |              |::|   |                    \:::\    \              /::::\   \:::\    \      /::::\   \:::\    \      /:::/    \:::\    \      /:::/    \:::\    \      /::::\   \:::\    \      /::::\   \:::\    \      /:::/    \:::\    \      /::::\    \                      /::::\    \              /::::\    \      /::::\    \              /:::/    /       /:::/ |::|   |           /:::/ |::|   |          /:::/    / \:::\    \     /::::\   \:::\    \     /:::/    / \:::\    \     /::::\   \:::\    \       \:::\   \:::\    \          /::::\    \      /:::/    /                |::|    |               /:::/   /:::/    /           |::|   |              |::|   |                    \:::\    \
  /::::::\   \:::\    \    /::::::\   \:::\    \    /:::/    / \:::\    \    /:::/    / \:::\    \    /::::::\   \:::\    \    /::::::\   \:::\    \    /:::/    / \:::\    \    /::::::\    \   _____    ____    /::::::\    \    _____   /::::::\    \    /::::::\____\________    /:::/    /       /:::/  |::|___|______    /:::/  |::|   | _____   /:::/____/   \:::\____\   /::::::\   \:::\    \   /:::/____/   \:::\____\   /::::::\   \:::\    \    ___\:::\   \:::\    \        /::::::\    \    /:::/    /      _____      |::|    |     _____    /:::/   /:::/   _/___         |::|   |              |::|___|______               \:::\    \            /::::::\   \:::\    \    /::::::\   \:::\    \    /:::/    / \:::\    \    /:::/    / \:::\    \    /::::::\   \:::\    \    /::::::\   \:::\    \    /:::/    / \:::\    \    /::::::\    \   _____    ____    /::::::\    \    _____   /::::::\    \    /::::::\____\________    /:::/    /       /:::/  |::|___|______    /:::/  |::|   | _____   /:::/____/   \:::\____\   /::::::\   \:::\    \   /:::/____/   \:::\____\   /::::::\   \:::\    \    ___\:::\   \:::\    \        /::::::\    \    /:::/    /      _____      |::|    |     _____    /:::/   /:::/   _/___         |::|   |              |::|___|______               \:::\    \
 /:::/\:::\   \:::\    \  /:::/\:::\   \:::\ ___\  /:::/    /   \:::\    \  /:::/    /   \:::\ ___\  /:::/\:::\   \:::\    \  /:::/\:::\   \:::\    \  /:::/    /   \:::\ ___\  /:::/\:::\    \ /\    \  /\   \  /:::/\:::\    \  /\    \ /:::/\:::\    \  /:::/\:::::::::::\    \  /:::/    /       /:::/   |::::::::\    \  /:::/   |::|   |/\    \ |:::|    |     |:::|    | /:::/\:::\   \:::\____\ |:::|    |     |:::|    | /:::/\:::\   \:::\____\  /\   \:::\   \:::\    \      /:::/\:::\    \  /:::/____/      /\    \     |::|    |    /\    \  /:::/___/:::/   /\    \  ______|::|___|___ ____      /::::::::\    \               \:::\    \          /:::/\:::\   \:::\    \  /:::/\:::\   \:::\ ___\  /:::/    /   \:::\    \  /:::/    /   \:::\ ___\  /:::/\:::\   \:::\    \  /:::/\:::\   \:::\    \  /:::/    /   \:::\ ___\  /:::/\:::\    \ /\    \  /\   \  /:::/\:::\    \  /\    \ /:::/\:::\    \  /:::/\:::::::::::\    \  /:::/    /       /:::/   |::::::::\    \  /:::/   |::|   |/\    \ |:::|    |     |:::|    | /:::/\:::\   \:::\____\ |:::|    |     |:::|    | /:::/\:::\   \:::\____\  /\   \:::\   \:::\    \      /:::/\:::\    \  /:::/____/      /\    \     |::|    |    /\    \  /:::/___/:::/   /\    \  ______|::|___|___ ____      /::::::::\    \               \:::\    \
/:::/  \:::\   \:::\____\/:::/__\:::\   \:::|    |/:::/____/     \:::\____\/:::/____/     \:::|    |/:::/__\:::\   \:::\____\/:::/  \:::\   \:::\____\/:::/____/  ___\:::|    |/:::/  \:::\    /::\____\/::\   \/:::/  \:::\____\/::\    /:::/  \:::\____\/:::/  |:::::::::::\____\/:::/____/       /:::/    |:::::::::\____\/:: /    |::|   /::\____\|:::|____|     |:::|    |/:::/  \:::\   \:::|    ||:::|____|     |:::|____|/:::/  \:::\   \:::|    |/::\   \:::\   \:::\____\    /:::/  \:::\____\|:::|    /      /::\____\    |::|    |   /::\____\|:::|   /:::/   /::\____\|:::::::::::::::::|    |    /::::::::::\____\_______________\:::\____\        /:::/  \:::\   \:::\____\/:::/__\:::\   \:::|    |/:::/____/     \:::\____\/:::/____/     \:::|    |/:::/__\:::\   \:::\____\/:::/  \:::\   \:::\____\/:::/____/  ___\:::|    |/:::/  \:::\    /::\____\/::\   \/:::/  \:::\____\/::\    /:::/  \:::\____\/:::/  |:::::::::::\____\/:::/____/       /:::/    |:::::::::\____\/:: /    |::|   /::\____\|:::|____|     |:::|    |/:::/  \:::\   \:::|    ||:::|____|     |:::|____|/:::/  \:::\   \:::|    |/::\   \:::\   \:::\____\    /:::/  \:::\____\|:::|    /      /::\____\    |::|    |   /::\____\|:::|   /:::/   /::\____\|:::::::::::::::::|    |    /::::::::::\____\_______________\:::\____\
\::/    \:::\  /:::/    /\:::\   \:::\  /:::|____|\:::\    \      \::/    /\:::\    \     /:::|____|\:::\   \:::\   \::/    /\::/    \:::\   \::/    /\:::\    \ /\  /:::|____|\::/    \:::\  /:::/    /\:::\  /:::/    \::/    /\:::\  /:::/    \::/    /\::/   |::|~~~|~~~~~     \:::\    \       \::/    / ~~~~~/:::/    /\::/    /|::|  /:::/    / \:::\    \   /:::/    / \::/    \:::\  /:::|____| \:::\   _\___/:::/    / \::/   |::::\  /:::|____|\:::\   \:::\   \::/    /   /:::/    \::/    /|:::|____\     /:::/    /    |::|    |  /:::/    /|:::|__/:::/   /:::/    /|:::::::::::::::::|____|   /:::/~~~~/~~      \::::::::::::::::::/    /        \::/    \:::\  /:::/    /\:::\   \:::\  /:::|____|\:::\    \      \::/    /\:::\    \     /:::|____|\:::\   \:::\   \::/    /\::/    \:::\   \::/    /\:::\    \ /\  /:::|____|\::/    \:::\  /:::/    /\:::\  /:::/    \::/    /\:::\  /:::/    \::/    /\::/   |::|~~~|~~~~~     \:::\    \       \::/    / ~~~~~/:::/    /\::/    /|::|  /:::/    / \:::\    \   /:::/    / \::/    \:::\  /:::|____| \:::\   _\___/:::/    / \::/   |::::\  /:::|____|\:::\   \:::\   \::/    /   /:::/    \::/    /|:::|____\     /:::/    /    |::|    |  /:::/    /|:::|__/:::/   /:::/    /|:::::::::::::::::|____|   /:::/~~~~/~~      \::::::::::::::::::/    /
 \/____/ \:::\/:::/    /  \:::\   \:::\/:::/    /  \:::\    \      \/____/  \:::\    \   /:::/    /  \:::\   \:::\   \/____/  \/____/ \:::\   \/____/  \:::\    /::\ \::/    /  \/____/ \:::\/:::/    /  \:::\/:::/    / \/____/  \:::\/:::/    / \/____/  \/____|::|   |           \:::\    \       \/____/      /:::/    /  \/____/ |::| /:::/    /   \:::\    \ /:::/    /   \/_____/\:::\/:::/    /   \:::\ |::| /:::/    /   \/____|:::::\/:::/    /  \:::\   \:::\   \/____/   /:::/    / \/____/  \:::\    \   /:::/    /     |::|    | /:::/    /  \:::\/:::/   /:::/    /  ~~~~~~|::|~~~|~~~        /:::/    /          \::::::::::::::::/____/          \/____/ \:::\/:::/    /  \:::\   \:::\/:::/    /  \:::\    \      \/____/  \:::\    \   /:::/    /  \:::\   \:::\   \/____/  \/____/ \:::\   \/____/  \:::\    /::\ \::/    /  \/____/ \:::\/:::/    /  \:::\/:::/    / \/____/  \:::\/:::/    / \/____/  \/____|::|   |           \:::\    \       \/____/      /:::/    /  \/____/ |::| /:::/    /   \:::\    \ /:::/    /   \/_____/\:::\/:::/    /   \:::\ |::| /:::/    /   \/____|:::::\/:::/    /  \:::\   \:::\   \/____/   /:::/    / \/____/  \:::\    \   /:::/    /     |::|    | /:::/    /  \:::\/:::/   /:::/    /  ~~~~~~|::|~~~|~~~        /:::/    /          \::::::::::::::::/____/
          \::::::/    /    \:::\   \::::::/    /    \:::\    \               \:::\    \ /:::/    /    \:::\   \:::\    \               \:::\    \       \:::\   \:::\ \/____/            \::::::/    /    \::::::/    /            \::::::/    /                 |::|   |            \:::\    \                  /:::/    /           |::|/:::/    /     \:::\    /:::/    /             \::::::/    /     \:::\|::|/:::/    /          |:::::::::/    /    \:::\   \:::\    \      /:::/    /            \:::\    \ /:::/    /      |::|____|/:::/    /    \::::::/   /:::/    /         |::|   |          /:::/    /            \:::\~~~~\~~~~~~                         \::::::/    /    \:::\   \::::::/    /    \:::\    \               \:::\    \ /:::/    /    \:::\   \:::\    \               \:::\    \       \:::\   \:::\ \/____/            \::::::/    /    \::::::/    /            \::::::/    /                 |::|   |            \:::\    \                  /:::/    /           |::|/:::/    /     \:::\    /:::/    /             \::::::/    /     \:::\|::|/:::/    /          |:::::::::/    /    \:::\   \:::\    \      /:::/    /            \:::\    \ /:::/    /      |::|____|/:::/    /    \::::::/   /:::/    /         |::|   |          /:::/    /            \:::\~~~~\~~~~~~
           \::::/    /      \:::\   \::::/    /      \:::\    \               \:::\    /:::/    /      \:::\   \:::\____\               \:::\____\       \:::\   \:::\____\               \::::/    /      \::::/____/              \::::/    /                  |::|   |             \:::\    \                /:::/    /            |::::::/    /       \:::\__/:::/    /               \::::/    /       \::::::::::/    /           |::|\::::/    /      \:::\   \:::\____\    /:::/    /              \:::\    /:::/    /       |:::::::::::/    /      \::::/___/:::/    /          |::|   |         /:::/    /              \:::\    \                               \::::/    /      \:::\   \::::/    /      \:::\    \               \:::\    /:::/    /      \:::\   \:::\____\               \:::\____\       \:::\   \:::\____\               \::::/    /      \::::/____/              \::::/    /                  |::|   |             \:::\    \                /:::/    /            |::::::/    /       \:::\__/:::/    /               \::::/    /       \::::::::::/    /           |::|\::::/    /      \:::\   \:::\____\    /:::/    /              \:::\    /:::/    /       |:::::::::::/    /      \::::/___/:::/    /          |::|   |         /:::/    /              \:::\    \
           /:::/    /        \:::\  /:::/    /        \:::\    \               \:::\  /:::/    /        \:::\   \::/    /                \::/    /        \:::\  /:::/    /               /:::/    /        \:::\    \               \::/    /                   |::|   |              \:::\    \              /:::/    /             |:::::/    /         \::::::::/    /                 \::/____/         \::::::::/    /            |::| \::/____/        \:::\  /:::/    /    \::/    /                \:::\__/:::/    /        \::::::::::/____/        \:::\__/:::/    /           |::|   |         \::/    /                \:::\    \                              /:::/    /        \:::\  /:::/    /        \:::\    \               \:::\  /:::/    /        \:::\   \::/    /                \::/    /        \:::\  /:::/    /               /:::/    /        \:::\    \               \::/    /                   |::|   |              \:::\    \              /:::/    /             |:::::/    /         \::::::::/    /                 \::/____/         \::::::::/    /            |::| \::/____/        \:::\  /:::/    /    \::/    /                \:::\__/:::/    /        \::::::::::/____/        \:::\__/:::/    /           |::|   |         \::/    /                \:::\    \
          /:::/    /          \:::\/:::/    /          \:::\    \               \:::\/:::/    /          \:::\   \/____/                  \/____/          \:::\/:::/    /               /:::/    /          \:::\    \               \/____/                    |::|   |               \:::\    \            /:::/    /              |::::/    /           \::::::/    /                   ~~                \::::::/    /             |::|  ~|               \:::\/:::/    /      \/____/                  \::::::::/    /          ~~~~~~~~~~               \::::::::/    /            |::|   |          \/____/                  \:::\    \                            /:::/    /          \:::\/:::/    /          \:::\    \               \:::\/:::/    /          \:::\   \/____/                  \/____/          \:::\/:::/    /               /:::/    /          \:::\    \               \/____/                    |::|   |               \:::\    \            /:::/    /              |::::/    /           \::::::/    /                   ~~                \::::::/    /             |::|  ~|               \:::\/:::/    /      \/____/                  \::::::::/    /          ~~~~~~~~~~               \::::::::/    /            |::|   |          \/____/                  \:::\    \
         /:::/    /            \::::::/    /            \:::\    \               \::::::/    /            \:::\    \                                        \::::::/    /               /:::/    /            \:::\    \                                         |::|   |                \:::\    \          /:::/    /               /:::/    /             \::::/    /                                       \::::/____/              |::|   |                \::::::/    /                                 \::::::/    /                                     \::::::/    /             |::|   |                                    \:::\    \                          /:::/    /            \::::::/    /            \:::\    \               \::::::/    /            \:::\    \                                        \::::::/    /               /:::/    /            \:::\    \                                         |::|   |                \:::\    \          /:::/    /               /:::/    /             \::::/    /                                       \::::/____/              |::|   |                \::::::/    /                                 \::::::/    /                                     \::::::/    /             |::|   |                                    \:::\    \
        /:::/    /              \::::/    /              \:::\____\               \::::/    /              \:::\____\                                        \::::/    /               /:::/    /              \:::\____\                                        \::|   |                 \:::\____\        /:::/    /               /:::/    /               \::/____/                                         |::|    |               \::|   |                 \::::/    /                                   \::::/    /                                       \::::/    /              |::|   |                                     \:::\____\                        /:::/    /              \::::/    /              \:::\____\               \::::/    /              \:::\____\                                        \::::/    /               /:::/    /              \:::\____\                                        \::|   |                 \:::\____\        /:::/    /               /:::/    /               \::/____/                                         |::|    |               \::|   |                 \::::/    /                                   \::::/    /                                       \::::/    /              |::|   |                                     \:::\____\
        \::/    /                \::/____/                \::/    /                \::/____/                \::/    /                                         \::/____/                \::/    /                \::/    /                                         \:|   |                  \::/    /        \::/    /                \::/    /                 ~~                                               |::|____|                \:|   |                  \::/    /                                     \::/____/                                         \::/____/               |::|___|                                      \::/    /                        \::/    /                \::/____/                \::/    /                \::/____/                \::/    /                                         \::/____/                \::/    /                \::/    /                                         \:|   |                  \::/    /        \::/    /                \::/    /                 ~~                                               |::|____|                \:|   |                  \::/    /                                     \::/____/                                         \::/____/               |::|___|                                      \::/    /
         \/____/                  ~~                       \/____/                  ~~                       \/____/                                                                    \/____/                  \/____/                                           \|___|                   \/____/          \/____/                  \/____/                                                                    ~~                       \|___|                   \/____/                                       ~~                                                ~~                      ~~                                            \/____/                          \/____/                  ~~                       \/____/                  ~~                       \/____/                                                                    \/____/                  \/____/                                           \|___|                   \/____/          \/____/                  \/____/                                                                    ~~                       \|___|                   \/____/                                       ~~                                                ~~                      ~~                                            \/____/

```

As this is , the answer with the fewest amount of bytes wins!

# Answer
> 27 votes
# Minecraft, 22313 (50% hand-written code) bytes

Why did I ever promise to do this.... Well, it works, but I took a few liberties:

* Letters are followed by newlines
* There is no lowercase input (as uppercase and lowercase are the same)
* Letters look weird as Minecraft does not have monospaced font (it looks better with 'Force Unicode Font' in language settings)

Here is a screenshot:

Screenshot of output:

Download the world here. Note that the input keyboard is very precise on the clicking. Click a little to the right of each letter to avoid this confusion.

### Explanation

1. Input Keyboard pops up, takes input until 'Enter' "key" is pressed.
2. Teleports the "Controller" `ArmorStand` backwards one block until it reaches end of string.
3. While it is teleporting, the `ArmorStand` checks what character that the `ArmorStand` it is standing on represents, and activates that command block (which outputs the fancy letter).
4. Once the Controller reaches the starting position, it resets the program and kills the `ArmorStand`s.

# Answ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_2/GSq4/SYb:10/Sg|FbISXHQHBSGSl3bSGSY|:11/SXI_3/GSe|FOd/GSl3bTng/GSabHNGSabHTq3bNGSabHQEStESabHNGSp3/SabHTq2/Sx|FOl3bTo3/Sj|:5/Sdb:8/SubECdb:8/Sh|F bECabHNGSTEStH_2/GSab:10/CabH_2/GSg|FOdbEStHTnfGSebH/GSebHTq3b/GSebHQ/Cw/CebH/GSp3/SebHT 15b/C 20|FOq3bTiGSmISgbKSx~2tKSj|F 2~|q3b/GSX/Cw:8/Se~10q8/Sh|FOeb/CwHTend/GShbKShbHTq6/ShbHTR0bKSp3/ShbHTR1|FOtHTfGSp3/SjbIS 39bICmFOtKS 33bKS 37bKSj|FO 36bHTncGSl4/Sl3bBq4/Sl3bBR0bISp3/Sl3bBR0bFOubHBcGSp3/Sq2/CR1|FR|q2|OubIS 35bIS 39bISmFO 37bHBnabEStECtEStECtESR1bECtEStESR1b:|OwESabEStESu~2R7|FA|t:|OwES 37bECR1bEC 15|F_3| 38bESndb/Cv~2 23b/Cv~2 23b/CV8b/Cw/CR3b|_3|xb/Ceb/Cw/CV8~2 23b|_3|xb/C 39~2R8~2 22~2R4b/C",["_4","_4b","_4/",":2b",":2/",":2|",":3/",":3b",":4/",":4b",":6/",":6b"," 2b"," 2/"," 3|"," 3/"," 3b"," 4"," 4/"," 4b"," 5"," 6"," 6/"," 6b"," 7"," 7/"," 8"," 8/"," 9"," 10"," 10/"," 11"," 12"," 12/"," 13"," 14"," 14b:"," 14|"," 14/:"," 15/:"," 15b:"," 16"," 16/"," 16b"," 17"," 18"," 18b"," 19"," 20_5","\\"].map((l,i)=>b=b.replace(new RegExp([..."ABCDEFGHIJKLMNOPQRSTUVWXYZacdefghijklmopqrstuvwxyb"][i],"g"),l)),a="",b.match(/\D|\d+/g).map(x=>a+=+x?l.repeat(x-1):l=x),a=a.split`n`,o=a.map(_=>""))].map(c=>a.map((d,i)=>o[i]+=c<"A"?"        ":d.slice((g=n=>(c.charCodeAt()+n-65)*25-(c>"L")*8-(c>"T")*4-(c>"Y")*4)(0),g(1))))&&o.join`
`

```

## Explanation

ASCII art string is compressed using the format `<character><number_of_times_to_repeat>`. After this another stage of compression is added where numerous common combinations (eg. `20_5`) are replaced by single characters. Special characters `n` represents a new line and `b` represents a backslash.

I could probably improve the compression algorithm a bit but I think I'll leave it like this for now.

```
f=s=>
  [...s.toUpperCase(              // use upper-case letter codes

    // magical decompression magic
    b="e_5yyyyyyyyyyyh_5yx_7x_5x_7x_5yr_5yyyR0_5ynd/bTv/bTv/bTv/bTv/bTv/bTv/bTv/bTv/bTv/bTv/bTv/bTfbTv/bTu/DTu/bTu/DTu/bTv/bTk/bTv/bTv/bTv/bTu_6 15|bTv/bTdncDTsDTsDTsDTsDTsDTsDTsDBsDTsDTsDBsDBcDBsDBp4bTp2bTp4bTp2bTsDTiDTsDBsDBsDB 15|FOm:bBsDTnZJTo4bTo4bTo4bTo4bTo4bTo4bTo3/StHTq3bTo3/Sp3/SZ:4|Op4|Oo6bTj/JTj/LTj/JTo4bTgbHTo3/Sp3/Sp3/S 15|FOmFOtHTnWLTiLTiLTiLTiLTiLTiLTiGSwHTq3bTiGSp3/SZ:5|Oo5|Oj/:8bTg/LTg/:8bTg/LTiLTgbHTiGSp3/Sp3/ 3_/_3j|FOmFOubHTnU/GbHTfGbHTfGbHTfGbHTfGbHTfGbHTfGbHTfGS 20bHTq3bTfGSp3/SZ:6|Oj/:6|OiG~2bHTd/GbHTd/G~2bHTd/GbHTfGbHTgbHTfGSp3/Sp3/PbTh|FOmFOwHTnSG_2bHTcG_2bHTcGMHTcGMHTcG_2bHTcG_2bHTcGMHTcGC 22bHTq3bTcGCp3/SZG|FOiG|FOg/GTHTZG_2bHTZGTHTZG_2bHTcG_2bHTgbHTcGSp3/Cp3/PDBg|FOmFOxbHTnPJQHTWJQHTWGTHTWGTHTWJQHTWJQHTWGTHTWJT 22/JTo4bTWJTo3/SZG |FOg/G |FOfGS bHTU/JQHTU/GS bHTU/JQHTYb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HQH b/ChbKSTKShbKSu|FOhbHTv/GSg|F/GSUbHSGSjbKSUbH|F/GSe|:9/STHQHTWGShbHT /GSV|FA|/GSTKPGSd|FOfGShbH~4b~6ngbISXHQISXHTq3bSGSXHQHBq3bBYbHQHBq4/SXICl4/Sv|FOjbHTsGSh|KSYbH_2/GSq4/SYb:10/Sg|FbISXHQHBSGSl3bSGSY|:11/SXI_3/GSe|FOd/GSl3bTng/GSabHNGSabHTq3bNGSabHQEStESabHNGSp3/SabHTq2/Sx|FOl3bTo3/Sj|:5/Sdb:8/SubECdb:8/Sh|F bECabHNGSTEStH_2/GSab:10/CabH_2/GSg|FOdbEStHTnfGSebH/GSebHTq3b/GSebHQ/Cw/CebH/GSp3/SebHT 15b/C 20|FOq3bTiGSmISgbKSx~2tKSj|F 2~|q3b/GSX/Cw:8/Se~10q8/Sh|FOeb/CwHTend/GShbKShbHTq6/ShbHTR0bKSp3/ShbHTR1|FOtHTfGSp3/SjbIS 39bICmFOtKS 33bKS 37bKSj|FO 36bHTncGSl4/Sl3bBq4/Sl3bBR0bISp3/Sl3bBR0bFOubHBcGSp3/Sq2/CR1|FR|q2|OubIS 35bIS 39bISmFO 37bHBnabEStECtEStECtESR1bECtEStESR1b:|OwESabEStESu~2R7|FA|t:|OwES 37bECR1bEC 15|F_3| 38bESndb/Cv~2 23b/Cv~2 23b/CV8b/Cw/CR3b|_3|xb/Ceb/Cw/CV8~2 23b|_3|xb/C 39~2R8~2 22~2R4b/C",
    ["_4","_4b","_4/",":2b",":2/",":2|",":3/",":3b",":4/",":4b",":6/",":6b"," 2b"," 2/"," 3|"," 3/"," 3b"," 4"," 4/"," 4b"," 5"," 6"," 6/"," 6b"," 7"," 7/"," 8"," 8/"," 9"," 10"," 10/"," 11"," 12"," 12/"," 13"," 14"," 14b:"," 14|"," 14/:"," 15/:"," 15b:"," 16"," 16/"," 16b"," 17"," 18"," 18b"," 19"," 20_5","\\"]
      .map((l,i)=>b=b.replace(new RegExp([..."ABCDEFGHIJKLMNOPQRSTUVWXYZacdefghijklmopqrstuvwxyb"][i],"g"),l)),

    // decompression stage 2
    a="",                         // a = array of each line of the ASCII art letters
    b.match(/\D|\d+/g)            // get an array of characters and numbers
      .map(x=>
        a+=+x                     // if the current element is a number
          ?l.repeat(x-1)          // repeat the previous character x times
          :l=x                    // else set l to the new character
                                  // add it to a in case there is no number after it
      ),

    a=a.split`n`,                 // split a into an array of lines
    o=a.map(_=>"")                // o = array containing each line of the output
  )].map(c=>
    a.map((d,i)=>                 // loop through each line of the current letter
      o[i]+=                      // add the letter's substring for the current line
        c<"A"?"        ":         // space = 8 spaces
        d.slice(
          (g=n=>
            (c.charCodeAt()+n-65) // get the letter index
              *25                 // each ASCII art letter is 25 characters wide
              -(c>"L")*8          // except L is 17
              -(c>"T")*4          // T is 21
              -(c>"Y")*4          // Y is 21
          )(0),g(1))
    )
  )
  &&o.join`
`
```

```
Letters to Test: <input type="text" oninput="output.innerHTML=f(this.value)" /><pre id="output"></pre>
```

# Answer
> 9 votes
# PHP, 1898 ~~1905~~ ~~1922~~ bytes

Yeah, PHP beats all, at least currently. :)

```
$r=[L=>8,T=>4,V=>4,X=>1,Y=>4];for(;$c=strtoupper($argv[1][$z++]);)foreach(explode("
",gzuncompress(base64_decode('eNrtWluO5CAM/OcU3ID/nAWpL4Jy9u0QQmzsMjiTleZjIu12D5XCDwpwSMd4XZ/jisr1B/wc+HxM5BWGDsC7X2TER3dDMPRvKR//Z3nLH/BzIG0bQl5j6AC8+0VGn0Jja/F0Faah/17gCP5/d/UO8G0H1hNudzJ0ACsauQuDs8CybeX4EM12V8yzMAv99wKpfhkjzGgIs7crE1Azj4DarlpPVruLAQCYDhQgzqwFQimqzbpnYRL67wVQvpxafKhErDgJtDAU68ludzAggLX4XImfNG7GD6QoPAu3Dc3pXwygfPm0+FSJWHECqD3tu2IdBehnYMC3GywpUakXn0hx9Cx0G98tXThtA1qYNvCijaMGSUshWgDsypZoQYpTgN6TsI4i9zMw4NsNzMx2JSr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K/DfhGwP2bllk9iBcJFLo2mJa7PiNvMmavVVa0GJaedxCwVsNNGZMB8fxa4ZENPYu4K/9vKLL91hzPEG21xu4+kfsrDLcUleyG6d4Os/giw3L4gXGvDZBFvw1sXN3xZlK8T7p/5K4/u27G9bYlurTI3Q3zvR1m8UWG/1ovOeddoSz6bbwYIE6i212/DTfD35NwN/wDTMusIA')))as$k=>$v)$o[$k].=' '==$c?'        ':substr($v,(ord($c)-65)*25,25-$r[$c]);echo implode("
",$o);

```

Works from command line:

```
$ php asciistuff.php HelLo
$ php asciistuff.php "H E L L O"

```

It handles upper- and lowercase characters as well as whitespaces.

**Golfed version without the long string**

```
$r=[L=>8,T=>4,V=>4,X=>1,Y=>4];for(;$c=strtoupper($argv[1][$z++]);)foreach(explode("
",gzuncompress(base64_decode('encoded alphabet')))as$k=>$v)$o[$k].=' '==$c?'        ':substr($v,(ord($c)-65)*25,25-$r[$c]);echo implode("
",$o);

```

**Ungolfed**

```
// store difference of shorter letters to default value (25)
$r=[L=>8,T=>4,V=>4,X=>1,Y=>4];

// loop through each character in the input
for(;$c = strtoupper($argv[1][$z++]);)
    // the whole alphabet is compressed and stored in a base64 encoded string
    // it's then stored line by line in an array
    // for each character we loop through every line of the alphabet
    foreach(explode("\n",gzuncompress(base64_decode('encoded alphabet'))) as $k => $v)
        // if a white space is given, 8 whitespaces are added to each line of the output
        // otherwise the letter sequence, dependent on letter length, is extracted from the current line
        $o[$k] .= ' ' == $c ? '        ' : substr($v, (ord($c) - 65) * 25, 25 - $r[$c]);
// finally print the result
echo implode("\n",$o);

```

---

**Edits**

* *Saved* **17 bytes** by refactoring the loops and declaring the lines of the alphabet inline.
* *Saved* **7 bytes** by replacing `foreach()` with `implode()` and by replacing `"\n"` with real line breaks.

# Answer
> 5 votes
# Matlab (Java), ~~28125~~ ~~28119~~ 6810 bytes

Could perhaps still be golfed somewhat more. Now I am using a string compression via Java, stolen from here

The core is the nice thing that in Matlab you can also get 2d (or if you need 3d etc) slices from matrices, and also *stick* them together again. This saves a lot of work! This is done for every character of the input.

```
Z=uint8([120;156;237;155;193;145;219;60;12;133;51;255;164;16;116;160;91;14;62;165;16;206;160;145;29;55;145;38;83;70;86;36;1;226;1;240;102;253;175;44;75;14;117;241;228;139;150;164;200;199;7;144;18;191;255;247;173;94;63;126;214;159;223;191;126;146;94;75;161;120;45;151;75;196;239;240;178;164;144;211;59;57;131;142;174;112;173;138;3;92;241;226;255;188;92;180;97;181;221;189;118;197;181;221;92;255;249;94;98;199;210;26;185;171;254;246;191;28;119;213;223;126;39;147;195;181;221;181;141;80;102;109;119;33;40;179;54;131;12;182;208;98;3;7;6;40;216;193;134;217;195;138;57;192;21;115;132;239;152;19;40;181;249;235;62;149;4;172;35;234;161;29;209;1;205;136;154;50;205;88;185;50;7;134;50;229;215;149;185;254;94;83;149;92;83;149;80;166;18;166;76;37;157;58;149;212;27;201;149;217;111;36;218;72;37;111;111;8;187;28;162;32;74;163;65;37;43;77;84;66;156;9;162;213;230;175;231;171;100;161;68;37;11;142;21;204;94;167;146;241;20;13;179;254;175;237;125;169;185;222;222;231;40;187;154;219;28;101;87;179;12;10;212;76;58;40;197;99;70;47;17;236;84;130;61;6;42;241;56;120;181;180;51;226;132;74;59;3;62;151;151;108;171;146;107;80;9;122;73;87;73;244;146;209;68;172;89;155;56;189;196;195;243;70;156;152;151;160;151;24;28;243;146;18;189;164;151;233;189;164;104;6;3;238;240;94;102;226;37;235;125;209;75;236;12;183;158;103;230;189;120;137;155;247;226;37;197;171;228;32;94;114;156;236;213;214;142;56;168;164;209;160;146;70;51;28;85;34;20;85;82;47;175;18;138;216;140;210;63;161;146;227;71;28;130;136;179;244;232;48;84;178;180;167;11;17;135;211;236;149;211;236;149;69;37;46;226;148;214;199;62;226;20;179;30;49;17;199;134;12;85;9;67;200;144;136;195;65;37;213;220;130;74;56;19;196;254;17;231;56;94;242;241;74;216;42;79;87;194;86;121;186;18;94;137;142;255;69;102;51;147;85;158;172;132;25;114;29;89;9;179;203;117;250;74;120;113;189;22;30;198;67;142;121;73;173;33;174;132;213;51;220;112;100;201;10;229;201;202;195;188;132;189;59;244;54;100;216;251;189;210;12;151;220;167;10;248;148;180;19;212;178;12;117;12;181;176;83;7;228;37;214;167;70;94;98;124;202;230;37;195;167;66;94;178;168;147;155;24;210;212;242;111;175;113;30;165;18;166;212;167;10;250;20;168;68;158;26;84;34;142;132;42;81;154;236;124;145;83;137;116;155;83;137;105;167;85;137;182;147;64;37;17;211;231;241;249;85;178;119;196;121;235;151;91;227;200;229;34;206;192;18;113;120;20;177;134;246;30;113;120;93;22;247;107;68;156;140;46;37;161;75;29;76;83;174;244;182;189;119;189;90;111;123;218;203;202;105;73;41;149;148;38;227;126;23;165;146;82;202;228;240;122;217;107;129;248;110;215;14;38;190;235;28;197;248;46;115;212;197;247;98;168;153;38;18;113;220;156;148;136;227;109;1;254;253;252;185;57;92;214;87;244;81;2;165;217;224;165;207;17;109;55;143;169;122;181;173;41;150;142;108;176;205;177;74;229;78;81;115;157;67;218;110;75;49;27;236;20;179;65;41;55;121;24;206;224;139;100;131;183;84;66;71;80;73;187;188;74;244;2;149;40;52;42;209;33;181;42;193;118;91;26;85;226;155;88;70;147;206;191;102;72;151;155;27;24;120;237;242;96;224;62;163;212;25;29;13;220;189;179;48;99;53;48;119;103;224;250;20;250;175;254;219;12;92;105;182;225;141;53;171;47;218;154;135;226;77;205;70;241;26;81;234;19;143;24;35;24;245;0;139;213;49;118;144;12;98;162;201;55;54;188;61;108;17;37;192;174;226;68;36;25;188;181;253;112;128;136;115;123;255;129;253;159;183;180;63;212;254;232;189;204;171;82;43;147;158;164;153;93;42;219;65;32;147;184;105;233;58;200;200;196;62;244;144;9;116;80;201;109;227;113;239;69;78;232;37;45;50;172;13;55;94;194;18;70;174;224;37;134;14;47;97;83;194;168;130;135;184;77;205;172;77;140;94;162;20;188;164;219;157;96;49;152;214;209;219;121;201;158;34;57;180;151;124;97;101;169;94;34;89;75;234;37;217;219;51;31;213;196;75;226;87;61;178;178;84;252;186;94;242;144;93;170;37;248;253;40;51;154;73;192;198;160;156;153;248;17;117;17;199;238;101;50;249;137;15;119;13;139;176;120;88;4;226;197;171;68;38;62;238;101;146;201;52;45;54;249;130;93;1;83;102;38;236;205;228;16;111;207;234;3;108;232;39;115;207;17;241;190;123;142;39;76;32;220;156;167;53;202;155;165;137;219;77;18;138;187;73;80;156;174;130;187;44;193;118;77;86;193;14;182;182;176;131;93;78;14;158;122;197;250;144;107;193;77;229;14;71;140;191;102;112;80;13;206;125;83;74;161;76;117;165;214;109;148;234;187;80;164;250;46;20;105;93;117;186;218;188;74;12;53;99;47;107;145;160;146;134;157;242;176;131;124;254;48;218;29;168;121;11;111;233;190;42;57;134;151;64;217;210;135;80;182;204;94;200;35;100;92;90;49;206;75;122;49;232;37;80;140;246;43;20;35;253;122;129;98;250;254;23;148;61;188;196;150;45;94;130;101;143;104;198;17;66;217;54;154;105;217;24;205;150;67;168;228;174;151;51;91;211;177;99;57;94;37;145;174;72;193;123;110;208;188;132;80;27;223;73;243;150;109;215;15;55;198;34;75;42;54;160;183;22;167;180;195;250;212;236;105;187;124;178;95;62;159;84;168;249;164;125;3;2;51;144;164;123;237;12;84;10;51;80;40;204;64;41;55;153;45;119;190;157;140;112;139;97;187;235;237;36;167;152;147;145;172;79;151;96;78;66;67;159;120;113;197;153;68;140;86;110;196;107;95;154;124;178;12;106;243;201;50;168;251;30;70;71;163;224;26;82;158;2;191;211;173;181;81;119;99;210;178;106;109;131;54;220;107;35;247;61;76;175;205;125;15;211;21;225;191;135;113;111;188;73;218;73;17;31;96;213;113;182;235;192;251;103;95;248;22;112;158;138;179;248;235;153;205;243;191;204;152;167;226;62;86;201;17;54;227;159;175;146;121;42;238;248;17;242;213;84;50;79;197;189;166;151;204;83;113;232;121;102;222;139;151;60;253;141;205;89;179;87;91;59;226;121;42;142;28;158;17;199;71;28;130;136;51;79;197;61;35;226;28;199;75;230;169;184;0;211;100;101;155;61;254;231;159;119;162;146;251;212;60;21;119;198;53;206;60;21;71;160;18;109;39;129;74;34;166;207;227;243;171;100;239;136;131;31;48;168;135;200;229;34;206;192;243;84;220;103;232;60;21;55;76;192;174;29;230;169;56;95;102;152;155;127;77;160;230;169;184;0;15;145;13;222;82;201;60;21;151;53;177;224;71;13;231;94;51;28;227;227;177;196;192;221;59;11;51;86;3;207;83;113;4;56;83;206;60;21;183;215;94;230;60;21;119;62;47;153;167;226;246;22;201;161;189;228;11;43;203;121;42;206;224;93;119;188;231;169;56;196;146;189;2;30;217;171;193;38;95;176;43;96;202;204;132;189;153;28;226;237;89;125;128;13;253;100;238;57;34;222;119;207;241;132;9;132;155;243;68;243;84;156;165;143;216;215;120;196;53;79;197;57;149;124;42;193;154;167;226;232;47;94;2;101;75;31;66;217;50;123;33;143;144;113;105;197;204;83;113;251;170;100;158;138;203;232;60;21;151;193;255;179;62;53;123;218;46;159;236;151;207;39;21;106;62;57;79;197;57;156;210;121;42;174;225;121;42;174;99;3;255;0;61;59;177;97]);
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;isc=InterruptibleStreamCopier.getInterruptibleStreamCopier;
c=java.io.ByteArrayOutputStream;isc.copyStream(java.util.zip.InflaterInputStream(java.io.ByteArrayInputStream(Z)),c);
Q=typecast(c.toByteArray,'uint8');n=double(Q(2))*8+2;s=typecast(Q(3:n),'double')';Q=Q(n+1:end);M=reshape(char(Q),s);
l=[];for k=lower(input('','s'));l=[l,M(:,25*(k-97)+(1:25))];end;disp(l)

```

# Answer
> 3 votes
# Python 2, 3376 bytes

Homemade compression, via lots of replacing of substrings.

```
x,y,z='123456789abcdefghi','ABCDEFGHIJKL','MNOPQRST'
k=dict(zip(x[9:],'10 11 12 13 14 17 21 24 25'.split()))
for c in x[:9]:k[c]=c
k.update(dict(zip(y,'2 3 4 5 6 8 9 10 11 16 17 18'.split())))
k.update(dict(zip(z,'2 3 4 5 6 7 8 15'.split())))
Y='v'*11+'aP2-vv9R9-v9R9-vv6Pa-vvvh-6Pa-vp'+'9Z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6-9V6!i-70C/O/7-7zalglil7z9-g-6U9-p8V7!7XO0p70Cq7-7XO0pi!8V7-7XO0pi-70A|3|a-7XO0-8V7-8V7-ji-8|A|4|p70A|3|a!g!i!7z9-g-7XO0pp8ypj8ypj8ypi-j8yp8ypi-80:|3|a-8y-8yp8yp9~~e-i-8|A|O|p80:|3|a-8ypg-ji-j7|A|N|9-g-8ypp9w9-9~~e-9w9-9~~e-9w9-i-i-9w9-9w9-i-90|N|a-9w -9w9-9w9-i-i-9~~e-90|N|a-9w9-g-9~~e-i-9~~e-8~~e-g-9w9-p'+'i-'*11+'f-'+'i-'*7+'g-i-i-i-h-g-i-8'
d=zip('nmlkj!&()UVXZzyxwvutsrqpo','2/E03U2- 3/C03U3- -60Eq6- -4|A|4| 80A/O/p -70Cq7- -7z9-5zp -50Fq5- 2/B|O|- X40 r4/ 0B0 /0409- |A|3| 0Aq 8-8/A0 0/O/ aPa- -4/B/ -7/C04 /E040 /B/ /4/ 8-'.split()+[' - '])
for a in d:Y=Y.replace(a[0],a[1])
Y=Y.split('-')
v=[]
for l in Y:
 r=''
 for c in l:
  if c in x:r+=' '*int(k[c])
  elif c in y:r+=':'*int(k[c])
  elif c in z:r+='_'*int(k[c])
  elif c=='0':r+='\\'
  else:r+=c
 v.append(r)
X=zip(*[v[i:i+27]for i in range(0,len(v),27)])
i=input()
for j in range(22):
 s=''
 for c in i.lower():l=26if c==' 'else ord(c)-97;s+=X[l][j]
 print s

```

# Answer
> 1 votes
# Python 2, 1208 bytes

Here’s the readable part of the source:

```
s=raw_input()
i=22
while i:i-=1;print''.join('''BINARY_STUFF'''.decode('zip').split('@')[ord(c)%32][i::22]for c in s)

```

The complete source contains non-printable characters, so it is presented as a hexdump that can be decoded with `xxd -r`.

```
00000000: efbb bf73 3d72 6177 5f69 6e70 7574 2829  ...s=raw_input()
00000010: 0a69 3d32 320a 7768 696c 6520 693a 692d  .i=22.while i:i-
00000020: 3d31 3b70 7269 6e74 2727 2e6a 6f69 6e28  =1;print''.join(
00000030: 2727 2778 0152 1862 c001 d0be 7dde ba8e  '''x.R.b....}...
00000040: 0371 146f 451d f0bb 6a21 308d 08ae 7d41  .q.oE...j!0...}A
00000050: ed1f 073e 1037 3ac8 892f ff8c 3be2 9387  ...>.7:../..;...
00000060: 2306 df85 d6db 9583 eb3a e1b6 ceb8 c4e8  #........:......
00000070: 2246 616b 5f0f dc06 c2f4 7360 4f90 f433  "Fak_.....s`O..3
00000080: 484f f2c7 9fb8 d4fe 67e5 d560 5eac f453  HO......g..`^..S
00000090: b8eb d28c 51c7 deb5 716b c0f1 1b7c 85b0  ....Q...qk...|..
000000a0: 1136 c2a0 b88c e132 86cb 182e 63b8 df90  .6.....2....c...
000000b0: 2641 d868 f65b 6974 52e9 ad84 838a 5c72  &A.h.[itR.....\r
000000c0: 3af6 25e8 d897 699a 2cb3 3449 960b 779d  :.%...i.,.4I..w.
000000d0: a509 590e 461f 9b26 db06 2a1f 0a84 8782  ..Y.F..&..*.....
000000e0: f0d0 1dcd 93dc e16a 2f9a 260b 0cba f680  .......j/.&.....
000000f0: 4361 b073 fb9b 6ef7 60aa 490f 0ea6 9a30  Ca.s..n.`.I....0
00000100: 2a09 1735 468d a926 654c 9a1c 9166 a4b5  *..5F..&eL...f..
00000110: 19d2 6fa1 fb6d 05d1 73ab c9f9 6972 719a  ..o..m..s...irq.
00000120: a89a 384d 88e5 34d1 3353 d504 fe55 9377  ..8M..4.3S...U.w
00000130: 7fe8 1889 dd40 cf4d aed0 d5c4 1835 4689  .....@.M.....5F.
00000140: ed6a 426c 5513 6283 19db 4724 b690 d860  .jBlU.b...G$...`
00000150: b89f 534d de60 0a7b 5b0a f602 9526 338c  ..SM.`.{[....&3.
00000160: 1aa3 4e13 7492 2643 cf4d 93df 4307 9e3e  ..N.t.&C.M..C..>
00000170: 7486 b661 7ae8 f060 183c 99c2 d674 0a5b  t..az..`.<...t.[
00000180: 8787 0ea9 a787 4e5f faf1 a193 2a09 86ab  ......N_....*...
00000190: 09c3 55d3 34a9 d943 a7d7 3423 eafc 87ce  ..U.4..C..4#....
000001a0: fb2e 8897 e605 3129 a805 3129 582c 8849  ......1)..1)X,.I
000001b0: 4194 d899 f794 16c4 0319 a570 d328 15d3  A..........p.(..
000001c0: e05e 666a c791 0b85 5133 fab4 6ad2 cbe6  .^fj....Q3..j...
000001d0: e26d 4485 52d2 a3cf 2a94 91f4 a0df 5c1d  .mD.R...*.....\.
000001e0: 4ead 5085 d24a 8744 d5dc 8404 f04a c7d3  N.P..J.D.....J..
000001f0: 4d86 6a9b af74 da6f a5f3 b034 6160 598b  M.j..t.o...4a`Y.
00000200: d129 5d94 26e8 a234 419d 261e cba4 8999  .)].&..4A.&.....
00000210: 7e9b dd6f a509 aa34 011f 9726 bf29 6cdf  ~..o...4...&.)l.
00000220: d2bc d2a1 5155 8cf4 64a3 ad6b f1d0 e15c  ....QU..d..k...\
00000230: 72bd ec9a 627e d061 d69e 62ae b8dd c59c  r...b~.a..b.....
00000240: 08dc 7e29 b7df 4a43 fb4c 4967 b5ba 55dd  ..~)..JC.LIg..U.
00000250: 4f5a ffc6 edb5 b0b7 d7c2 de5e 0bc7 2a18  OZ.........^..*.
00000260: 4641 d2c4 6941 9a34 30dc 5f6f 74c2 ea3e  FA..iA.40._ot..>
00000270: 4c3f c5ea 27a3 b9e5 d58c 8134 a684 a8ce  L?..'......4....
00000280: 48da aedb de6a d730 a348 5342 d453 42d4  H....j.0.HSB.SB.
00000290: db55 893b f09a eb84 29e1 a945 7c39 354d  .U.;....)..E|95M
000002a0: b634 a709 cd69 6224 4dd6 8de6 34a1 45c5  .4...ib$M...4.E.
000002b0: a489 d969 4207 df77 e590 71f8 982a be6d  ...iB..w..q..*.m
000002c0: 62a6 664d cc0c 1ef6 ee22 ec93 4f55 86da  b.fM....."..OU..
000002d0: ff17 c5bf f267 aa38 ff9a ec7e 17d5 50bd  .....g.8...~..P.
000002e0: 519a 07a3 e120 baf3 b6f1 a0b9 f3ee 7709  Q.... ........w.
000002f0: c33c 68cc 650c 9731 0c2a 1fde 6a1f a2a6  .<h.e..1.*..j...
00000300: 19d8 9eb6 ab79 11a2 ca92 e8ec 8c84 5b23  .....y........[#
00000310: e4d6 80a8 b204 254b 4e38 2379 c76a 32c2  ......%KN8#y.j2.
00000320: 8da0 aa26 4c33 eaba 9aa0 aa26 4428 5593  ...&L3.....&D(U.
00000330: 4aac a1f4 860e 1518 7535 418f d524 d7b9  J.......u5A..$..
00000340: b99a 9c90 276f b6c0 ecb7 2d30 5755 93e9  ....'o....-0WU..
00000350: c947 d418 3d56 9344 fdfc 6af2 e4ed 2a62  .G..=V.D..j...*b
00000360: 838a 2dbc e1c4 f5a9 9f06 3332 8505 7d46  ..-.......32..}F
00000370: 620c 9731 fcba 4769 8fae 24bf ddc7 a7be  b..1..Gi..$.....
00000380: 9d9f b320 e98d bc40 bdad e405 0963 872b  ... ...@.....c.+
00000390: c3b5 eb22 46cd 28fc 6947 5e0f 6dec 3237  ..."F.(.iG^.m.27
000003a0: edc0 d260 1446 1301 d6ce 6e0b 5b0b 9672  ...`.F....n.[..r
000003b0: fbad b9fd ba5a 8d57 50a7 090a a393 34a1  .....Z.WP.....4.
000003c0: 4370 ffa7 e915 fdb4 9e9c 26e7 5713 6252  Cp........&.W.bR
000003d0: 4d1c d3d5 84b9 c3a1 9ae4 8be1 e802 534d  M.............SM
000003e0: f4c5 aa26 30d5 c41c 754c 146e 7d5a 4dd8  ...&0...uL.n}ZM.
000003f0: 2217 f3e6 c1c4 8415 534c cc53 d284 5177  ".......SL.S..Qw
00000400: 9a6e 1492 2d8d aaa3 5673 9d46 185a 87ab  .n..-...Vs.F.Z..
00000410: fd57 3dc6 bdeb 7df0 7ba1 a9c5 03d4 6b8f  .W=...}.{.....k.
00000420: 1397 a75b 1a3a 384d 934a 5893 4a9d 86c0  ...[.:8M.JX.J...
00000430: 3af7 80a5 30ea b198 b8db 7f3e aba4 bdc2  :...0......>....
00000440: 59e5 bc63 ab35 880a 5121 2a94 0689 2bbc  Y..c.5..Q!*...+.
00000450: 4883 0bba f406 a2fa 800c ea6f 05c8 d5fc  H..........o....
00000460: 0199 5c2d e1a2 b95a 309a ab1d 3e20 3314  ..\-...Z0...> 3.
00000470: e4ac 7228 a824 798b b5c7 1f3b b9e9 0e27  ..r(.$y....;...'
00000480: 2727 2e64 6563 6f64 6528 277a 6970 2729  ''.decode('zip')
00000490: 2e73 706c 6974 2827 4027 295b 6f72 6428  .split('@')[ord(
000004a0: 6329 2533 325d 5b69 3a3a 3232 5d66 6f72  c)%32][i::22]for
000004b0: 2063 2069 6e20 7329                       c in s)

```

---
Tags: code-golf, ascii-art, kolmogorov-complexity
---


title: Crosses, no Noughts

text:
# Question
Title: Crosses, no Noughts
Everyone realizes that Tic Tac Toe is a solved game. However, the Misère version of only-Xs provides an interesting alternative.

In this version of the game, both players play Xs onto the board and try to avoid making three in a row. If you'd like to see more about this, Numberphile has a nice video about this concept.

## Given a board of Misère Crosses, play an optimal move.

A board is three lines of three characters each, which are `X` or . Thus:

```
X X
X
 XX

```

is a valid board. You may take this in any convenient format, so long as your input and output use the same format. Formats include (but are not limited to): A multi-line string (with optional trailing newline); A 2D array of characters which are `X` or ; a 1D flattened array of boolean values representing if each position has been played.

An optimal move is one that guarantees you will win by continuing to play optimally or prolongs your loss for as long as possible and is defined by the following rules:

* Avoid making three in a row.
* If you go first, play in the middle.
* If the only occupied space is the middle, play in any of the remaining spaces.
* If the middle square is not occupied and an outer square is, play opposite your opponent's last play.
* If the middle square is occupied and an outer square is, play a "knights move" (opposite, one over) away from a previous move that does not cause you to lose.
* If no remaining squares are left where you won't lose, play in any of the remaining squares.

\[NOTE: this has been proved to be non-optimal in one case but you should use this algorithm anyway.\]

**You may assume that all of your previous moves were optimal.** Thus, the first example board is not a valid input. Your opponent's moves may or may not be optimal.

If the game has ended (i.e. a three in a row has been made), behavior is undefined.

As this is , the shortest answer in bytes wins!

One possible path, using only optimal moves, is this:

```
[   ]  [   ]  [X  ]  [X  ]  [X  ]  [X  ]  [XX ]
[   ]->[ X ]->[ X ]->[ XX]->[ XX]->[ XX]->[ XX]
[   ]  [   ]  [   ]  [   ]  [ X ]  [XX ]  [XX ]

```

Here are possible inputs originating from the opponent using non-optimal moves:
(Note that only the left-side boards on this list are valid inputs.)

```
[X  ]  [X  ]
[   ]->[   ]
[   ]  [  X]

[XX ]  [XX ]
[   ]->[   ]
[  X]  [ XX]

[XX ]  [XX ]
[X  ]->[X X]
[ XX]  [ XX]

```

# Answer
> 3 votes
# Ruby, Rev B 121 bytes

Submission is the anonymous function, minus the `f=`. Shown in test program to illustrate use.

```
f=->n{["~mK)\7","}uYwQO"][l=n%2].bytes{|t|9.times{|i|(m=n|1<<i)==n||8.times{|j|m/2*257>>j&255==126-t&&t+j%2!=119&&l=m}}}
l}

puts g=f[gets.to_i]
puts
[7,6,5,
 8,0,4,
 1,2,3].each{|i|print g>>i&1; puts if i/3==1}

```

2 bytes saved by making the centre square the least significant bit instead of most significant bit (remove by `/2` instead of `%256`.) Remaining savings by a reorganization of the table of acceptable moves. Organizing as centre square free/occupied instead of by total number of X's allows for a simpler test. Also, now there are only 2 strings in the array so the `%w{string1 string2}` syntax is abandoned in favour of the `["string1","string2"]` syntax. This enables a nonprintable character `\7` to be included, which in turn enables a simpler encoding to be used: `126-t` instead of `(36-t)%120`.

# Ruby, Rev A 143 bytes

```
->n{l=r=("%b"%n).sum%8
%w{$ %5 - I+Wy Q S#}[r].bytes{|t|9.times{|i|(m=n|1<<i)==n||8.times{|j|m%256*257>>j&255==(t-36)%120&&t+j%2!=43&&l=m}}}
l}

```

This is an anonymous function. The input / output format was left open, so I've gone for a 9-bit binary number. the 512's bit represents the centre, with the remaining bits spiralling round it (the 1's bit is considered to be a corner.)

There are far more possible inputs than acceptable outputs, so the algorithm is to try all moves, and find one that fits an acceptable output pattern. The acceptable output patterns for each number of X's are hardcoded.

The information about the centre square is stripped off and the remaining 8 bits are multiplied by 257 to duplicate them. This pattern is then rotated past the acceptable patterns by rightshifting.

The loop is not exited when a pattern is found, so the returned pattern will be the LAST acceptable pattern found. For this reason, preferable patterns (where there is a preference) come later in the list.

Given the 'Knights move' strategy it is of little importance whether a pattern is rotated by 45 degrees or not. The ungolfed version follows the knights move strategy and therefore does not need to differentiate between corner squares and edge squares: three in a row is to be avoided anyway.

However, I found that this is not always the best strategy, as there is the following trick. If your opponent goes first and takes the centre he should win. But on his second move he makes the error of allowing you to make a 2x2 square you should take it, as this allows you to force him to make three in a row. This is implemented in the golfed version. A little extra code is needed in this one instance to distinguish between three X's in a corner (force opponent to lose) and 3 X's along one edge (immediate suicide.)

**Ungolfed in test program**

The ungolfed version follows the logic expressed in the question.

In the golfed version the table is slightly modified to `[[0],[1,17],[9],[37,7,51,85],[45],[47,119]]` to implement the slightly different behaviour for the case `r=3`. It is then compressed to printable ASCII (requiring decoding `(t-36)%120`). An additional bit of logic is required to differentiate between three X's in a corner and three X's along an edge in the case of the table entry 7: `&&t+j%2!=43`

```
f=->n{l=r=("%b"%n).sum%8                                      #convert input to text, take character checksum to count 1's(ASCII 49.)
                                                              #0 is ASCII 48, so %8 removes unwanted checksum bloat of 48 per char.
                                                              #l must be initialised here for scoping reasons.
  [[0],[1,17],[9],[11,13,37,51,85],[45],[47,119]][r].each{|t| #according to r, find the list of acceptable perimeter bitmaps, and search for a solution.
    9.times{|i|(m=n|1<<i)==n||                                #OR 1<<i with input. if result == n, existing X overwritten, no good.
                                                              #ELSE new X is in vacant square, good. So..
      8.times{|j|m%256*257>>j&255==t&&l=m}}                   #%256 to strip off middle square. *257 to duplicate bitmap.
                                                              #rightshift, see if pattern matches t. If so, write to l
  }
l}                                                            #return l (the last acceptable solution found) as the answer.

#call function and pretty print output (not part of submission)
puts g=f[gets.to_i]
puts
[6,7,0,
 5,8,1,
4,3,2].each{|i|print g>>i&1; puts if i<3}

```

**Output of test program**

This what happens when the computer plays itself.

```
C:\Users\steve>ruby tictac.rb
0
256

000
010
000

C:\Users\steve>ruby tictac.rb
256
384

010
010
000

C:\Users\steve>ruby tictac.rb
384
400

010
010
100

C:\Users\steve>ruby tictac.rb
400
404

010
010
101

C:\Users\steve>ruby tictac.rb
404
436

010
110
101

C:\Users\steve>ruby tictac.rb
436
444

010
110
111

```

**GAME ANALYSIS PLAYING FIRST**

This is actually very simple and linear.

When playing first, the middle square will always be the first square occupied.

### r=0

```
...  binary representation 0
.X.
...

```

### r=2

```
X..  binary representation 1001=9
.XX
...

```

### r=4

```
X.. binary representation 101101=45
.XX
XX.

```

There is only one way (up to symmetry) to have five X's including the middle square on the board without the game being over. There is an X in the middle square, one on each diagonal (at 90 degrees to each other) and one on each horizontal/vertical centreline (at 90 degrees to each other.) As an entire edge cannot be occupied the above is the only arrangement possible. Other player must lose on next move.

**GAME ANALYSIS PLAYING SECOND**

Play is quite different depending if the other player chooses the middle square.

### r=1

middle square occupied

```
.X. X..  binary representation 1
.X. .X.
... ...

```

middle square free

```
X.. .X. binary representation 10001=17
... ...
..X .X.

```

### r=3

Middle square occupied, if other player plays adjacent to your last X Playing a knight's move away as below is supported in the ungolfed version

```
XX. .XX binary representation 1011=11
.X. XX. or mirror image 1101=13
X.. ...

```

However the above is NOT the best move and is not supported in the golfed version. The best move is as follows, forcing a win on the next turn:

```
XX. binary representation 111=7.           XXX
XX. Only to be used where j is odd.        .X.
... Even j would look like image to right. ...

```

Middle square occupied, if other player plays at 90 or 135 degrees to your last X (play knight's move away.)

```
X.X .X. binary representation 100101=37
.X. .XX
.X. X..

```

Middle square free

```
X.X .X. XX. binary representations:
... X.X ...    1010101=85 (first two)
X.X .X. .XX and 110011=51 (last one)

```

### r=5

middle square occupied. For the reasons stated above in r=4, there are four possible moves, all of which lose. only one is supported: 101111=47.

middle square free. There is only one possible board up to symmetry, as follows. Other player must lose on next move, so there is no need to support r\>5.

```
XX. binary representation 1110111=119
X.X
.XX

```

---
Tags: code-golf, game
---


title: Find the nth cross-alternate sum

text:
# Question
Title: Find the nth cross-alternate sum
Given an input of a single positive integer, output the "cross-alternate sum" that corresponds to that integer.

Take the example of the input `n=5`. To find the cross-alternate sum, first create a square grid of width and height `n` that, reading from left to right and top to bottom, starts at `1` and increases by one each position:

```
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

```

Then, take the sums from the grid that form a "cross" (that is, both diagonals combined):

```
 1           5
    7     9
      13
   17    19
21          25

1 5 7 9 13 17 19 21 25

```

Finally, take the alternating sum of this sequence:

```
1+5-7+9-13+17-19+21-25

-11

```

Another example, for `n=6` (just to show what the cross looks like for even-numbered `n`):

```
 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

 1              6
    8       11
      15 16
      21 22
   26       29
31             36

1+6-8+11-15+16-21+22-26+29-31+36

20

```

Since this is , the shortest code in bytes will win.

Here are the correct outputs for `n=1` to `n=100`, which you can use as test cases:

```
1
4
-3
10
-11
20
-23
34
-39
52
-59
74
-83
100
-111
130
-143
164
-179
202
-219
244
-263
290
-311
340
-363
394
-419
452
-479
514
-543
580
-611
650
-683
724
-759
802
-839
884
-923
970
-1011
1060
-1103
1154
-1199
1252
-1299
1354
-1403
1460
-1511
1570
-1623
1684
-1739
1802
-1859
1924
-1983
2050
-2111
2180
-2243
2314
-2379
2452
-2519
2594
-2663
2740
-2811
2890
-2963
3044
-3119
3202
-3279
3364
-3443
3530
-3611
3700
-3783
3874
-3959
4052
-4139
4234
-4323
4420
-4511
4610
-4703
4804
-4899
5002

```

# Answer
> 26 votes
# Jelly, ~~21~~ ~~19~~ ~~11~~ ~~10~~ 7 bytes

```
²~³¡H+2

```

Try it online!

## Idea

Assume for a second that the first term of the final sum is subtracted rather than added.

Let **n** be a positive integer.

### Even case

```
 1              6
    8       11
      15 16
      21 22
   26       29
31             36

```

The differences between the diagonal elements on the lower half of the rows are the first **n ÷ 2** odd natural numbers. Since **1 + 3 + 5 + … + (2k + 1) = k2**, they sum to **(n ÷ 2)2 = n2 ÷ 4**.

In this example

```
- 1 + 6 - 8 + 11 - 15 + 16 - 21 + 22 - 26 + 29 - 31 + 36  =
-(1 - 6)-(8 - 11)-(15 - 16)-(21 - 22)-(26 - 29)-(31 - 36) =
(    5   +   3    +    1  )+(   1    +    3    +    5   ) =
             9             +              9               = 18

```

Thus, the sum is **2 × n2 ÷ 4 = n2 ÷ 2**.

### Odd case

```
 1           5
    7     9
      13
   17    19
21          25

```

The differences between the diagonal elements on the corresponding rows from above and below (`1` and `5`, and `21` and `25`; `7` and `9`, and `17` and `19`) is the same, so they will cancel out in the alternating sum.

In this example

```
- 1 + 5 - 7 + 9 - 13 + 17 - 19 + 21 - 25  =
-(1 - 5)-(7 - 9)- 13 +(17 - 19)+(21 - 25) =
    4   +   2   - 13 -    2    -    4     = -13

```

All that's left is the negative of the central element, which is the arithmetic mean of the first and last number, so it can be calculated as **-(n2 \+ 1) ÷ 2**.

### General case

Since **~x = -(x + 1)** for two's complement integers (**~** denotes bitwise NOT), the formula for the odd case can be rewritten as **~n2 ÷ 2**.

Also, since the first term (**1**) of the original sum is added instead of subtracted, the above formulas leave an error of **2**, which has to be corrected.

Therefore, the nth cross-alternate sum is **n2 ÷ 2 + 2** if **n** is even, and **~n2 ÷ 2 + 2** if it is odd.

Finally, bitwise NOT is an involution, i.e., **~~x = x** for all **x**. This way **~~~x = ~x**, **~~~~x = x**, and, in general, **~nx** (meaning that **~** is applied **n** times) is **x** if **n** is even and **~x** if it is odd.

Thus, we can rewrite our general formula as **~nn2 ÷ 2 + 2** for all positive integers **n**.

## Code

```
²~³¡H+2    Main link. Input: n

²          Yield n².
 ~         Apply bitwise NOT to n²...
  ³¡           n times.
    H      Halve the result.
     +2    Add 2.

```

# Answer
> 5 votes
# JavaScript, ~~40~~ ~~38~~ 22 bytes

Using that new-fangled, fancy closed form solution that's all the rage!

```
n=>(n%2?3-n*n:4+n*n)/2

```

Thanks to ThomasKwa, I can eliminate my costly recursive function.

# Answer
> 4 votes
# Jelly, 12 bytes

```
RṖµ²+‘×-*$SC

```

Try it here.

# Answer
> 4 votes
# CJam, 13 ~~15~~ bytes

```
ri2#_{~}*2/2+

```

Two bytes off thanks to Dennis.

**Try it online!**

# Answer
> 3 votes
## Minkolang 0.15, ~~26~~ ~~15~~ 13 bytes

Using Dennis' insane algorithm, and golfed off another two bytes thanks to him. That guy is responsible for halving of byte count!

```
n2;d[~]4+2:N.

```

Try it here!

### Explanation

> @VoteToClose `n`^2, apply bitwise NOT `n` times, add four, halve. – Thomas Kwa 7 mins ago

See Dennis' answer for the explanation of why that works. In a comment on this answer, he suggested another improvement that works because `:` is integer division, so I can negate the top of stack and not worry about the +1 from doing the binary complement. Furthermore, n and n^2 have the same parity, which removes the need for a swap.

```
n                Take number from input - n
 2;              n**2
   d             Duplicates top of stack
    [~]          For loop that negates the top of stack n times
       4+        Add 4
         2:      Divide by 2
           N.    Output as number and stop.

```

# Answer
> 2 votes
# Pure Bash, 28

Well now that @Dennis has showed us all how to do this, this needs updating:

```
echo $[-1**$1*($1*$1+1)/2+2]

```

---

Previous answer:

# Bash + GNU utilities, 77

Here's a start:

```
a=$1
(seq 1 $[a+1] $[a*a]
seq $1 $[a>1?a-1:1] $[a*a])|sort -un|paste -sd+-|bc

```

N is passed as a command-line parameter.

`paste` is really handy here for producing the alternating sum. The `-d` option allows a list of separator characters, which are used cyclically.

# Answer
> 2 votes
# Julia, ~~41~~ ~~40~~ ~~25~~ ~~19~~ 16 bytes

```
n->-n%2$n^2÷2+2

```

This is an anonymous function that accepts an integer and returns an integer. To call it, assign it to a variable.

The approach here, devised by Dennis, is as follows. First we get the parity of *n*, i.e. *n* (mod 2), and negate it. This gives us 0 for even inputs and -1 for odd. We then bitwise XOR with *n*2. When *n* is even, this is just *n*2 because XOR with 0 is just the number. When *n* is odd, XOR with -1 is the same as bitwise negation. So at this point we either have *n*2 or the bitwise NOT of *n*2. We integer divide this by 2 and add 2 to get the result.

Saved a byte thanks to Sp3000 on a previous version, and saved 9 thanks to Dennis on this one!

# Answer
> 2 votes
# MATL, 13 ~~27~~ bytes

Using Dennis' amazing formulas:

```
2^t2\?Q_]2/2+

```

**Try it online!**

Direct approach (**27 bytes**):

```
2^:GtetXdwPXdhutn:-1w^!*s2+

```

# Answer
> 2 votes
# GolfScript, 12 bytes

```
~2?.{~}*2/2+

```

This uses the algorithm from my Jelly answer. Try it online!

### How it works

```
~            # Evaluate the input.
 2?          # Square it.
   .         # Push a copy of the square.
    {~}      # Push a code block that applies bitwise NOT.
       *     # Execute it n² times. Since n² and n have the same parity,
             # this is equivalent to executing in only n times.
        2/   # Halve the result.
          2+ # Add 2.

```

# Answer
> 2 votes
## ES7, 17 bytes

```
n=>-1**n*n*n+4>>1

```

Simple port of @Dennis's Python 2 answer.

While writing this answer I managed to golf my ES6 port to 17 bytes too!

```
n=>(n*n^-n%2)/2+2

```

# Answer
> 1 votes
# Jolf, 13 bytes

Try it here!

```
½?mρj-3Qj+4Qj
 ?mρj         if parity of j
     -3Qj     return 3 - j*j
         +4Qj else return 4 + j*j
½             (and halve the result)

```

# Answer
> 1 votes
# Python 2, 24 bytes

```
lambda n:(-1)**n*n*n/2+2

```

This uses the algorithm from my Jelly answer, with a minor modification:

Instead of applying `~` **n** times, we apply `-` **n** times (by multiplying by **(-1)n**). This is equivalent because **~x = -x - 1** and integer division floors in Python, so **~x / 2 = (-x - 1) / 2 = -x / 2**.

# Answer
> 1 votes
# GS2, 9 bytes

```
V,@!α2+''

```

This uses the algorithm from my Jelly answer. Try it online!

```
V,@e 7+''

```

is equally short, but notably contains no non-ASCII characters.

### How it works

```
V          Parse the input as an integer n.
 ,         Compute n².
  @        Push a copy of n².
   !       Bitwise NOT.
    α      Make a block of the previous instruction.
     2     Execute the block n² times. Since n² and n have the same parity,
           this is equivalent to executing in only n times.
      +    Halve the result.
       ''  Increment twice.

```

# Answer
> 1 votes
# Pyth, 11 bytes

```
+2/u_GQ*QQ2

```

Try it online in the Pyth Compiler.

### How it works

This uses the algorithm from my Jelly answer, with a minor modification:

Instead of applying `~` **n** times, we apply `-` **n** times (by multiplying by **(-1)n**). This is equivalent because **~x = -x - 1** and integer division floors in Pyth, so **~x / 2 = (-x - 1) / 2 = -x / 2**.

```
+2/u_GQ*QQ2  Evaluated input: Q

       *QQ   Yield Q².
   u  Q      Set G = Q². For each non-negative integer below Q:
    _G         Set G = -G.
             Return G.
  /       2  Halve the result.
+2           Add 2.

```

# Answer
> 1 votes
# dc, 17

Using the same tried and tested formula from Dennis:

```
?dd*1+r_1r^*2/2+p

```

Try it online Oh, why doesn't the Ideone bash sandbox include `dc`?

### Command-line Test:

```
for i in {1..100}; do echo $i | dc -e '?dd*1+r_1r^*2/2+p'; done

```

# Answer
> 1 votes
# J, 16 bytes

```
[:<.2%~4+*:*_1^]

```

This uses the same algorithm as my Jelly answer. Test it with J.js.

# Answer
> 0 votes
# Dyalog APL, 13 bytes

```
⌊2+.5××⍨×¯1*⊢

```

This uses the same algorithm as my Jelly answer. Test it on TryAPL.

# Answer
> 0 votes
# Lua, 33 bytes (Try it online)

```
i=(...)print((-1)^i*i*i/2-.5*i%2)

```

How it works:

```
i=(...)print((-1)^i*i*i/2-.5*i%2)
i=(...)                           Take input and store to i
       print(
             (-1)^i               Raise (-1) to the i-th power: -1 if odd, 1 if even
                   *i*i/2         Multiply by i squared and halved.
                         -.5*i%2  i%2 is the remainder when i is divided by 2
                                  if i is odd, then i%2 will be 1, and this expression
                                  will evaluate to -0.5
                                  but if i is even, then i%2 will be 0, which makes
                                  this expression evaluate to 0
                                )

```

---
Tags: code-golf, sequence
---


title: Generate m-sequences

text:
# Question
Title: Generate m-sequences
# Introduction

*Maximum-length sequences*, usually known as *m-sequences*, are binary sequences with some interesting properties (pseudo-noise appeareance, optimal periodic autocorrelation) which make them suitable for many applications in telecommunications, such as spread-spectrum transmission, measurement of impulse responses of propagation channels, and error-control coding. As a specific example, they are the basis for the synchronization signals used in the (4G) LTE mobile communication system.

These sequences are best understood as generated by means of an *L*-stage *binary shift register with linear feedback*. Consider the following figure (from the linked Wikipedia article), which shows an example for *L*=4.

The *state* of the shift register is given by the content (`0` or `1`) of each stage. This can also be interpreted as the binary expansion of a number, with the most significant bit at left. The shift register is initiallized with a certain state, which we will assume to be `[0 ... 0 1]`. At each discrete-time instant, the contents are shifted to the right. The value of the rightmost stage is the output, and gets removed from the register. The successive values of the rightmost state constitute the output sequence. The leftmost part is filled with the result of the feedback, which is a modulo-2 (XOR) combination of certain stages of the register (in the figure they are the two rightmost stages). The stages that are actually connected to the modulo-2 adder will be referred to as the *feedback configuration*.

A sequence produced by a shift register is always periodic. Depending on the feedback configuration, different sequences result. For certain configurations the period of the resulting sequence is the maximum possible, that is, an m-sequence results. This maximum is 2*L*-1: the register has 2*L* states, but the all-zero state can't be part of the cycle because it is an *absorbing state* (once it is reached it is never abandoned).

A shift-register sequence is defined by the number of stages *L* and the feedback configuration. Only certain feedback configurations give rise to m-sequences. The example above is one of them, and the sequence is `[1 0 0 0 1 0 0 1 1 0 1 0 1 1 1]`, with period 15.

It can be assumed that the last stage is always part of the feedback, otherwise that stage (and possibly some of the preceding) would be useless.

There are several ways to describe the feedback configuration:

* As a binary array of size *L*: `[0 0 1 1]` in the example. As indicated, the last entry last will always be `1`.
* As a number with that binary expansion. But since it would not be possible to distinguish for example `[0 1 1]` and `[0 0 1 1]`, a dummy leading `1` is added. In the example, the number defining the feedback configuration would be `19`.
* As a polynomial (over the field GF(2)). Again, a leading 1 coefficient needs to be introduced. In the example, the polynomial would be *x*4+*x*+1. The exponents increase to the left.

The description in terms of polynomials is interesting because of the following characterization: the sequence generated from the shift register will be an m-sequence if and only if the polynomial is *primitive*.

# TL;DR / The challenge

Given a feedback configuration with *L*\>0 stages, generate one cycle of the resulting sequence, starting from the `0···01` state as described above. The feedback configuration will always correspond to an m-sequence, so the period will always be 2*L*-1.

**Input** can be given by any means, as long as it is not pre-processed:

1. Binary array. In this case *L* is not explicitly given, because it is the length of the vector.
2. You can choose to include a dummy leading `1` in the above array (and then *L* is the length minus 1).
3. Number with dummy leading `1` digit in its binary expansion.
4. Number without that dummy `1` *and* a separate input explicitly giving *L*.
5. Strings representing any of the above.

...or any other reasonable format.

The input format used must be the same for all possible inputs. Please indicate which one you choose.

For reference, A058947 gives all possible inputs that give rise to m-sequences, in format 2 (that is, all primitive polynomials over GF(2)).

The **output** will contain 2*L*-1 values. It can be an array of zeros and ones, a binary string, or any other format that is easily readable. The format must be the same for all inputs. Separating spaces or newlines are accepted.

Code golf. Fewest best.

# Test cases

Input in format 2, input in format 3, output as a string:

```
[1 1]
3
1

[1 0 0 1 1]
19
100010011010111

[1 0 1 0 0 1]
41
1000010101110110001111100110100

[1 0 0 0 0 1 1]
67
100000100001100010100111101000111001001011011101100110101011111

```

# Answer
# Jelly, 18 bytes

```
æḟ2µ&³B^/+ḤµḤ¡BḣḤṖ

```

This takes a single integer, encoding **a0** in its MSB and **aL-1** in its LSB.

The test cases become **1**, **12**, **18** and **48** in this format. Try it online!

### How it works

```
æḟ2µ&³B^/+ḤµḤ¡BḣḤṖ  Main link. Argument: n (binary-encoded taps)

æḟ2                 Round n down to k = 2 ** (L - 1), the nearest power of 2.
   µ                Begin a new, monadic link. Argument: r = k
    &³              Bitwise AND r with n.
      B             Convert to binary.
       ^/           Bitwise XOR all binary digits.
          Ḥ         Unhalve; yield 2r.
         +          Add the results to left and right.
           µ        Convert the previous chain into a single link.
            Ḥ       Unhalve; yield 2k = 2 ** L.
             ¡      Execute the link 2k times, updating r in each iteration.
              B     Convert the final value of r to binary.
               ḣḤ   Keep only the first 2k binary digits.
                 Ṗ  Pop; discard the last digit.

```
> 2 votes

# Answer
## Haskell, ~~88~~ 82 bytes

```
r@(h:t)%m=h:(t++[mod(sum[v|(v,1)<-zip r m])2])%m
l#x=take(2^l-1)$(1:(0<$[2..l]))%x

```

I'm using input method 1 (but in reverse order, because I'm shifting to the left) together with `L` as an explicit parameter.

Usage example (test case #2): `4 # [1,1,0,0]` -\> `[1,0,0,0,1,0,0,1,1,0,1,0,1,1,1]`.

How it works: `%` does the work. First parameter is the content of the shift register, second parameter is the feedback configuration. `#` starts the whole process by building the initial register content, calling `%` and taking the first `2^L-1` elements.

```
main function #:

                1:0<$[2..l]      -- initial register content: one 1 followed
                                 -- by L-1 0s
                            %x   -- call the helper function with the
                                 -- feedback configuration
take(2^l-1)                      -- and take the first 2^L-1 elements

helper function % (bind r to the whole register set, h to the first register a0
and t to register a1..aL. Bind m to the feedback config):

h:                               -- next element is h, followed by
                         %m      -- a recursive call of %, where the new register is
   t++                           -- t followed by
           [v|(v,1)<-zip r m]    -- the elements of l, where the corresponding
                                 -- elements of m are "1"
      mod(sum ...          )2    -- xored

```
> 4 votes

# Answer
# Pyth, 19 bytes

```
eM.u+xF*VQNPN+m0tQ1

```

Try it online: Demonstration or Test Suite

### Explanation:

```
eM.u+xF*VQNPN+m0tQ1   implicit: Q = input list in format 1
              m0tQ    create a list with len(Q)-1 zeros
             +    1   and append a one at the end
                      this is the starting sequence
  .u                  apply the following statement repeatedly to the starting seq. N:
       *VQN              vectorized multiplication of Q and N
     xF                  xor all this values
    +      PN            remove the last value of N and prepend the new calculated one
                      .u stops, when it detects a cycle and returns a list of
                      all intermediate states of N
eM                    take the last element of each state and print this list

```
> 3 votes

# Answer
# MATL, 26 bytes

```
g`tGZ&Bs2\2GBnq^*+H#2\tq]x

```

This works in current version (15.0.0) of the language.

Input is a number describing the feedback configuration, using format 3 as described in the challenge. Output is separated by newlines.

**Try it online!**

### Explanation

This uses numbers to represent the register contents, and a bitwise XOR operation to compute the feedback bit.

At each iteration the fed-back bit is introduced at an "extra stage" at the left of the leftmost stage. A divmod operation by `2` then gives the new contents (division quotient) and the output (division remainder). The stack holds the already produced output and the current state on top.

Instead of looping a fixed number (2*L*-1) of times, a "do...while" loop is used, which in this case requires fewer bytes. The loop exits when the `1` state is reached again.

```
g         % take input. Convert to logical: gives 1, because input is nonzero.
          % This is the initial state. The input has been copied into clipboard G
`         % do...while
  t       %   duplicate
  G       %   push input
  Z&      %   bitwise XOR
  Bs2\    %   convert to binary, sum, compute parity. The result is the feedback bit
  2GBnq^  %   compute next power of two that exceeds the register size. This is used
          %   for introducing the fed-back bit in a leftmost stage "outside" the
          %   register, which will be immediately shifted right
  *+      %   multiply by feedback bit and add to register state
  H#2\    %   divmod by 2. Gives output bit and new state on top
  tq      %   duplicate and subtract 1. This is falsey if state is 1 (loop will be
          %   exited in that case)
]         % end do...while. Exit if top of stack is falsey
x         % delete current state. Implicitly display sequence

```
> 1 votes

# Answer
## JavaScript (ES6), 100 bytes

```
(l,f)=>[...Array((1<<l)-1)].map((_,i)=>(v=i?(x=v&f,[1,2,4,8,16].map(n=>x^=x>>n),x&1)<<l-1|v>>1:1)&1)

```

Takes type 4 input (length and feedback without the leading 1; well, the 1 is optional.) Returns an array of 0s and 1s. The parity calculation is annoying; I wanted to use `reduce` but that turns out to be 2 bytes longer, because the precedence of `<<` is wrong for my use case.
> 1 votes

# Answer
# Kotlin, 93 bytes

(91 + len(str(Number)))

Input `Number` is hardcoded in the source, without leading `1` and in reverse bit order (like @Dennis' answer). Input `L` is not needed!

The test cases become `1`, `12`, `18`, `48`.

```
fun main(a:Array<String>){var v=1
do{print(v and 1)
v=(v shr 1)xor 48*(v and 1)
}while(v!=1)}

```

Try it online!

Result appears rotated left by `L` bits:

```
100001100010100111101000111001001011011101100110101011111100000

```

Compare with original:

```
      100001100010100111101000111001001011011101100110101011111100000
100000100001100010100111101000111001001011011101100110101011111

      1010111011000111110011010010000
 1000010101110110001111100110100

      100110101111000
  100010011010111

```
> 0 votes

---
Tags: code-golf, math, sequence, coding-theory
---


title: Chaotic Number Shuffling

text:
# Question
Title: Chaotic Number Shuffling
You have a deck of cards, consisting of cards with non-negative numbers on them, and you haven't shuffled it yet, and no player trusts the other players to be fair at shuffling. You devise a way to shuffle the deck that is completely deterministic, but chaotic enough that the other players are okay with using it. Here's how you perform a single shuffle:

1. Start with the deck face down. There are a series of M empty piles (where M is the maximum number in the deck) labeled as the 1st, 2nd, 3rd, ... ,Mth pile, and there is also an empty storage pile.
2. While the deck is not empty, do the following: Take the first card off the top of the deck, let N be its number and put the card face down on the top of the storage pile. If there are at least N cards left in the deck, deal the top N cards face down such that the top card goes in the 1st pile, the second card from the top goes in the 2nd pile, etc. until the Nth card gets put in the Nth pile. If there are fewer than N cards in the deck, deal all of them.
3. Construct the new deck by putting the first pile on the storage pile, then the second pile on the new pile, then the third pile on that new pile, then the fourth pile on that one, until all piles are in your deck.

This method is pretty intuitive when doing by hand, but after a few games some players want a computer to do it, due to the deep-seated mistrust within the group and a few lucky draws. They refuse to use other deck shuffling programs that would work better. Write a program that takes as input a list of non-negative integers (in some format you specify) and shuffles it once using this method, outputting the result. You may assume the integers and the length of the list are less than 2^16.

Here are some worked-out examples (layout is deck|storage|1st|2nd|3rd|...|Mth, cards go from bottom to top):

```
9,8,7,6,5,4,3,2,1,0|||||||||||
9,8,7,6,5,4,3,2,1|0||||||||||
9,8,7,6,5,4,3|0,1|2|||||||||
9,8,7|0,1,3|2,4|5|6|||||||
|0,1,3,7|2,4,8|5,9|6|||||||
0,1,3,7,2,4,8,5,9,6||||||||||| (starting another iteration)
0,1,3|6|9|5|8|4|2|7||||
|6,3|9,1|5,0|8|4|2|7||||
6,3,9,1,5,0,8,4,2,7|||||||||||

```

Here is a long string of successive shuffles:

```
[9,8,7,6,5,4,3,2,1,0]->[0,1,3,7,2,4,8,5,9,6]->[6,3,9,1,5,0,8,4,2,7]->
[7,3,2,6,4,8,0,5,1,9]->[9,1,5,0,8,4,6,2,3,7]->[7,1,3,9,2,6,4,8,0,5]->
[5,9,0,3,8,1,4,7,6,2]->[2,4,9,6,1,5,7,8,3,0]->[0,3,1,9,8,6,4,7,2,5]->
[5,9,2,1,7,3,4,0,6,8]->[8,5,6,0,4,3,7,1,2,9]->[9,2,1,7,3,4,0,6,5,8]

```

Another few:

```
[1,0,1,0]->[0,1,1,0]->[0,1,0,1]->[1,1,0,0]->[0,0,1,1]->[1,0,0,1]->[1,0,1,0]
[1,1,1,0]->[0,1,1,1]->[1,1,1,0]
[1,0,1,1]->[1,0,1,1]
[4,3,2,1]->[1,3,2,4]->[4,2,3,1]->[1,2,3,4]->[4,3,2,1]
[1,2,1,2,1,2]->[2,1,1,1,2,2]->[2,1,2,2,1,1]->[1,2,2,1,2,1]->[1,1,2,2,2,1]

```

# Answer
> 2 votes
# Perl 5, 81 bytes

A naive approach (just shuffling per the specs). Doubtless someone will find a shorter way to do it (even in Perl 5, I mean).

```
{while(@_){for(1..($.=pop)){push@{$a[$_]},pop}push@{$a[0]},$.}$,=$/;say@$_ for@a}

```

This is a subroutine, but what it prints (not returns) is the shuffled deck. It requires `-M5.01` (which is free).

```
$ perl -M5.01 -e'sub{while(@_){for(1..($.=pop)){push@{$a[$_]},pop}push@{$a[0]},$.}$,=$/;say@$_ for@a}->(9,8,7,6,5,4,3,2,1,0)'
0
1
3
7
2
4
8
5
9
6

```

It also adds extra newlines in various places (depending on the deck), but there's… well, nothing *technically* in the specs forbidding that. `:-)`

# Answer
> 0 votes
# Ruby, 91 bytes

There is probably a better way to create an array of arrays. There's probably a shorter way to do all the shuffling, too. We'll see what future answers and comments bring to the table and all golfing suggestions are welcome.

```
->d{z=(1..d.size).map{[]};([d[-1]+1,d.size].min.times{|i|z[i]<<d.pop})while d[0];z.flatten}

```

**Ungolfing:**

```
def shuffle(deck)
  # an array of arrays
  z = (1..deck.size).map{[]}

  # while there are still cards in the deck
  while deck[0]
    # if we're almost out of cards, use how many are left
    # else, put in deck[-1] + 1 in the piles (that is, including deck[-1])
    card = [deck[-1] + 1, deck.size].min
    card.times do |i|
      z[i] << deck.pop
    end
  end

  # flatten all of those arrays done and return
  return z.flatten
end

```

---
Tags: code-golf, number
---


# Question
Title: Write a palindrome that prints "Hello, World!" without using comments
Your Task: Print "Hello, World!" using a palindrome.

Simple, right?

Nope.

# You have one restriction: **you may not use comments.**

"Comments" include

* Normal `//` or `#` comments
* Adding extraneous characters to a string literal
* Triple-quoted string comments
* Creating a string literal to hide characters

You may not use a pre-defined procedure to print "Hello, World!". (like HQ9+'s H command)

This is , so the shortest answer in bytes wins.

# Answer
## Stuck, 0 bytes

I think this wins? An empty program in stuck prints `Hello, World!`

Shamelessly stolen from this answer
> 4 votes

# Answer
# Bash, 47 bytes

```
echo Hello, World!
exit
tixe
!dlroW, olleH ohce

```
> 2 votes

# Answer
# Gogh, 31 bytes

```
"Hello, World!"Ƥ"!dlroW ,olleH"

```

---

### Explanation

```
"Hello, World!"    “ Push the string literal ”
¡                  “ Output the TOS          ”
"!dlroW ,olleH"    “ Push the string literal ”
                   “ Non-implicit output     ”

```
> 1 votes

# Answer
## Help, WarDoq!, 1 byte

```
H

```

Print Hello, World!.
> 1 votes

---
Tags: code-golf, restricted-source, palindrome
---