Part 1 solution for day 5

This commit is contained in:
2024-12-05 15:26:49 +08:00
parent 6b8948af09
commit 6a0ef4fea6
3 changed files with 1422 additions and 0 deletions

36
2024/day-5/solution-1.sh Normal file
View 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 }'