--- /dev/null
+#!/bin/bash
+
+start_password=254032
+end_password=789860
+
+check_valid_1() {
+ cur_password=$1
+ last_val=${cur_password:0:1}
+ val=$last_val
+ got_dupe=0
+ if [ $cur_password -ge $start_password ] && [ $cur_password -le $end_password ]; then
+ for (( a=1; a<${#cur_password}; a++ )); do
+ val=${cur_password:$a:1}
+ if [ $val -ge $last_val ]; then
+ if [ $val -eq $last_val ]; then
+ got_dupe=1
+ fi
+ else
+ return 1
+ fi
+ last_val=$val
+ done
+ if [ $got_dupe -eq 1 ]; then
+ return 0
+ fi
+ else
+ return 1
+ fi
+
+ return 1
+}
+
+check_valid_2() {
+ cur_password=$1
+ last_val=${cur_password:0:1}
+ val=$last_val
+ dupe_count=1
+ got_dupe=0
+ if [ $cur_password -ge $start_password ] && [ $cur_password -le $end_password ]; then
+ for (( a=1; a<${#cur_password}; a++ )); do
+ val=${cur_password:$a:1}
+ if [ $val -ge $last_val ]; then
+ if [ $val -eq $last_val ]; then
+ dupe_count=$((dupe_count+1))
+ else
+ # only pairs
+ if [ $dupe_count -eq 2 ]; then
+ got_dupe=1
+ fi
+ dupe_count=1
+ fi
+ else
+ return 1
+ fi
+ last_val=$val
+ done
+ if [ $dupe_count -eq 2 ]; then
+ got_dupe=1
+ fi
+ if [ $got_dupe -eq 1 ]; then
+ return 0
+ else
+ return 1
+ fi
+ else
+ return 1
+ fi
+
+ return 1
+}
+
+get_next() {
+ password=$1
+ temp_password=$((password+1))
+ last_val=${temp_password:0:1}
+ new_password="$last_val"
+ for (( a=1; a<${#temp_password}; a++ )); do
+ val=${temp_password:$a:1}
+ if [ $val -lt $last_val ]; then
+ val=$last_val
+ fi
+ new_password+=$val
+ last_val=$val
+ done
+
+ echo "$new_password"
+}
+
+cur_password=$start_password
+valid_passwords=0
+valid_passwords_2=0
+while [ $cur_password -le $end_password ]; do
+ echo -n "$cur_password: "
+ if ( check_valid_1 $cur_password ); then
+ echo -n valid
+ valid_passwords=$((valid_passwords+1))
+ if ( check_valid_2 $cur_password ); then
+ echo " valid"
+ valid_passwords_2=$((valid_passwords_2+1))
+ else
+ echo " invalid"
+ fi
+ else
+ echo invalid
+ fi
+ cur_password=$(get_next $cur_password)
+done
+
+echo "There are $valid_passwords valid passwords"
+echo "And $valid_passwords_2 really valid passwords"