Day 4
This commit is contained in:
109
2024/day-4/solution-1.sh
Normal file
109
2024/day-4/solution-1.sh
Normal file
@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Horizontal search
|
||||
TOTAL_XMAS=$( grep -o 'XMAS' input.txt | wc -l )
|
||||
TOTAL_XMAS=$(( TOTAL_XMAS + $( grep -o 'SAMX' input.txt | wc -l ) ))
|
||||
|
||||
# Add spaces for cut use
|
||||
sed -E 's/(.)/\1\ /g' input.txt > input-proc
|
||||
FIELD_COUNT=$( head -1 input-proc | awk '{print NF}' )
|
||||
LINE_COUNT=$( cat input-proc | wc -l )
|
||||
INPUT_PROC=$( cat input-proc )
|
||||
|
||||
# Transpose for vertical search
|
||||
TOTAL_XMAS=$((
|
||||
TOTAL_XMAS + $(
|
||||
RSTRING=$(
|
||||
for i in $( seq 1 $FIELD_COUNT )
|
||||
do
|
||||
printf "$INPUT_PROC" | cut -f $i -d " " | paste -s -d ''
|
||||
done
|
||||
)
|
||||
echo $( printf "$RSTRING" | grep -o 'XMAS' | wc -l ) + $( printf "$RSTRING" | grep -o 'SAMX' | wc -l ) | bc
|
||||
)
|
||||
))
|
||||
|
||||
# Diagonal forward search
|
||||
for COL_START in $( seq 1 $FIELD_COUNT )
|
||||
do
|
||||
COL_COUNTER=$COL_START
|
||||
TOTAL_XMAS=$((
|
||||
TOTAL_XMAS + $(
|
||||
RSTRING=$(
|
||||
while read -r LINE
|
||||
do
|
||||
if [[ $COL_COUNTER -gt $FIELD_COUNT || $COL_COUNTER < 1 ]]
|
||||
then
|
||||
continue
|
||||
fi
|
||||
printf "$LINE" | cut -f $COL_COUNTER -d " "
|
||||
(( COL_COUNTER++ ))
|
||||
done <<< "$INPUT_PROC" | paste -s -d ""
|
||||
)
|
||||
echo $( printf "$RSTRING" | grep -o 'XMAS' | wc -l ) + $( printf "$RSTRING" | grep -o 'SAMX' | wc -l ) | bc
|
||||
)
|
||||
))
|
||||
done
|
||||
for ROW_START in $( seq 2 $LINE_COUNT )
|
||||
do
|
||||
COL_COUNTER=1
|
||||
TOTAL_XMAS=$((
|
||||
TOTAL_XMAS + $(
|
||||
RSTRING=$(
|
||||
while read -r LINE
|
||||
do
|
||||
if [[ $COL_COUNTER -gt $FIELD_COUNT || $COL_COUNTER < 1 ]]
|
||||
then
|
||||
continue
|
||||
fi
|
||||
printf "$LINE" | cut -f $COL_COUNTER -d " "
|
||||
(( COL_COUNTER++ ))
|
||||
done <<< "$( tail -n +$ROW_START input-proc )" | paste -s -d ""
|
||||
)
|
||||
echo $( printf "$RSTRING" | grep -o 'XMAS' | wc -l ) + $( printf "$RSTRING" | grep -o 'SAMX' | wc -l ) | bc
|
||||
)
|
||||
))
|
||||
done
|
||||
# Diagonal backwards search
|
||||
for COL_START in $( seq $FIELD_COUNT -1 1 )
|
||||
do
|
||||
COL_COUNTER=$COL_START
|
||||
TOTAL_XMAS=$((
|
||||
TOTAL_XMAS + $(
|
||||
RSTRING=$(
|
||||
while read -r LINE
|
||||
do
|
||||
if [[ $COL_COUNTER -gt $FIELD_COUNT || $COL_COUNTER < 1 ]]
|
||||
then
|
||||
continue
|
||||
fi
|
||||
printf "$LINE" | cut -f $COL_COUNTER -d " "
|
||||
(( COL_COUNTER-- ))
|
||||
done <<< "$INPUT_PROC" | paste -s -d ""
|
||||
)
|
||||
echo $( printf "$RSTRING" | grep -o 'XMAS' | wc -l ) + $( printf "$RSTRING" | grep -o 'SAMX' | wc -l ) | bc
|
||||
)
|
||||
))
|
||||
done
|
||||
for ROW_START in $( seq 2 $LINE_COUNT )
|
||||
do
|
||||
COL_COUNTER=$FIELD_COUNT
|
||||
TOTAL_XMAS=$((
|
||||
TOTAL_XMAS + $(
|
||||
RSTRING=$(
|
||||
while read -r LINE
|
||||
do
|
||||
if [[ $COL_COUNTER -gt $FIELD_COUNT || $COL_COUNTER < 1 ]]
|
||||
then
|
||||
continue
|
||||
fi
|
||||
printf "$LINE" | cut -f $COL_COUNTER -d " "
|
||||
(( COL_COUNTER-- ))
|
||||
done <<< "$( tail -n +$ROW_START input-proc )" | paste -s -d ""
|
||||
)
|
||||
echo $( printf "$RSTRING" | grep -o 'XMAS' | wc -l ) + $( printf "$RSTRING" | grep -o 'SAMX' | wc -l ) | bc
|
||||
)
|
||||
))
|
||||
done
|
||||
|
||||
printf "%s\n" "$TOTAL_XMAS"
|
Reference in New Issue
Block a user