blob: 9c011d5f8b332bc349f000eabc954d04f52e7c93 (
plain) (
blame)
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
|
#!/usr/bin/env bash
set -eufCo pipefail
function play_one() (
local game="$1"
local id=0
id=$(echo "$game" | cut -d' ' -f2 | cut -d':' -f1)
while read -r set; do
local red=12
local green=13
local blue=14
while read -r item; do
local n=0
n=$(echo "$item" | cut -d' ' -f1)
if [[ "$item" =~ "red" ]]; then
red=$((red - n))
elif [[ "$item" =~ "green" ]]; then
green=$((green - n))
elif [[ "$item" =~ "blue" ]]; then
blue=$((blue - n))
fi
done < <(echo "$set" | tr ',' '\n')
if [[ "$red" -lt 0 || "$green" -lt 0 || "$blue" -lt 0 ]]; then
echo 0; return 0;
fi
done < <(echo "$game" | cut -d':' -f2 | cut -d' ' -f2- | tr ';' '\n')
echo "$id"
)
function play_two() (
local game="$1"
local id=0
id=$(echo "$game" | cut -d' ' -f2 | cut -d':' -f1)
local colors=()
while read -r set; do
local red=0
local green=0
local blue=0
while read -r item; do
local n=0
n=$(echo "$item" | cut -d' ' -f1)
if [[ "$item" =~ "red" ]]; then
red=$((red + n))
elif [[ "$item" =~ "green" ]]; then
green=$((green + n))
elif [[ "$item" =~ "blue" ]]; then
blue=$((blue + n))
fi
done < <(echo "$set" | tr ',' '\n')
colors+=("$red $green $blue")
done < <(echo "$game" | cut -d':' -f2 | cut -d' ' -f2- | tr ';' '\n')
local red=0
local green=0
local blue=0
for i in "${colors[@]}"; do
rbg=($i)
[[ "${rbg[0]}" -gt "$red" ]] && red="${rbg[0]}"
[[ "${rbg[1]}" -gt "$green" ]] && green="${rbg[1]}"
[[ "${rbg[2]}" -gt "$blue" ]] && blue="${rbg[2]}"
done
echo "$((red * green * blue))"
)
sum_one=0
sum_two=0
while read -r game; do
sum_one=$((sum_one + $(play_one "$game")))
sum_two=$((sum_two + $(play_two "$game")))
done < <(cat 02.txt)
echo "$sum_one"
echo "$sum_two"
|