-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.sh
30 lines (28 loc) · 798 Bytes
/
03.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
#!bin/bash
r1=0
r2=0
ok=1
while read -r line; do
matches=$(echo "$line" | grep -o 'mul([0-9]\+,[0-9]\+)' | sed 's/mul(//;s/)//')
for m in $matches; do
IFS=',' read -r L R <<< "$m"
#echo "$m"
r1=$(($L*$R+$r1))
done
### egrep works well, grep works too
matches=$(grep -oE "mul\([0-9]+,[0-9]+\)|do\(\)|don't\(\)" <<< $line)
#matches=$(egrep -o "mul\([0-9]+,[0-9]+\)|do\(\)|don't\(\)" <<< $line)
for m in $matches; do
if [[ $m == "do()" ]]; then
ok=1
elif [[ $m == "don't()" ]]; then
ok=0
elif [[ $ok == 1 ]]; then
sub=$(sed 's/mul(//;s/)//' <<< $m)
IFS=',' read -r L R <<< "$sub"
r2=$(($L*$R+$r2))
fi
done
done
echo "part 1: $r1"
echo "part 2: $r2"