add utils.sh and dynamicaly find base directory

This commit is contained in:
Patrick Michl 2022-12-01 17:45:12 +01:00
parent 0b28781160
commit b6a36aabd3
No known key found for this signature in database
GPG Key ID: BFE0ACEE21CD5EB0
3 changed files with 29 additions and 22 deletions

6
aoc.jl
View File

@ -16,7 +16,11 @@ function _cookie()
end
function _get_input(year, day)
res = HTTP.get("https://adventofcode.com/$year/day/$day/input", cookies = _cookie())
res = HTTP.get(
"https://adventofcode.com/$year/day/$day/input",
cookies = _cookie(),
headers = Dict("User-Agent", "git.fuckwit.dev/fuckwit/aoc2022/src/branch/master/aoc.jl by huanzodev@gmail.com")
)
if res.status 200
error("Unable to fetch infput for AOC $year day $day")
end

View File

@ -1,26 +1,9 @@
#!/usr/bin/env bash
unset PATH
BASE='.*';until [[ "$(eval "echo $BASE")" =~ \.git ]]; do BASE="../$BASE"; done;BASE="${BASE/'*'}"
source "$BASE/utils.sh"
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 < pivot)); then
smaller+=("$i")
elif ((i > 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[@]}"
}
INPUT="$(<"$BASE/data/day_1.txt")"
cals_per_elf() {
mapfile -t list <<< "$1"
@ -30,7 +13,7 @@ cals_per_elf() {
((tmp+=i))
done
cals+=("$tmp")
qsort "${cals[@]}"
utils.qsort "${cals[@]}"
}
p1() {

20
utils.sh Normal file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env bash
utils.qsort() {
read -ra arr <<< "$@"
((${#arr[@]} == 0)) && echo && return
local smaller=() bigger=() pivot="${arr[0]}" res=()
for i in "${arr[@]}"; do
if ((i < pivot)); then
smaller+=("$i")
elif ((i > pivot)); then
bigger+=("$i")
fi
done
read -ra smaller_sorted <<< "$(utils.qsort "${smaller[@]}")"
read -ra bigger_sorted <<< "$(utils.qsort "${bigger[@]}")"
res=("${smaller_sorted[@]}" "$pivot" "${bigger_sorted[@]}")
echo "${res[@]}"
}