]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/commitdiff
Bash version of day5
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Sat, 5 Dec 2020 16:56:23 +0000 (16:56 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Sat, 5 Dec 2020 16:56:23 +0000 (16:56 +0000)
day5/get_seat_id.sh [new file with mode: 0644]

diff --git a/day5/get_seat_id.sh b/day5/get_seat_id.sh
new file mode 100644 (file)
index 0000000..971394a
--- /dev/null
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+parse_line() {
+    line=$1
+    row=${line:0:7}
+    col=${line:7}
+    row_number=$(row_part=${row//F/0}; row_part=${row_part//B/1}; echo $((2#$row_part)))
+    col_number=$(col_part=${col//L/0}; col_part=${col_part//R/1}; echo $((2#$col_part)))
+    seat_id=$((($row_number*8) + $col_number))
+    echo "$seat_id,$row_number,$col_number"
+}
+
+exec 3<input.txt
+
+declare -A seats
+
+while read -u 3 line; do
+    data=$(parse_line $line)
+    seat_id=${data%%,*}
+    row_col=${data#*,}
+    seats[$seat_id]=$row_col
+done
+
+sorted_seats=( $(echo "${!seats[@]}" | tr ' ' '\n' | sort -g) )
+
+echo Last seat id: ${sorted_seats[$((${#sorted_seats[@]}-1))]}
+
+# find our seat by going through the sorted list and finding the first mismatch
+seat_num=${sorted_seats[0]}
+for seat in ${sorted_seats[@]}; do
+    if [ $seat -ne $seat_num ]; then
+        echo "Your seat number is: $seat_num"
+        break
+    fi
+    seat_num=$((seat_num+1))
+done
+
+exit 0
+