-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.sh
57 lines (43 loc) · 1.2 KB
/
day2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# Advent of Code 2020, Day 2
# (c) blu3r4y
# path to input file
INPUT="./data/day2.txt"
# number of valid passwords for part 1 and 2
NVALID_ONE=0
NVALID_TWO=0
# read the file line by line
while IFS= read -r line
do
_IFS="$IFS"
# replace the field delimiters with '#'
line=${line//: /#}
line=${line// /#}
line=${line//-/#}
# parse to an array named policy
IFS="#" read -ra policy <<< "$line"
# extract individual policy parts
low="${policy[0]}"
high="${policy[1]}"
char="${policy[2]}"
pass="${policy[3]}"
# count number of requested character
# (by replacing all others and getting the length)
cleaned="${pass//[^${char}]}"
nchars="${#cleaned}"
# condition for part 1
if [[ $low -le $nchars ]] && [[ $nchars -le $high ]]
then NVALID_ONE=$[$NVALID_ONE + 1]; fi
lbracket=0
if [[ ${pass:$[low-1]:1} == $char ]]
then lbracket=1; fi
hbracket=0
if [[ ${pass:$[high-1]:1} == $char ]]
then hbracket=1; fi
# condition for part 2 (with not-equal instead of xor)
if [[ $lbracket -ne $hbracket ]]
then NVALID_TWO=$[$NVALID_TWO + 1]; fi
IFS="$_IFS"
done < "$INPUT"
echo $NVALID_ONE
echo $NVALID_TWO