Some bash cleanup (Day19/part 2)
This commit is contained in:
14
OUTPUT
14
OUTPUT
@@ -429,3 +429,17 @@ 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
|
||||
|
8
day19/OUTPUT
Normal file
8
day19/OUTPUT
Normal file
@@ -0,0 +1,8 @@
|
||||
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
|
||||
|
10
day19/README
10
day19/README
@@ -54,8 +54,6 @@ 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:
|
||||
@@ -138,8 +136,10 @@ 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?
|
||||
|
||||
Answer:
|
||||
Your puzzle answer was 412.
|
||||
|
||||
Although it hasn't changed, you can still get your puzzle input.
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
You can also [Share] this puzzle.
|
||||
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.
|
||||
|
@@ -8,8 +8,8 @@ shopt -s extglob
|
||||
|
||||
declare -a RULE=() MATCH=() STRING=()
|
||||
# shellcheck disable=SC2034
|
||||
declare var_0
|
||||
declare -i res=0 MAXPROF=0
|
||||
declare var_0 rule8 rule11
|
||||
declare -i res=0
|
||||
|
||||
# build a regexp use-able by bash =~ operator.
|
||||
# Recursively replace rules, until we get final text values.
|
||||
@@ -23,8 +23,6 @@ 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"
|
||||
@@ -69,29 +67,25 @@ done
|
||||
|
||||
|
||||
# hack:
|
||||
# 1) build part 1 tree
|
||||
# 1) build rules 42 and 31
|
||||
# 2) calculate rule 8 and 11 with some "guessed" minimum possible value
|
||||
# 3) rebuild tree from scratch, but with rules 8 and 11 pre-loaded
|
||||
buildtree 1 var_0 0
|
||||
# 3) build rule 0
|
||||
|
||||
# This value was found manually: stable from 5.
|
||||
# result is "stable" from this value
|
||||
SOME_LIMIT=5
|
||||
tmp8="(${MATCH[42]})+"
|
||||
tmp11="(${MATCH[42]}${MATCH[31]})"
|
||||
|
||||
buildtree 1 var_0 42
|
||||
buildtree 1 var_0 31
|
||||
rule8="${MATCH[42]}+"
|
||||
rule11="${MATCH[42]}${MATCH[31]}"
|
||||
for ((i=2; i<SOME_LIMIT; ++i)); do
|
||||
tmp11+="|((${MATCH[42]}){$i}(${MATCH[31]}){$i})"
|
||||
rule11+="|${MATCH[42]}{$i}${MATCH[31]}{$i}"
|
||||
done
|
||||
|
||||
MATCH=()
|
||||
MATCH[8]="$tmp8"
|
||||
MATCH[11]="($tmp11)"
|
||||
MATCH[8]="$rule8"
|
||||
MATCH[11]="($rule11)"
|
||||
buildtree 1 var_0 0
|
||||
|
||||
res=0
|
||||
for str in "${STRING[@]}"; do
|
||||
[[ "$str" =~ ^${MATCH[0]}$ ]] && ((res++))
|
||||
done
|
||||
|
||||
printf "%s : res=%d\n" "$CMD" "$res"
|
||||
|
||||
exit 0
|
||||
|
Reference in New Issue
Block a user