Compare commits

..

1 Commits

Author SHA1 Message Date
8bc0e74e10 Day 19 part 2: some bash cleanup 2021-07-21 17:45:33 +02:00
4 changed files with 17 additions and 35 deletions

14
OUTPUT
View File

@@ -429,17 +429,3 @@ ex2.bash : res=20394514442037
ex12-c : res=20394514442037
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+74
=========================================
================= day19 =================
=========================================
+++++++++++++++++ ex1
ex1.bash : res=285
time: 0:00.56 real, 0.55 user, 0.00 sys
context-switch: 12+1, page-faults: 0+5967
+++++++++++++++++ ex2
ex2.bash : res=412
time: 0:03.37 real, 3.34 user, 0.03 sys
context-switch: 19+1, page-faults: 0+11909

View File

@@ -1,8 +0,0 @@
ex1.bash : res=285
time: 0:00.56 real, 0.55 user, 0.00 sys
context-switch: 12+1, page-faults: 0+5967
ex2.bash : res=412
time: 0:03.37 real, 3.34 user, 0.03 sys
context-switch: 19+1, page-faults: 0+11909

View File

@@ -54,6 +54,8 @@ Your goal is to determine the number of messages that completely match rule 0. I
How many messages completely match rule 0?
Your puzzle answer was 285.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
As you look over the list of messages, you realize your matching rules aren't quite right. To fix them, completely replace rules 8: 42 and 11: 42 31 with the following:
@@ -136,10 +138,8 @@ However, after updating rules 8 and 11, a total of 12 messages match:
After updating rules 8 and 11, how many messages completely match rule 0?
Your puzzle answer was 412.
Answer:
Both parts of this puzzle are complete! They provide two gold stars: **
Although it hasn't changed, you can still get your puzzle input.
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Share] this puzzle.

View File

@@ -8,8 +8,8 @@ shopt -s extglob
declare -a RULE=() MATCH=() STRING=()
# shellcheck disable=SC2034
declare var_0 rule8 rule11
declare -i res=0
declare var_0
declare -i res=0 MAXPROF=0
# build a regexp use-able by bash =~ operator.
# Recursively replace rules, until we get final text values.
@@ -23,6 +23,8 @@ function buildtree {
shift 2
local args=$* res="" arg t
((prof > MAXPROF)) && MAXPROF=$prof
for arg in $args; do
if [[ -z "${arg/[|ab]}" ]]; then
res+="$arg"
@@ -71,19 +73,21 @@ done
# 2) calculate rule 8 and 11 with some "guessed" minimum possible value
# 3) build rule 0
# result is "stable" from this value
# result starts to be "stable" from this value
SOME_LIMIT=5
buildtree 1 var_0 42
buildtree 1 var_0 31
rule8="${MATCH[42]}+"
rule11="${MATCH[42]}${MATCH[31]}"
tmp8="(${MATCH[42]})+"
tmp11="(${MATCH[42]}${MATCH[31]})"
for ((i=2; i<SOME_LIMIT; ++i)); do
rule11+="|${MATCH[42]}{$i}${MATCH[31]}{$i}"
tmp11+="|((${MATCH[42]}){$i}(${MATCH[31]}){$i})"
done
MATCH[8]="$rule8"
MATCH[11]="($rule11)"
MATCH[8]="$tmp8"
MATCH[11]="($tmp11)"
buildtree 1 var_0 0
res=0
for str in "${STRING[@]}"; do
[[ "$str" =~ ^${MATCH[0]}$ ]] && ((res++))
done