Skip to content

Instantly share code, notes, and snippets.

@brimstone
Created January 1, 2014 04:44
Show Gist options
  • Save brimstone/8205166 to your computer and use it in GitHub Desktop.
Save brimstone/8205166 to your computer and use it in GitHub Desktop.
Add operations to and solve: 10 9 8 7 6 5 4 3 2 1 = 2014
#!/bin/bash
s=$1
e=$[ $s + 1 ]
for a in $(seq $(echo "4^8 * $s" | bc -l) $(echo "4^8 * $e" | bc -l)); do
n=$(echo "obase=4;$a" | bc)
f=$(
( for b in $(seq 1 $[ 9 - ${#n} ]); do
printf "0"; done; echo "$n"
) | sed -e 's/\(.\)/\1\n/g' | tr '0123' '+\-*/' | for a in {10..1}; do
read o
printf "$a $o "
done
echo
)
printf "$f= "
echo "$f" | bc -l
done
Usage:
./2014 0 > out.0
./2014 1 > out.1
./2014 2 > out.2
./2014 3 > out.3
10 * 9 * 8 * 7 / 6 / 5 * 4 * 3 - 2 - 1 = 2013
10 * 9 * 8 * 7 / 6 / 5 * 4 * 3 - 2 * 1 = 2014
10 * 9 * 8 * 7 / 6 / 5 * 4 * 3 - 2 / 1 = 2014
10 * 9 * 8 * 7 / 6 / 5 * 4 * 3 - 2 + 1 = 2015
10 * 9 * 8 * 7 / 6 / 5 * 4 * 3 + 2 - 1 = 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment