Part 1 solution for day 5
This commit is contained in:
36
2024/day-5/solution-1.sh
Normal file
36
2024/day-5/solution-1.sh
Normal file
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
while read -r LINE
|
||||
do
|
||||
GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' )
|
||||
PASS_RULES=0
|
||||
# Loop through rules and find a breaking rule
|
||||
while read -r RULE
|
||||
do
|
||||
GREPC=$( printf "%s" "$LINE" | grep -c "$RULE" )
|
||||
if [[ $GREPC -ne 1 ]]
|
||||
then
|
||||
#printf "%s rule violated for %s \n" "$RULE" "$LINE"
|
||||
PASS_RULES=1
|
||||
break
|
||||
fi
|
||||
# Filter rules that only has included pages
|
||||
done <<< "$( < input-rules grep -E "($GREP_EXPR)\\|($GREP_EXPR)" | sed 's/|/,.*/')"
|
||||
# All rules have passed
|
||||
if [[ $PASS_RULES -eq 0 ]]
|
||||
then
|
||||
printf "%s\n" "$LINE"
|
||||
fi
|
||||
done < input-updates |
|
||||
tr ',' ' ' |
|
||||
awk '
|
||||
BEGIN{ total = 0 }
|
||||
{
|
||||
if (NF % 2 == 1) {
|
||||
idx = int( NF / 2 ) + 1
|
||||
} else {
|
||||
idx = NF / 2
|
||||
}
|
||||
total += $idx
|
||||
}
|
||||
END{ print total }'
|
Reference in New Issue
Block a user