From 9f6182ca9fa3848404c50d1e11896b0e1264e1a6 Mon Sep 17 00:00:00 2001 From: Clement Date: Thu, 5 Dec 2024 22:51:53 +0800 Subject: [PATCH] Use bash arrays, pipe from part 1 to part 2 --- 2024/day-5/solution-2.sh | 89 ++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/2024/day-5/solution-2.sh b/2024/day-5/solution-2.sh index a62fa22..5c88056 100644 --- a/2024/day-5/solution-2.sh +++ b/2024/day-5/solution-2.sh @@ -1,49 +1,58 @@ #!/usr/bin/env bash -rm key-* -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 +{ + while read -r LINE 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/|/,.*/')" - - # Don't process for valid lines - [[ $PASS_RULES -eq 0 ]] && continue - - # Generate a hashmap and rebuild the entire line from rules - CACHED_LINE=$LINE - while [[ $( printf "%s" "$CACHED_LINE" | tr ',' '\n' | wc -c ) -gt 0 ]] - do - # Loop through rules and build all the KVs - rm key-* - DAG_GREP_EXPR=$( printf "%s" "$CACHED_LINE" | tr ',' '|' ) + GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' ) + PASS_RULES=0 + # Loop through rules and find a breaking rule while read -r RULE do - KEY=$( printf "%s" "$RULE" | cut -f1 -d'|' ) - VALUE=$( printf "%s" "$RULE" | cut -f2 -d'|' ) - printf "%s" "$VALUE" > key-"$KEY" - done <<< "$( < input-rules grep -E "($DAG_GREP_EXPR)\\|($DAG_GREP_EXPR)" )" - KEY=$( printf "%s" "$CACHED_LINE" | tr ',' '\n' | head -1 ) - # Going through dependencies to find final key - while [[ -f key-$KEY ]] + 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/|/,.*/')" + + # Don't process for valid lines + [[ $PASS_RULES -eq 0 ]] && continue + printf "%s\n" "$LINE" + + done < input-updates +} | { + while read -r LINE # Generate a hashmap and rebuild the entire line from rules + do + CACHED_LINE=$( printf "%s" "$LINE" | tr ',' '\n' ) + while [[ $( printf "%s" "$CACHED_LINE" | wc -c ) -gt 0 ]] do - KEY=$( cat "key-$KEY" ) - done - printf "%s\n" "$KEY" - CACHED_LINE=$( printf "%s" "$CACHED_LINE" | tr ',' '\n' | grep -v "$KEY" | paste -s -d ',' ) - done | tac | paste -s -d' ' -done < input-updates | + # Loop through rules and build all the KVs + DAG_GREP_EXPR=$( printf "%s" "$CACHED_LINE" | paste -s -d '|' ) + KV_ARRAY=( $( < input-rules grep -E "($DAG_GREP_EXPR)\\|($DAG_GREP_EXPR)" | tr '|' ' ' | paste -s -d ' ') ) + # Going through dependencies to find final key + KEY=${KV_ARRAY[0]} + while true + do + BREAK=0 + for (( i=0; i<${#KV_ARRAY[@]}; i+=2 )) # Check if key exists + do + if [[ ${KV_ARRAY[$i]} -eq $KEY ]] + then + KEY=${KV_ARRAY[$i+1]} + BREAK=1 + break + fi + done + if [[ $BREAK -eq 1 ]] ; then continue ; else break ; fi + done + printf "%s\n" "$KEY" + CACHED_LINE=$( printf "%s" "$CACHED_LINE" | grep -v "$KEY" ) + done | tac | paste -s -d' ' + done +} | tee final-output | awk ' BEGIN{ total = 0 }