-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.sh
executable file
·99 lines (90 loc) · 2.38 KB
/
part1.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
file="./input.txt";
monkeyCount=-1;
monkeyItems=();
monkeyOps=();
monkeyTest=();
monkeyTrue=();
monkeyFalse=();
monkeyActivity=();
while read -r line; do
if [[ "$line" =~ ^Monkey ]]; then
((monkeyCount++));
monkeyActivity[$monkeyCount]=0;
elif [[ "$line" =~ ^Starting\ items: ]]; then
monkeyItems[$monkeyCount]=$(echo "${line:15}" | sed s/\ //g);
elif [[ "$line" =~ ^Operation:\ new\ \= ]]; then
monkeyOps[$monkeyCount]=$(echo "${line:16}" | sed s/\ //g);
elif [[ "$line" =~ ^Test:\ divisible\ by ]]; then
monkeyTest[$monkeyCount]="${line:19}";
elif [[ "$line" =~ ^If\ true:\ throw\ to\ monkey ]]; then
monkeyTrue[$monkeyCount]="${line:25}";
elif [[ "$line" =~ ^\If\ false:\ throw\ to\ monkey ]]; then
monkeyFalse[$monkeyCount]="${line:26}";
fi
done < $file;
((monkeyCount++));
# evalOps old ops -> new
function evalOps {
old=$1;
ops=$2;
if [ "${ops:0:3}" == "old" ]; then
if [ "${ops:3:1}" == "*" ]; then
if [ "${ops:4}" == "old" ]; then
echo $((old * old));
else
echo $((old * ${ops:4}));
fi
elif [ "${ops:3:1}" == "+" ]; then
if [ "${ops:4}" == "old" ]; then
echo $((old + old));
else
echo $((old + ${ops:4}));
fi
else
exit 1;
fi
else
exit 1;
fi
}
for (( round = 0; round < 20; round++ )); do
echo "Round $round";
for (( monkey = 0; monkey < monkeyCount; monkey++ )); do
items=(${monkeyItems[$monkey]//,/ });
if [ ${#items[@]} -eq 0 ]; then
continue;
fi
ops=${monkeyOps[$monkey]};
test=${monkeyTest[$monkey]};
true=${monkeyTrue[$monkey]};
false=${monkeyFalse[$monkey]};
for (( itemI = 0; itemI < ${#items[@]}; itemI++ )); do
((monkeyActivity[$monkey]++));
# inspect
item=${items[$itemI]};
# evalOps
item=$(evalOps $item $ops);
# div 3
((item /= 3));
# test and throw
if (( item % test == 0 )); then
monkeyItems[$true]+=",$item";
else
monkeyItems[$false]+=",$item";
fi
monkeyItems[$monkey]="";
done
done;
done;
first=0;
second=0;
for (( monkey = 0; monkey < monkeyCount; monkey++ )); do
if (( ${monkeyActivity[monkey]} > first )); then
second=$first;
first=${monkeyActivity[monkey]};
elif (( ${monkeyActivity[monkey]} > second )); then
second=${monkeyActivity[monkey]};
fi
done
echo "Answer: $((first * second))";