From f2ef26fa6e2d81742b6f59d16669f5680ba505ba Mon Sep 17 00:00:00 2001 From: Pierre Jeanjean Date: Fri, 10 Dec 2021 10:55:53 +0100 Subject: [PATCH] Day 10 --- 10/01.sh | 42 ++++++++++++++++++++++++++++++++++++++ 10/02.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100755 10/01.sh create mode 100755 10/02.sh diff --git a/10/01.sh b/10/01.sh new file mode 100755 index 0000000..2d8672b --- /dev/null +++ b/10/01.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +score=0 +while read line; do + chars=( $(sed 's/\(.\)/\1 /g' <<< $line) ) + opened_chars=() + for char in ${chars[@]}; do + case $char in + '('|'['|'{'|'<') + opened_chars+=( $char );; + ')') + if [ ${opened_chars[-1]} = '(' ]; then + unset 'opened_chars[-1]' + else + score=$((score+3)) + break + fi;; + ']') + if [ ${opened_chars[-1]} = '[' ]; then + unset 'opened_chars[-1]' + else + score=$((score+57)) + break + fi;; + '}') + if [ ${opened_chars[-1]} = '{' ]; then + unset 'opened_chars[-1]' + else + score=$((score+1197)) + break + fi;; + '>') + if [ ${opened_chars[-1]} = '<' ]; then + unset 'opened_chars[-1]' + else + score=$((score+25137)) + break + fi;; + esac + done +done +echo $score diff --git a/10/02.sh b/10/02.sh new file mode 100755 index 0000000..2c6715c --- /dev/null +++ b/10/02.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +scores=() +while read line; do + chars=( $(sed 's/\(.\)/\1 /g' <<< $line) ) + opened_chars=() + broken=0 + for char in ${chars[@]}; do + case $char in + '('|'['|'{'|'<') + opened_chars+=( $char );; + ')') + if [ ${opened_chars[-1]} = '(' ]; then + unset 'opened_chars[-1]' + else + broken=1 + break + fi;; + ']') + if [ ${opened_chars[-1]} = '[' ]; then + unset 'opened_chars[-1]' + else + broken=1 + break + fi;; + '}') + if [ ${opened_chars[-1]} = '{' ]; then + unset 'opened_chars[-1]' + else + broken=1 + break + fi;; + '>') + if [ ${opened_chars[-1]} = '<' ]; then + unset 'opened_chars[-1]' + else + broken=1 + break + fi;; + esac + done + if [ $broken -eq 0 ]; then + score=0 + n_opened_chars=${#opened_chars[@]} + for i in $(seq $((n_opened_chars-1)) -1 0); do + case ${opened_chars[$i]} in + '(') + score=$((score*5+1));; + '[') + score=$((score*5+2));; + '{') + score=$((score*5+3));; + '<') + score=$((score*5+4));; + esac + done + scores+=( $score ) + fi +done +scores=( $(printf '%s\n' "${scores[@]}" | sort -nr) ) +n_scores=${#scores[@]} +echo ${scores[$((n_scores/2))]}