Skip to content

Instantly share code, notes, and snippets.

@ljyang
Created December 3, 2022 14:17
Show Gist options
  • Save ljyang/ca608a511d9cd54f738b2ee1baf1310c to your computer and use it in GitHub Desktop.
Save ljyang/ca608a511d9cd54f738b2ee1baf1310c to your computer and use it in GitHub Desktop.
aoc day3
Part one script
*************************************************
#!/bin/bash
file="day3.txt"
dupes=""
declare -i tally
while read -r line; do
linelen=${#line}
let linehalf=$linelen/2
a=$(echo "$line" | cut -c1-$linehalf)
b=$(echo "$line" | cut -c$(($linehalf+1))-$linelen)
for (( c=0; c<$linehalf; c++))
do
if [ `expr index $b ${a:$c:1}` -gt 0 ]; then
dupes+=${a:$c:1}
break
fi
done
done <$file
echo "$dupes"
for (( d=0; d<${#dupes}; d++))
do
curchar=${dupes:$d:1}
ascii=$(printf '%d' "'$curchar")
if [ $ascii -gt 91 ];
then
ascii=$((ascii-=96))
else
ascii=$((ascii-=38))
fi
tally+=$ascii
done
echo "$tally"
*************************************************
Part two script
*************************************************
#!/bin/bash
file="day3.txt"
dupes=""
declare -i tally
while true; do
read -r one || break
read -r two || break
read -r three || break
linelen=${#one}
let linehalf=$linelen/2
for (( c=0; c<$linelen; c++))
do
if [[ `expr index $two ${one:$c:1}` -gt 0 && `expr index $three ${one:$c:1}` -gt 0 ]]; then
dupes+=${one:$c:1}
break
fi
done
done <$file
echo "$dupes"
for (( d=0; d<${#dupes}; d++))
do
curchar=${dupes:$d:1}
ascii=$(printf '%d' "'$curchar")
if [ $ascii -gt 91 ];
then
ascii=$((ascii-=96))
else
ascii=$((ascii-=38))
fi
tally+=$ascii
done
echo "$tally"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment