Last active
September 9, 2019 12:21
-
-
Save xPMo/e549b802c408e0b8e93cacecb3c5ed9b to your computer and use it in GitHub Desktop.
Expanded solution to https://codegolf.stackexchange.com/a/182455/86147
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zip_and_transpose() { | |
# for convenience, we zip our leading spaces (which provide the offset for diagonal cases) here | |
# Using ^^ will wrap to previous entries until "argv" is exhausted (this is only useful in the first call) | |
for s line (${spaces:^^argv}) | |
s+=$line && # concatenate the spaces onto the start of the line | |
for (( i=0 ; i++ < $#s; )) | |
a[i]+=$s[i] | |
} | |
spaces=(+) # we need to declare this an an array from the start for the zip to work correctly | |
# zip_and_transpose will append to elements of a. | |
# We must ensure: | |
# 1: a is an array (so a[i]+=... will append to an element, not insert into a string) | |
# 2: the existing contents are separated from the new conents | |
# ${foo/%/bar} will append 'bar' onto each element of foo | |
# a=(...) will set a as an array | |
# We initialize a with the contents of the command line parameters | |
# === HORIZONTAL (implicit) === | |
a=(${@/%/_}) | |
# === VERTICAL === | |
zip_and_transpose $@ | |
# === DIAGONAL 1 === | |
a=(${a/%/-}) | |
# This is the simplest way I found to create increasing spaces ('_' '__' '___' ...) | |
# The (l:expr:) flag padds the parameter to width 'expr' with spaces. I add the '_' | |
# at the end to ensure that the length starts at 1. | |
for s;spaces[++j]=${(l:j:)}_ | |
# === DIAGONAL 2 === | |
a=(${a/%/ }) | |
zip_and_transpose ${(Oa)@} # rows in inverse order | |
# a basic test ternary to finish. | |
[[ $a = *XXXX* ]]&&<<<X||<<<Y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try it online!