Day 13 Part 1 working
This commit is contained in:
1279
2024/day-13/input
Normal file
1279
2024/day-13/input
Normal file
File diff suppressed because it is too large
Load Diff
45
2024/day-13/solution-1.sh
Normal file
45
2024/day-13/solution-1.sh
Normal file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
FILE=input
|
||||
read -r -a INPUT_ARRAY <<< "$( sed -E 's/.*X[\+\=]([0-9]+), Y[\+\=]([0-9]+)/\1 \2 /g' < "$FILE" | paste -s -d " " )"
|
||||
printf "%s\n" "${INPUT_ARRAY[@]}" >&2
|
||||
INPUT_LEN=${#INPUT_ARRAY[@]}
|
||||
|
||||
TOTAL_TOKEN=0
|
||||
for (( i=0 ; i < INPUT_LEN; i+=6 ))
|
||||
do
|
||||
A=${INPUT_ARRAY[i]}
|
||||
C=${INPUT_ARRAY[i+1]}
|
||||
B=${INPUT_ARRAY[i+2]}
|
||||
D=${INPUT_ARRAY[i+3]}
|
||||
X=${INPUT_ARRAY[i+4]}
|
||||
Y=${INPUT_ARRAY[i+5]}
|
||||
|
||||
I=$(( ( B*Y - D*X ) / ( B*C - D*A ) )) # Button A
|
||||
J=$(( ( X - A*I ) / B )) # Button B
|
||||
|
||||
TOKEN=$(( I*3 + J*1 ))
|
||||
|
||||
{
|
||||
printf "Answer: I= %s, J= %s\n" "$I" "$J"
|
||||
printf "Verify answer : %s to %s, %s to %s\n" "$(( A*I + B*J ))" "$X" "$(( C*I + D*J ))" "$Y"
|
||||
printf "Total cost: %s\n" "$(( I*3 + J*1 ))"
|
||||
} >&2
|
||||
|
||||
if ! [[ $(( A*I + B*J )) -eq $X ]]
|
||||
then
|
||||
continue
|
||||
elif ! [[ $(( C*I + D*J )) -eq $Y ]]
|
||||
then
|
||||
continue
|
||||
elif [[ $I -lt 1 ]] || [[ $J -lt 1 ]]
|
||||
then
|
||||
continue
|
||||
elif [[ $I -gt 100 ]] || [[ $J -gt 100 ]]
|
||||
then
|
||||
continue
|
||||
else
|
||||
(( TOTAL_TOKEN+=TOKEN ))
|
||||
fi
|
||||
done
|
||||
printf "Total token: %s\n" "$TOTAL_TOKEN"
|
45
2024/day-13/solution-2.sh
Normal file
45
2024/day-13/solution-2.sh
Normal file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
FILE=input
|
||||
read -r -a INPUT_ARRAY <<< "$( sed -E 's/.*X[\+\=]([0-9]+), Y[\+\=]([0-9]+)/\1 \2 /g' < "$FILE" | paste -s -d " " )"
|
||||
printf "%s\n" "${INPUT_ARRAY[@]}" >&2
|
||||
INPUT_LEN=${#INPUT_ARRAY[@]}
|
||||
|
||||
TOTAL_TOKEN=0
|
||||
for (( i=0 ; i < INPUT_LEN; i+=6 ))
|
||||
do
|
||||
A=${INPUT_ARRAY[i]}
|
||||
C=${INPUT_ARRAY[i+1]}
|
||||
B=${INPUT_ARRAY[i+2]}
|
||||
D=${INPUT_ARRAY[i+3]}
|
||||
X="10000000000000${INPUT_ARRAY[i+4]}"
|
||||
Y="10000000000000${INPUT_ARRAY[i+5]}"
|
||||
|
||||
I=$(( ( B*Y - D*X ) / ( B*C - D*A ) )) # Button A
|
||||
J=$(( ( X - A*I ) / B )) # Button B
|
||||
|
||||
TOKEN=$(( I*3 + J*1 ))
|
||||
|
||||
{
|
||||
printf "Answer: I= %s, J= %s\n" "$I" "$J"
|
||||
printf "Verify answer : %s to %s, %s to %s\n" "$(( A*I + B*J ))" "$X" "$(( C*I + D*J ))" "$Y"
|
||||
printf "Total cost: %s\n" "$(( I*3 + J*1 ))"
|
||||
} >&2
|
||||
|
||||
if ! [[ $(( A*I + B*J )) -eq $X ]]
|
||||
then
|
||||
continue
|
||||
elif ! [[ $(( C*I + D*J )) -eq $Y ]]
|
||||
then
|
||||
continue
|
||||
elif [[ $I -lt 1 ]] || [[ $J -lt 1 ]]
|
||||
then
|
||||
continue
|
||||
#elif [[ $I -gt 100 ]] || [[ $J -gt 100 ]]
|
||||
#then
|
||||
# continue
|
||||
else
|
||||
(( TOTAL_TOKEN+=TOKEN ))
|
||||
fi
|
||||
done
|
||||
printf "Total token: %s\n" "$TOTAL_TOKEN"
|
15
2024/day-13/test-input-1
Normal file
15
2024/day-13/test-input-1
Normal file
@ -0,0 +1,15 @@
|
||||
Button A: X+94, Y+34
|
||||
Button B: X+22, Y+67
|
||||
Prize: X=8400, Y=5400
|
||||
|
||||
Button A: X+26, Y+66
|
||||
Button B: X+67, Y+21
|
||||
Prize: X=12748, Y=12176
|
||||
|
||||
Button A: X+17, Y+86
|
||||
Button B: X+84, Y+37
|
||||
Prize: X=7870, Y=6450
|
||||
|
||||
Button A: X+69, Y+23
|
||||
Button B: X+27, Y+71
|
||||
Prize: X=18641, Y=10279
|
Reference in New Issue
Block a user