2 [[ ! ${DEBUG+set} ]] && return
6 # http://www.geeksforgeeks.org/heaps-algorithm-for-generating-permutations/
7 # _permute size n # helper for permute, munges $arr
9 debug
"$indent in _permute $@ ${arr[*]}"
11 if [[ $1 == 1 ]]; then
17 local size
=$1 n
=$2 tmp i
19 while [[ $i -lt $size ]]; do
20 _permute $
((size-1
)) $n
21 if [[ $
((size
%2)) == 1 ]]; then
22 debug
"$indent $size odd, swapping first and last"
24 arr
[0]=${arr[$((size-1))]}
27 debug
"$indent $size even, swapping $i and last"
29 arr
[$i]=${arr[$((size-1))]}
34 debug
"$indent leaving $size $n ${arr[*]}"
37 # permute n # outputs n! lines, for each possible permutation
40 while [[ $i -lt $1 ]]; do
50 while read a amt b
; do
52 case " ${people[*]} " in
54 *) people
[$
((count
++))]=$a ;;
57 done < <(sed 's/would gain //; s/would lose /-/; s/happ.*next to //; s/.$//')
58 echo "read $lines lines, found ${#change[*]} rules among ${#people[*]} people:"
60 [[ ! $part1 ]] && people
+=("you")
62 while read first rest
; do
63 echo trying
$first $rest
68 sum=$
((${change[${people[$prev]}.${people[$next]}]}+sum))
69 sum=$
((${change[${people[$next]}.${people[$prev]}]}+sum))
73 [[ $sum -gt $max ]] && max
=$sum
74 done < <(permute
${#people[*]})
75 echo "max happiness $max"