aoc22

2022 Advent of Code
git clone https://git.parazyd.org/aoc22
Log | Files | Refs

02.sh (616B)


      1 #!/bin/sh
      2 
      3 common=0
      4 
      5 while read -r line; do
      6 	range0="${line%,*}"
      7 	min0="${range0%-*}"
      8 	max0="${range0#*-}"
      9 
     10 	range1="${line#*,}"
     11 	min1="${range1%-*}"
     12 	max1="${range1#*-}"
     13 
     14 	if [ "$min1" -ge "$min0" ] && [ "$min1" -le "$max0" ]; then
     15 		common="$(( common + 1 ))"
     16 		continue
     17 	fi
     18 
     19 	if [ "$max1" -ge "$min0" ] && [ "$max1" -le "$max0" ]; then
     20 		common="$(( common + 1 ))"
     21 		continue
     22 	fi
     23 
     24 	if [ "$min0" -ge "$min1" ] && [ "$min0" -le "$max1" ]; then
     25 		common="$(( common + 1 ))"
     26 		continue
     27 	fi
     28 
     29 	if [ "$max0" -ge "$min1" ] && [ "$max0" -le "$max1" ]; then
     30 		common="$(( common + 1 ))"
     31 		continue
     32 	fi
     33 done
     34 
     35 echo "$common"