DEV Community

Cover image for AoC 2021 Day 01
JasonJPeters
JasonJPeters

Posted on

AoC 2021 Day 01

The countdown to Christmas has begun with various types of advent calendars being opened daily. According to mine Christmas is tomorrow.

This year for the first time I decided to take part in the Advent of Code 2021 challenge using the shell scripting language for GNU Bash.

Below you will find my solutions for the first days puzzles as well links to the GitHub hosted repo.

Puzzle 1

#!/usr/bin/env bash

data="$(dirname "$0")/data"

last=0
count=0

while read -r d;
do  
    if [ 0 != $last ]; then

        [[ $d > $last ]] && count=$((count+1))

    fi

    last=$((d))
done < "$data"

echo "Day 1(a) Answer: $count"
Enter fullscreen mode Exit fullscreen mode

Puzzle 2

#!/usr/bin/env bash

mapfile data < "$(dirname "$0")/data"

index=0
last=0
count=0

for i in "${data[@]}"
do
    unset "$i"

    a=${data[$((index))]}
    b=${data[$((index+1))]}
    c=${data[$((index+2))]}

    sum=$((a+b+c))

    if [ 0 != $last ]; then

        [[ $sum > $last ]] && count=$((count+1))

    fi

    last=$((sum))
    index=$((index+1))
done

echo "Day 1(b) Answer: $count"
Enter fullscreen mode Exit fullscreen mode

Discussion (0)