]> git.sommitrealweird.co.uk Git - advent-of-code-2019.git/blobdiff - day04/get_valid_passwords.sh
Day 15
[advent-of-code-2019.git] / day04 / get_valid_passwords.sh
old mode 100644 (file)
new mode 100755 (executable)
index 9dd1562..7410186
@@ -1,12 +1,16 @@
 #!/bin/bash
 
-start_password=254032
-end_password=789860
+filename=${1:-input.txt}
+exec 3<"${filename}"
+
+read -u 3 line
+
+start_password=${line%-*}
+end_password=${line#*-}
 
 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
@@ -78,6 +82,11 @@ get_next() {
         val=${temp_password:$a:1}
         if [ $val -lt $last_val ]; then
             val=$last_val
+            for (( b=$a; b<${#temp_password}; b++ )); do
+                new_password+=$val
+            done
+            echo "$new_password"
+            return 0
         fi
         new_password+=$val
         last_val=$val
@@ -90,18 +99,20 @@ cur_password=$start_password
 valid_passwords=0
 valid_passwords_2=0
 while [ $cur_password -le $end_password ]; do
-    echo -n "$cur_password: "
+    #echo -n "$cur_password: "
     if ( check_valid_1 $cur_password ); then
-        echo -n valid
+        #echo -n valid
         valid_passwords=$((valid_passwords+1))
         if ( check_valid_2 $cur_password ); then
-            echo " valid"
+            #echo " valid"
             valid_passwords_2=$((valid_passwords_2+1))
         else
-            echo " invalid"
+            #echo " invalid"
+            true
         fi
     else
-        echo invalid
+        #echo invalid
+        true
     fi
     cur_password=$(get_next $cur_password)
 done