#!/usr/bin/env bash unset PATH readonly PATH source ./utils/main.sh source ./utils/array.sh solve() { local ax="$1" ay="$2" bx="$3" by="$4" px="$5" py="$6" tokens=0 a="$((px * by - py * bx))" b="$((py * ax - px * ay))" c="$((ax * by - ay * bx))" ((a % c == 0 && b % c == 0)) && tokens="$((3 * (a / c) + (b / c)))" echo "$tokens" } do_part() { local offset="$1" ax ay bx by result=0 while read -r line; do [[ -z "$line" ]] && continue [[ "$line" =~ Button\ A:\ X\+([0-9]*),\ Y\+([0-9]*) ]] && read -r ax ay <<<"${BASH_REMATCH[@]:1}" && continue [[ "$line" =~ Button\ B:\ X\+([0-9]*),\ Y\+([0-9]*) ]] && read -r bx by <<<"${BASH_REMATCH[@]:1}" && continue [[ "$line" =~ Prize:\ X=([0-9]*),\ Y=([0-9]*) ]] && ((result += $(solve "$ax" "$ay" "$bx" "$by" "$((BASH_REMATCH[1] + offset))" "$((BASH_REMATCH[2] + offset))"))) done printf '%s' "$result" } p1() { do_part 0 <<<"$1" } p2() { do_part 10000000000000 <<<"$1" } main "$@"