Skip to content

Instantly share code, notes, and snippets.

@roosto

roosto/timber_short.pl

Last active Aug 29, 2015
Embed
What would you like to do?
A file for a “Code Golf” challenge. Specially “Timber”, № 8, here: http://codegolf.stackexchange.com/questions/16707/9-hole-challenge
# roosto’s “Code Golf” v3 149 characters
# problem is #8 here: http://codegolf.stackexchange.com/questions/16707/9-hole-challenge
#
# The basic idea here is to go line by line, looking through the current line for whitespace.
# When we find a whitespace character we see if the character above it needs
# to have a non-whitespace character, a.k.a. a “supporting” character below it.
# If the “supporting” character above the whitespace character is not '/' or '\', a.k.a. a “bracing” character,
# then shit is fucked. We make comparisons easier and severely limit corner cases by
# kinda normalizing the input with a few regexs before we get into it.
#
# The corner cases are:
# * uneven line lengths (OK, because undef evaluates to false)
# * “bracing” characters at the beginning or end of a line (OK, because we pad beginning & end with '0')
# * “bracing” characters which do not have a “supporting” character on the appropriate side (OK, because we turn these into non-“bracing” characters)
#
# That last point was the hardest to figure for me. A “bracing” character without a “supporting”
# character on its appropriate side is still a “supporting” character if it has a “supporting”
# character beneath it.
#
# Improvements? I think the way that true or false is printed could be made shorter
#
$0="tru";
if ( 0 ) {
open F,pop; # bareword 'F' is now a file pointer to file named by ARGV[0]
while(<F>){ # line by line, contents of line stored as a string in $_
y/0/6/; # transliterate '0' -> '6'
s/^|\s|$/0/g; # all whitespace into '0', also pad the beginning & end of the line with '0' (pad end of line is in case of file not ending in newline)
s#\\(?=0)|(?<=0)/|[^\\/0]#6#g; # '\' followed by '0' & '/' preceded by '0' into '6', anything not '/', '\', or '0' into '6'
@n=split //; # transform $_ into an array, one element for each char
for(0..@n){ # quick & _quite_ dirty version of: for( $_ = 0; $_ < last index of @n; $_++ )
if(!$n[$_] && $l[$_]==6){ # in the “minified” version this if is a ternary to save characters–#sorrynotsorry (no, but really it’s gross [but really that’s what this is all about])
# if current character is 0; i.e., whitespace in original file && the character above it is not / or \, we are unstable
# keep in mind that we converted any '/' or '\' w/o its “supporting” char next to it to a '6' …
# a '/' or '\' w/o a “supporting” char next to it is still supported if a “supporting” char is below it
$0='fals' # no need for a semicolon for the last statement in a block! a 1 character savings! (I am a terrible person)
# we could `exit 1` here, but `exit 1;`; is 7 characters, no to mention the `print`…
# Why sacrifice character count in exchange for an efficiently running program? (I work on crux all day—I’ve earned this, OK.)
}
}
@l=@n; # save this line, so as we have the line above the next line to compare to
}
print "$0e\n"; # print return value
}
# $ tail -2 true | wc -c
# 147
$0=tru;open F,pop;while(<F>){y/0/6/;s/^|\s|$/0/g;s!\\(?=0)|(?<=0)/|[^\\/0]!6!g;@n=split//;for(0..@n){!$n[$_]&&$l[$_]==6?$0=fals:1}@l=@n}print"$0e
"
@roosto

This comment has been minimized.

Copy link
Owner Author

@roosto roosto commented Jul 23, 2014

hehe … That super gross minified Perl seems to be upsetting github’s Perl syntax highlighter. The / at the end of the first regex is marked as red.

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