day 6 fix bug
[aoc_eblake.git] / 2015 / advent11.sh
blobd2cc761d33ed7e4199d2ea1ca98b37b50b6fd9a8
1 valid() {
2 case $1 in
3 *[iol]*) return 1;;
4 *abc*|*bcd*|*cde*|*def*|*efg*|*fgh*|*pqr*|*qrs*|*rst*\
5 |*stu*|*tuv*|*uvw*|*vwx*|*wxy*|*xyz*) ;;
6 *) return 1;;
7 esac
8 first=$(echo $1 | sed '/\(.\)\1/{s/.*\(.\)\1.*/\1/;q}; Q1') || return 1
9 echo $1 | sed -n "/\([^$first]\)\1/q; q1"
11 next() {
12 case $1 in
13 *i?*) next=${1%i*}j
14 next+=${a:${#next}}
15 return;;
16 *l?*) next=${1%l*}m
17 next+=${a:${#next}}
18 return;;
19 *o?*) next=${1%o*}p
20 next+=${a:${#next}}
21 return;;
22 esac
23 next=$(echo $1 | sed 's/z$/0/; s/y$/z/; s/x$/y/; s/w$/x/; s/v$/w/;
24 s/u$/v/; s/t$/u/; s/s$/t/; s/r$/s/; s/q$/r/;
25 s/p$/q/; s/[no]$/p/; s/m$/n/; s/[kl]$/m/;
26 s/j$/k/; s/[hi]$/j/; s/g$/h/; s/f$/g/; s/e$/f/;
27 s/d$/e/; s/c$/d/; s/b$/c/; s/a$/b/; s/0/0a/')
28 while case $next in *0*) : ;; *) false ;; esac; do
29 next=$(echo $next | sed 's/z0/1/; s/y0/z/; s/x0/y/; s/w0/x/; s/v0/w/;
30 s/u0/v/; s/t0/u/; s/s0/t/; s/r0/s/; s/q0/r/;
31 s/p0/q/; s/n0/p/; s/m0/n/; s/k0/m/;
32 s/j0/k/; s/h0/j/; s/g0/h/; s/f0/g/; s/e0/f/;
33 s/d0/e/; s/c0/d/; s/b0/c/; s/a0/b/; s/1/0a/')
34 done
36 for arg; do
37 a=$(printf '%0*d' ${#arg} 0 | tr 0 a)
38 tries=0
39 echo "searching for password after $arg"
40 while ! valid $arg; do
41 tries=$((tries+1))
42 next $arg
43 arg=$next
44 (( $tries % 1000 )) || echo "trying $next"
45 done
46 echo "found $next after $tries tries"
47 done