1 # visit n limit prefix... # output all visits of n items that sum to limit
10 while [[ $i -le $limit ]]; do
11 visit $
((n
- 1)) $
((limit
- i
)) $
* $i
16 [[ ! ${DEBUG+set} ]] && return
21 regex
='(.*): capacity (.*), durability (.*), flavor (.*),'
22 regex
+=' texture (.*), calories (.*)'
24 [[ $line =~
$regex ]] ||
{ echo 'unable to parse line'; exit 1; }
25 name
[$count]=${BASH_REMATCH[1]}
26 cap
[$count]=${BASH_REMATCH[2]}
27 dur
[$count]=${BASH_REMATCH[3]}
28 fla
[$count]=${BASH_REMATCH[4]}
29 tex
[$count]=${BASH_REMATCH[5]}
30 cal[$count]=${BASH_REMATCH[6]}
33 echo "parsed details on $count ingredients"
34 # score values... # set score to the combination of the values
37 debug
"score for weights $key: "
38 if [[ $# != $count ]]; then
39 echo "wrong number of weights"; exit 1
41 local weight
=0 count
=0 i
44 if [[ $1 -lt 0 || $
(($1 + weight
)) -gt 100 ]]; then
45 echo "huh? $1 $i $weight"
49 weight
=$
((weight
+ $1))
50 c
=$
((c
+ $1 * ${cap[$count]}))
51 d
=$
((d
+ $1 * ${dur[$count]}))
52 f
=$
((f
+ $1 * ${fla[$count]}))
53 t
=$
((t
+ $1 * ${tex[$count]}))
54 C
=$
((C
+ $1 * ${cal[$count]}))
58 if [[ $C != 500 ]]; then
60 elif case $c$d$f$t in *-*) score
=0; false
;; *) : ;; esac; then
61 score
=$
((c
* d
* f
* t
))
67 ! ((tries
% 1000)) && echo $tries
70 [[ $best -lt $score ]] && best
=$score
71 done < <(visit
$count 100)
72 echo "after $tries tries, best score is $best"