Skip to content

Instantly share code, notes, and snippets.

@xPMo
Last active September 9, 2019 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xPMo/e549b802c408e0b8e93cacecb3c5ed9b to your computer and use it in GitHub Desktop.
Save xPMo/e549b802c408e0b8e93cacecb3c5ed9b to your computer and use it in GitHub Desktop.
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
@xPMo
Copy link
Author

xPMo commented Mar 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment