diff --git a/OUTPUT b/OUTPUT index a2494d2..f610ba2 100644 --- a/OUTPUT +++ b/OUTPUT @@ -443,3 +443,11 @@ ex1.bash : res=285 ex2.bash : res=412 time: 0:03.37 real, 3.34 user, 0.03 sys context-switch: 19+1, page-faults: 0+11909 + +========================================= +================= day20 ================= +========================================= + +ex1.bash : res=5966506063747 + time: 0:00.54 real, 0.53 user, 0.00 sys + context-switch: 1+1, page-faults: 0+223 diff --git a/day20/Makefile b/day20/Makefile index 224acfb..62c581b 100644 --- a/day20/Makefile +++ b/day20/Makefile @@ -20,7 +20,7 @@ ex1: @#$(TIME) ex1-c 2020 < $(INPUT) 2>&1 ex2: - @$(TIME) ex2.bash < $(INPUT) 2>&1 + @#$(TIME) ex2.bash < $(INPUT) 2>&1 @#$(TIME) ex1-c 30000000 < $(INPUT) 2>&1 clean: diff --git a/day20/OUTPUT b/day20/OUTPUT new file mode 100644 index 0000000..3c22426 --- /dev/null +++ b/day20/OUTPUT @@ -0,0 +1,4 @@ +ex1.bash : res=5966506063747 + time: 0:00.54 real, 0.53 user, 0.00 sys + context-switch: 1+1, page-faults: 0+223 + diff --git a/day20/ex1.bash b/day20/ex1.bash index 589dfff..ecf642d 100755 --- a/day20/ex1.bash +++ b/day20/ex1.bash @@ -54,18 +54,21 @@ for key in "${!nums[@]}"; do done -ALL=("${T[@]}" "${R[@]}" "${B[@]}" "${L[@]}" "${RT[@]}" "${RR[@]}" "${RB[@]}" "${RL[@]}") +ALL="${T[@]} ${R[@]} ${B[@]} ${L[@]} ${RT[@]} ${RR[@]} ${RB[@]} ${RL[@]}" +ALLSIZE=${#ALL} + declare -i res=1 count for ((i=0; i<${#nums[@]}; ++i)); do count=0 for t in ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]}; do - for s in "${ALL[@]}"; do - [[ $t == "$s" ]] && ((count++)) - done + # https://stackoverflow.com/questions/26212889/bash-counting-substrings-in-a-string/50601141#50601141 + S=${ALL//$t} + # 10 is line size + ((count += (ALLSIZE-${#S})/10)) done - - ((count ==6)) && ((res*=${nums[$i]})) + # 6 is 4 for itself, + 2 for other matching + ((count == 6)) && ((res*=${nums[$i]})) done printf "%s : res=%d\n" "$CMD" "$res"