2022 day 1: prepare bash common code + bash solutions

This commit is contained in:
2022-12-01 10:28:40 +01:00
parent 0fb4219c92
commit dbff06e5da
6 changed files with 2421 additions and 12 deletions

56
2022/day01/aoc.bash Executable file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/env bash
#
# aoc.bash: Advent of Code 2022, day 1
#
# Copyright (C) 2022 Bruno Raoult ("br")
# Licensed under the GNU General Public License v3.0 or later.
# Some rights reserved. See COPYING.
#
# You should have received a copy of the GNU General Public License along with this
# program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
. common.bash
declare -a total
declare max=0
declare -i res
parse() {
local -i elf=0
while read -r line; do
if [[ -n $line ]]; then
(( total[elf] += line))
else
(( total[elf] > max )) && (( max = total[elf] ))
((elf++))
fi
done
}
part1() {
res=$max
}
part2() {
local -i i elf newbest
res=0
for ((i=0; i<3; ++i)); do
newbest=0
for ((elf=0; elf<${#total[@]}; ++elf)); do
if (( total[elf] > total[newbest] )); then
newbest=$elf
fi
done
(( res+=total[newbest] ))
unset "total[$newbest]" # remove current max
total=("${total[@]}") # pack array
#printf "%d: %s\n" "$i" "${total[*]}"
done
}
main "$@"
printf "%s: res=%s\n" "$cmdname" "$res"
exit 0