]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blob - day05/get_seat_id.sh
Day 14 - implemented in bash, because, well, I could
[advent-of-code-2020.git] / day05 / get_seat_id.sh
1 #!/bin/bash
2
3 parse_line() {
4     line=$1
5     row=${line:0:7}
6     col=${line:7}
7     row_number=$(row_part=${row//F/0}; row_part=${row_part//B/1}; echo $((2#$row_part)))
8     col_number=$(col_part=${col//L/0}; col_part=${col_part//R/1}; echo $((2#$col_part)))
9     seat_id=$((($row_number*8) + $col_number))
10     echo "$seat_id,$row_number,$col_number"
11 }
12
13 exec 3<input.txt
14
15 declare -A seats
16
17 while read -u 3 line; do
18     data=$(parse_line $line)
19     seat_id=${data%%,*}
20     row_col=${data#*,}
21     seats[$seat_id]=$row_col
22 done
23
24 sorted_seats=( $(echo "${!seats[@]}" | tr ' ' '\n' | sort -g) )
25
26 echo Last seat id: ${sorted_seats[$((${#sorted_seats[@]}-1))]}
27
28 # find our seat by going through the sorted list and finding the first mismatch
29 seat_num=${sorted_seats[0]}
30 for seat in ${sorted_seats[@]}; do
31     if [ $seat -ne $seat_num ]; then
32         echo "Your seat number is: $seat_num"
33         break
34     fi
35     seat_num=$((seat_num+1))
36 done
37
38 exit 0
39