diff --git a/day06.sh b/day06.sh new file mode 100644 index 0000000..9229076 --- /dev/null +++ b/day06.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +unset PATH +readonly PATH +source ./utils/main.sh + +coord_to_index() { + local width="$1" x="$2" y="$3" + printf '%d' "$((y * width + x))" +} + +index_to_coord() { + local width="$1" index="$2" + printf '%d %d\n' "$((index % width))" "$((index / width))" +} + +p1() { + local input="$1" width=0 height=0 + local line arr=() x y dir='u' + local -A visited + + while read -r line; do + ((height++)) + [[ "$width" ]] && width="${#line}" + while IFS= read -rd '' -n 1 char; do + [[ "$char" == $'\n' ]] && continue + arr+=("$char") + done <<<"$line" + done <<<"$input" + + local i + for i in "${!arr[@]}"; do + [[ "${arr[i]}" == '^' ]] && { + read -r x y <<<"$(index_to_coord "$width" "$i")" + arr[i]="." + visited=(["$x $y"]=1) + break + } + done + + while :; do + case "$dir" in + u) + ((y - 1 < 0)) && break + [[ "${arr["$(coord_to_index "$width" "$x" "$((y - 1))")"]}" == '#' ]] && dir='r' && continue + ((y--)) + ;; + r) + ((x + 1 >= width)) && break + [[ "${arr["$(coord_to_index "$width" "$((x + 1))" "$y")"]}" == '#' ]] && dir='d' && continue + ((x++)) + ;; + d) + ((y + 1 >= height)) && break + [[ "${arr["$(coord_to_index "$width" "$x" "$((y + 1))")"]}" == '#' ]] && dir='l' && continue + ((y++)) + ;; + l) + ((x - 1 < 0)) && break + [[ "${arr["$(coord_to_index "$width" "$((x - 1))" "$y")"]}" == '#' ]] && dir='u' && continue + ((x--)) + ;; + *) : ;; + esac + visited+=(["$x $y"]=1) + done + + printf '%s' "${#visited[@]}" +} + +main "$@" diff --git a/day07.sh b/day07.sh new file mode 100644 index 0000000..16f32a6 --- /dev/null +++ b/day07.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +unset PATH +readonly PATH +source ./utils/main.sh +source ./utils/array.sh +NUM_JOBS="\j" + +solve() { + local part="$1" target="$2" curr="$3" rest=("${@:4}") + + ((${#rest[@]} == 0)) && { + ((curr == target)) && echo "$target" && return 0 + return 1 + } + + solve "$part" "$target" "$((curr + rest[0]))" "${rest[@]:1}" || + solve "$part" "$target" "$((curr * rest[0]))" "${rest[@]:1}" || { + ((part == 1)) && return 1 + solve "$part" "$target" "${curr}${rest[0]}" "${rest[@]:1}" + } +} + +p1() { + local input="$1" result=0 + + while read -r num; do + ((result += num)) + done < <( + while read -r line; do + while ((${NUM_JOBS@P} >= 16)); do wait -n; done + { + local nums=() target + read -ra nums <<<"${line#*: }" + target="${line%:*}" + solve 1 "$target" "${nums[@]}" + } & + done <<<"$input" + wait + ) + + printf '%d' "$result" +} + +p2() { + local input="$1" result=0 + + while read -r num; do + ((result += num)) + done < <( + while read -r line; do + while ((${NUM_JOBS@P} >= 16)); do wait -n; done + { + local nums=() target + read -ra nums <<<"${line#*: }" + target="${line%:*}" + solve 2 "$target" "${nums[@]}" + } & + done <<<"$input" + wait + ) + + printf '%d' "$result" +} + +main "$@"