aoc2024/day01.sh
Patrick Michl 85ea5d299b
shfmt
2024-12-05 18:25:04 +01:00

48 lines
733 B
Bash
Executable File

#!/usr/bin/env bash
unset PATH
readonly PATH
source ./utils/main.sh
source ./utils/array.sh
p1() {
local input="$1" a b tmp i
local left=() right=() res=()
while read -r a b; do
left+=("$a")
right+=("$b")
done <<<"$input"
array.timsort left
array.timsort right
for ((i = 0; i < ${#left[@]}; i++)); do
tmp="$((left[i] - right[i]))"
res+=("${tmp#-}")
done
array.sum res
}
p2() {
local input="$1" a b i j
local left=() right=() res=()
while read -r a b; do
left+=("$a")
right+=("$b")
done <<<"$input"
for i in "${left[@]}"; do
local tmp=0
for j in "${right[@]}"; do
((i == j)) && ((tmp++))
done
res+=("$((i * tmp))")
done
array.sum res
}
main "$@"