Add some one liners to other_ways for day5
[advent-of-code-2020.git] / day2 / get_valid_count.sh
1 #!/bin/bash
2
3 check_file() {
4     cat input.txt | while read line; do
5         password=${line#*: }
6         params=${line%: *}
7         min=${params%-*}
8         params=${params#*-}
9         max=${params% *}
10         char=${params#* }
11         count=0
12         for ((i=0; i<${#password}; i++)); do
13             if [ "${password:$i:1}" = "$char" ]; then
14                 count=$((count+1))
15             fi
16         done
17         if [ $count -ge $min ] && [ $count -le $max ]; then
18             echo "Got valid line!"
19         fi
20     done
21 }
22
23 echo "Got $(check_file | wc -l) valid lines"
24
25 exit 0
26 #!/usr/bin/python3
27
28 import regex
29
30 total_lines=0
31 valid_lines=0
32 for line in open("input.txt", "r"):
33     total_lines += 1
34     (min_count, max_count, letter, password) = regex.match('([0-9]+)-([0-9]+) ([a-z]): ([a-z]*)', line).group(1,2,3,4)
35     min_count=int(min_count)
36     max_count=int(max_count)
37     count = 0
38     for x in password:
39         if x == letter:
40             count += 1
41     if count >= min_count and count <= max_count:
42         valid_lines += 1
43
44 print(valid_lines, "valid lines of", total_lines)