add utils.sh and dynamicaly find base directory
This commit is contained in:
parent
0b28781160
commit
b6a36aabd3
6
aoc.jl
6
aoc.jl
@ -16,7 +16,11 @@ function _cookie()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function _get_input(year, day)
|
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
|
if res.status ≠ 200
|
||||||
error("Unable to fetch infput for AOC $year day $day")
|
error("Unable to fetch infput for AOC $year day $day")
|
||||||
end
|
end
|
||||||
|
25
src/day_1.sh
25
src/day_1.sh
@ -1,26 +1,9 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
unset PATH
|
unset PATH
|
||||||
|
BASE='.*';until [[ "$(eval "echo $BASE")" =~ \.git ]]; do BASE="../$BASE"; done;BASE="${BASE/'*'}"
|
||||||
|
source "$BASE/utils.sh"
|
||||||
|
|
||||||
INPUT="$(<data/day_1.txt)"
|
INPUT="$(<"$BASE/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[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
cals_per_elf() {
|
cals_per_elf() {
|
||||||
mapfile -t list <<< "$1"
|
mapfile -t list <<< "$1"
|
||||||
@ -30,7 +13,7 @@ cals_per_elf() {
|
|||||||
((tmp+=i))
|
((tmp+=i))
|
||||||
done
|
done
|
||||||
cals+=("$tmp")
|
cals+=("$tmp")
|
||||||
qsort "${cals[@]}"
|
utils.qsort "${cals[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
p1() {
|
p1() {
|
||||||
|
20
utils.sh
Normal file
20
utils.sh
Normal 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[@]}"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user