DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 5: Binary Boarding

Collapse
 
tripledonkey profile image
tripledonkey

Hi, new to this site, and AOC. I did the last 4 in Python, but I thought I would try shell for this one (i'm not great at shell so this may be not the best):

I solved part 1 with this shell oneliner...

echo ibase=2 $(sed -e 's/F/0/g' -e 's/B/1/g' -e 's/L/0/g' -e 's/R/1/g' < passes) | sed 's/ /;/g' | bc | sort -n | tail -n1
Enter fullscreen mode Exit fullscreen mode

for part two, I created the list of passport ids with:

echo ibase=2 $(sed -e 's/F/0/g' -e 's/B/1/g' -e 's/L/0/g' -e 's/R/1/g' < passes) | sed 's/ /;/g' | bc > ids
Enter fullscreen mode Exit fullscreen mode

then threw this at the ids file:

#!/bin/bash
i=0; 
for seat in $(cat ids)
do
    ((i=i+1))
    ((prev=i-1))
    ((next=i+1)) 
    if grep --silent ^${prev}$ ids
    then
        if grep --silent ^${next}$ ids
        then
            if ! grep --silent ^${i}$ ids
            then
                echo "${i}"
            fi
        fi
    fi
done
Enter fullscreen mode Exit fullscreen mode