day 9 part 1 solution
This commit is contained in:
@ -40,6 +40,7 @@ do
|
||||
while true
|
||||
do
|
||||
HEAD=$(( A - DIFF * HEADC ))
|
||||
if [[ ${SHADOW_ARRAY[HEAD]} == "X" ]]; then (( HEADC++ )); continue ; fi
|
||||
HEADVDIFF=$(( (A / LEN) - (HEAD / LEN) ))
|
||||
# Check if out of bounds
|
||||
if [[ $HEAD -lt ${#MAP_ARRAY[@]} ]] && \
|
||||
@ -57,6 +58,7 @@ do
|
||||
while true
|
||||
do
|
||||
TAIL=$(( B + DIFF * TAILC ))
|
||||
if [[ ${SHADOW_ARRAY[TAIL]} == "X" ]]; then (( TAILC++ )); continue ; fi
|
||||
TAILVDIFF=$(( (TAIL / LEN) - (B / LEN) ))
|
||||
# Check if out of bounds
|
||||
if [[ $TAIL -lt ${#MAP_ARRAY[@]} ]] && \
|
||||
@ -75,6 +77,6 @@ do
|
||||
unset IDX_ARRAY
|
||||
|
||||
done
|
||||
printf "%s" "${SHADOW_ARRAY[@]}" | fold -w "$LEN" | sed -E 's/(.)/\1\ /g'
|
||||
printf "\n"
|
||||
#printf "%s" "${SHADOW_ARRAY[@]}" | fold -w "$LEN" | sed -E 's/(.)/\1\ /g'
|
||||
#printf "\n"
|
||||
printf "%s" "${SHADOW_ARRAY[@]}" | grep -o 'X' | wc -l
|
||||
|
1
2024/day-9/input
Normal file
1
2024/day-9/input
Normal file
File diff suppressed because one or more lines are too long
58
2024/day-9/solution-1.sh
Normal file
58
2024/day-9/solution-1.sh
Normal file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
FILE=input
|
||||
# Sparse sequence
|
||||
read -r -a SPARSE_ARRAY <<< "$(
|
||||
INDEX=0
|
||||
FILE_ID=0
|
||||
while read -r CHAR
|
||||
do
|
||||
if [[ $(( INDEX % 2 )) -eq 0 ]]
|
||||
then
|
||||
for (( i=1; i<=$CHAR; i++ )) do printf "%s " "$FILE_ID"
|
||||
done
|
||||
(( FILE_ID++ ))
|
||||
else
|
||||
for (( i=1; i<=$CHAR; i++ ))
|
||||
do
|
||||
printf ". "
|
||||
done
|
||||
fi
|
||||
(( INDEX++ ))
|
||||
done <<< "$( sed -E 's/(.)/\1\n/g' "$FILE" | grep -v '^$' )"
|
||||
)"
|
||||
#printf "%s" "${SPARSE_ARRAY[@]}"
|
||||
#printf "\n"
|
||||
printf "Sparse array len: %s\n" "${#SPARSE_ARRAY[@]}"
|
||||
|
||||
# Reverse feed
|
||||
read -r -a FEED_ARRAY <<< "$(
|
||||
for (( i=0; i<${#SPARSE_ARRAY[@]}; i++ ))
|
||||
do
|
||||
if [[ ${SPARSE_ARRAY[i]} != "." ]]
|
||||
then
|
||||
printf "%s " "${SPARSE_ARRAY[i]}"
|
||||
fi
|
||||
done
|
||||
)"
|
||||
#printf "%s" "${FEED_ARRAY[@]}"
|
||||
#printf "\n"
|
||||
printf "Reverse feed len : %s\n" "${#FEED_ARRAY[@]}"
|
||||
|
||||
# Generate checksum for sparse array
|
||||
CHECKSUM=0
|
||||
i=0
|
||||
j=$(( ${#FEED_ARRAY[@]} - 1 ))
|
||||
for (( k=0; k<${#SPARSE_ARRAY[@]}; k++ ))
|
||||
do
|
||||
if [[ $i -gt $j ]]; then break ; fi
|
||||
if [[ ${SPARSE_ARRAY[k]} == '.' ]]
|
||||
then
|
||||
(( CHECKSUM += k * FEED_ARRAY[j] ))
|
||||
(( j-- ))
|
||||
else
|
||||
(( CHECKSUM += k * FEED_ARRAY[i] ))
|
||||
(( i++ ))
|
||||
fi
|
||||
done
|
||||
printf "%s" "$CHECKSUM"
|
1
2024/day-9/test-input
Normal file
1
2024/day-9/test-input
Normal file
@ -0,0 +1 @@
|
||||
2333133121414131402
|
Reference in New Issue
Block a user