From b6a36aabd338e029195b2901bd04181fe189e6da Mon Sep 17 00:00:00 2001 From: Patrick Michl Date: Thu, 1 Dec 2022 17:45:12 +0100 Subject: [PATCH] add utils.sh and dynamicaly find base directory --- aoc.jl | 6 +++++- src/day_1.sh | 25 ++++--------------------- utils.sh | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 utils.sh diff --git a/aoc.jl b/aoc.jl index e129133..6e6fcc2 100755 --- a/aoc.jl +++ b/aoc.jl @@ -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 diff --git a/src/day_1.sh b/src/day_1.sh index 7c33291..acd61f4 100644 --- a/src/day_1.sh +++ b/src/day_1.sh @@ -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="$( 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() { diff --git a/utils.sh b/utils.sh new file mode 100644 index 0000000..20d3505 --- /dev/null +++ b/utils.sh @@ -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[@]}" +}