From a9e4013969ba39c66be5dbafdbbbde9194dd3fe6 Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 6 Dec 2024 01:09:33 +0800 Subject: [PATCH] Use awk match instead of grep loop --- 2024/day-5/solution-2.sh | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/2024/day-5/solution-2.sh b/2024/day-5/solution-2.sh index be100e7..2cbc576 100644 --- a/2024/day-5/solution-2.sh +++ b/2024/day-5/solution-2.sh @@ -1,25 +1,21 @@ #!/usr/bin/env bash +INPUT_RULES=$(cat input-rules) { while read -r LINE do GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' ) - MATCH_COUNT=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" - MATCH_COUNT=1 - break - fi - # Filter rules that only has included pages - done <<< "$( < input-rules grep -E "($GREP_EXPR)\\|($GREP_EXPR)" | sed 's/|/,.*/')" - + BEEG_AWK_EXPR=$( + printf "%s" "$INPUT_RULES" | + grep -E "($GREP_EXPR)\\|($GREP_EXPR)" | + sed -E 's/(.*)\|(.*)/\1,.*\2/; s&^&/&; s&$&/&' | + paste -s -d '@' | + sed 's/@/ \&\& /g' + ) + AWK_MATCH=$( printf "%s" "$LINE" | awk "$BEEG_AWK_EXPR" | wc -c ) + # Don't process for valid lines - [[ $MATCH_COUNT -eq 0 ]] && continue + if [[ $AWK_MATCH -gt 0 ]] ; then continue; fi printf "%s\n" "$LINE" done < input-updates