From 67ae111303e1fa7be067ec699b1d9736150c608e Mon Sep 17 00:00:00 2001 From: Patrick Michl Date: Sat, 7 Dec 2024 18:42:37 +0100 Subject: [PATCH] reduce code duplication in day07 --- day07.sh | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/day07.sh b/day07.sh index 16f32a6..b77a7e7 100644 --- a/day07.sh +++ b/day07.sh @@ -20,8 +20,8 @@ solve() { } } -p1() { - local input="$1" result=0 +do_part() { + local part="$1" result=0 while read -r num; do ((result += num)) @@ -32,34 +32,21 @@ p1() { local nums=() target read -ra nums <<<"${line#*: }" target="${line%:*}" - solve 1 "$target" "${nums[@]}" + solve "$part" "$target" "${nums[@]}" } & - done <<<"$input" + done wait ) printf '%d' "$result" } +p1() { + do_part 1 <<<"$1" +} + p2() { - local input="$1" result=0 - - while read -r num; do - ((result += num)) - done < <( - while read -r line; do - while ((${NUM_JOBS@P} >= 16)); do wait -n; done - { - local nums=() target - read -ra nums <<<"${line#*: }" - target="${line%:*}" - solve 2 "$target" "${nums[@]}" - } & - done <<<"$input" - wait - ) - - printf '%d' "$result" + do_part 2 <<<"$1" } main "$@"