add day 1

This commit is contained in:
Patrick Michl
2022-12-01 08:19:26 +01:00
commit 14b097c61d
4 changed files with 2410 additions and 0 deletions

49
src/day_1.sh Normal file
View File

@@ -0,0 +1,49 @@
#!/usr/bin/env bash
unset PATH
INPUT="$(<data/day_1.txt)"
qsort() {
read -ra arr <<< "$@"
[[ "${#arr[@]}" == 0 ]] && echo "" && return
local smaller=() bigger=() pivot="${arr[0]}" res=()
for i in "${arr[@]}"; do
if [[ "$i" -lt "$pivot" ]]; then
smaller+=("$i")
elif [[ "$i" -gt "$pivot" ]]; then
bigger+=("$i")
fi
done
read -ra smaller_sorted <<< "$(qsort "${smaller[@]}")"
read -ra bigger_sorted <<< "$(qsort "${bigger[@]}")"
res=("${smaller_sorted[@]}" "$pivot" "${bigger_sorted[@]}")
echo "${res[@]}"
}
cals_per_elf() {
mapfile -t list <<< "$1"
local cals=() tmp=0
for i in "${list[@]}"; do
[[ "$i" == '' ]] && cals+=("$tmp") && tmp=0 && continue
((tmp+=i))
done
cals+=("$tmp")
qsort "${cals[@]}"
}
p1() {
read -ra cals <<< "$(cals_per_elf "$1")"
echo "${cals[@]: -1}"
}
p2() {
read -ra cals <<< "$(cals_per_elf "$1")"
local result=0
for i in "${cals[@]: -3}"; do ((result+=i)); done
echo "$result"
}
p1 "$INPUT"
p2 "$INPUT"