Skip to content

Instantly share code, notes, and snippets.

@kernigh
Created May 28, 2014 18:22
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 kernigh/db4e610e10b592286d86 to your computer and use it in GitHub Desktop.
Save kernigh/db4e610e10b592286d86 to your computer and use it in GitHub Desktop.
# This script compiles scramble.c and calculates the score.
# Before use, put scramble.c, $text, $pict in current directory.
text=pg100.txt
pict=filament.jpg
# Die if any command fails.
# Die if files are missing.
set -e
ls scramble.c $text $pict >/dev/null
dir=$(mktemp -d tmp.XXXXXXXXXX)
trap 'echo You may wish to rm -rf $dir' EXIT
echo [1/4] Compiling scramble...
gcc -O2 scramble.c -o $dir/scramble
echo [2/4] Scrambling files...
set +e # When scrambling, ignore exit status.
$dir/scramble <$text >$dir/$text.sc
$dir/scramble <$pict >$dir/$pict.sc
set -e
echo [3/4] Compressing scrambled files...
xz -kz5 $dir/$text.sc
xz -kz5 $dir/$pict.sc
echo [4/4] Checking descrambler...
set +e # When descrambling, ignore exit status.
xz -dc $dir/$text.sc.xz | $dir/scramble >$dir/$text.cp
xz -dc $dir/$pict.sc.xz | $dir/scramble >$dir/$pict.cp
set -e
cmp -- $text $dir/$text.cp
cmp -- $pict $dir/$pict.cp
# Grab file sizes.
sz() { wc -c <$1 | tr -d ' '; }
sz_scramble=$(sz scramble.c)
sz_text=$(sz $text)
sz_pict=$(sz $pict)
sz_t_sc=$(sz $dir/$text.sc)
sz_p_sc=$(sz $dir/$pict.sc)
sz_t_sx=$(sz $dir/$text.sc.xz)
sz_p_sx=$(sz $dir/$pict.sc.xz)
# Find absolute difference of original minus .sc sizes.
abs_minus() : $(( $1 = $2 - $3, $1 = $1 < 0 ? -$1 : $1 ))
abs_minus d_t_sc sz_text sz_t_sc
abs_minus d_p_sc sz_pict sz_p_sc
# Find non-negative difference of original minus .sc.xz sizes.
ge0_minus() : $(( $1 = $2 - $3, $1 = $1 < 0 ? 0 : $1 ))
ge0_minus d_t_sx sz_text sz_t_sx
ge0_minus d_p_sx sz_pict sz_p_sx
: $(( score = sz_scramble + d_t_sc + d_p_sc + d_t_sx + d_p_sx ))
echo SCORE: $score=$sz_scramble+$d_t_sc+$d_p_sc+$d_t_sx+$d_p_sx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment