Bash: 2022 day 4 part 2

This commit is contained in:
2022-12-08 09:50:06 +01:00
parent 76ab3d0c5b
commit 325c8254b8
3 changed files with 28 additions and 5 deletions

View File

@@ -63,3 +63,17 @@ aoc.bash: res=2760
aoc-c: res=2760
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+88
=========================================
================= day04 =================
=========================================
+++++++++++++++++ part 1
aoc.bash: res=444
time: 0:00.04 real, 0.04 user, 0.00 sys
context-switch: 5+1, page-faults: 0+338
+++++++++++++++++ part 2
aoc.bash: res=801
time: 0:00.04 real, 0.03 user, 0.00 sys
context-switch: 0+1, page-faults: 0+336

View File

@@ -67,8 +67,6 @@ pairs.
Your puzzle answer was =444=.
The first half of this puzzle is complete! It provides one gold star: *
** --- Part Two ---
It seems like there is still quite a bit of duplicate work planned.
Instead, the Elves would like to know the number of pairs that /overlap
@@ -87,4 +85,6 @@ So, in this example, the number of overlapping assignment pairs is =4=.
/In how many assignment pairs do the ranges overlap?/
Answer:
Your puzzle answer was =801=.
Both parts of this puzzle are complete! They provide two gold stars: **

View File

@@ -22,11 +22,9 @@ parse() {
while IFS=-, read -ra _arr; do
# arrange the two sections so that the lowest is the first
printf "_arr=%s\n" "${_arr[*]}"
if ((_arr[0] > _arr[2])); then
((_tmp=_arr[0], _arr[0]=_arr[2], _arr[2]=_tmp))
((_tmp=_arr[1], _arr[1]=_arr[3], _arr[3]=_tmp))
printf "\t->%s\n" "${_arr[*]}"
fi
sections+=("${_arr[*]}")
done
@@ -45,6 +43,17 @@ part1() {
done
}
part2() {
declare -ig res=0
local -a _sect
for line in "${sections[@]}"; do
# shellcheck disable=SC2206
_sect=($line)
(( _sect[2] <= _sect[1] )) && (( res++ ))
done
}
solve() {
if (($1 == 1)); then
part1