aoc2024/day01.sh
Patrick Michl 8fea880ef9
initial
2024-12-01 14:17:35 +01:00

50 lines
799 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]))"
echo "${left[$i]} - ${right[$i]} = ${tmp#-}"
res+=("${tmp#-}")
done > log
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 "$@"