]> git.sommitrealweird.co.uk Git - advent-of-code-2019.git/blob - day04/get_valid_passwords.sh
Minor refactor start
[advent-of-code-2019.git] / day04 / get_valid_passwords.sh
1 #!/bin/bash
2
3 start_password=254032
4 end_password=789860
5
6 check_valid_1() {
7     cur_password=$1
8     last_val=${cur_password:0:1}
9     val=$last_val
10     got_dupe=0
11     if [ $cur_password -ge $start_password ] && [ $cur_password -le $end_password ]; then
12         for (( a=1; a<${#cur_password}; a++ )); do
13             val=${cur_password:$a:1}
14             if [ $val -ge $last_val ]; then
15                 if [ $val -eq $last_val ]; then
16                     got_dupe=1
17                 fi
18             else
19                 return 1
20             fi
21             last_val=$val
22         done
23         if [ $got_dupe -eq 1 ]; then
24             return 0
25         fi
26     else
27         return 1
28     fi
29
30     return 1
31 }
32
33 check_valid_2() {
34     cur_password=$1
35     last_val=${cur_password:0:1}
36     val=$last_val
37     dupe_count=1
38     got_dupe=0
39     if [ $cur_password -ge $start_password ] && [ $cur_password -le $end_password ]; then
40         for (( a=1; a<${#cur_password}; a++ )); do
41             val=${cur_password:$a:1}
42             if [ $val -ge $last_val ]; then
43                 if [ $val -eq $last_val ]; then
44                     dupe_count=$((dupe_count+1))
45                 else
46                     # only pairs
47                     if [ $dupe_count -eq 2 ]; then
48                         got_dupe=1
49                     fi
50                     dupe_count=1
51                 fi
52             else
53                 return 1
54             fi
55             last_val=$val
56         done
57         if [ $dupe_count -eq 2 ]; then
58             got_dupe=1
59         fi
60         if [ $got_dupe -eq 1 ]; then
61             return 0
62         else
63             return 1
64         fi
65     else
66         return 1
67     fi
68
69     return 1
70 }
71
72 get_next() {
73     password=$1
74     temp_password=$((password+1))
75     last_val=${temp_password:0:1}
76     new_password="$last_val"
77     for (( a=1; a<${#temp_password}; a++ )); do
78         val=${temp_password:$a:1}
79         if [ $val -lt $last_val ]; then
80             val=$last_val
81         fi
82         new_password+=$val
83         last_val=$val
84     done
85
86     echo "$new_password"
87 }
88
89 cur_password=$start_password
90 valid_passwords=0
91 valid_passwords_2=0
92 while [ $cur_password -le $end_password ]; do
93     echo -n "$cur_password: "
94     if ( check_valid_1 $cur_password ); then
95         echo -n valid
96         valid_passwords=$((valid_passwords+1))
97         if ( check_valid_2 $cur_password ); then
98             echo " valid"
99             valid_passwords_2=$((valid_passwords_2+1))
100         else
101             echo " invalid"
102         fi
103     else
104         echo invalid
105     fi
106     cur_password=$(get_next $cur_password)
107 done
108
109 echo "There are $valid_passwords valid passwords"
110 echo "And $valid_passwords_2 really valid passwords"