diff --git a/1a.sh b/1a.sh new file mode 100755 index 0000000..cac1e31 --- /dev/null +++ b/1a.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +max=0 +c=0 + +while read a; do + if [[ -z "$a" ]]; then + [[ $c -gt $max ]] && max=$c + c=0 + else + let c+=$a + fi +done + +echo $max diff --git a/1b.sh b/1b.sh new file mode 100755 index 0000000..b91daae --- /dev/null +++ b/1b.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +max1=0 +max2=0 +max3=0 +c=0 + +while read a; do + if [[ -n "$a" ]]; then + let c+=$a + else + if [[ $c -gt $max1 ]]; then + max3=$max2 + max2=$max1 + max1=$c + elif [[ $c -gt $max2 ]]; then + max3=$max2 + max2=$c + elif [[ $c -gt $max3 ]]; then + max3=$c + fi + c=0 + fi +done + +echo $(( $max1 + $max2 + $max3 )) diff --git a/2a.sh b/2a.sh new file mode 100755 index 0000000..f67af28 --- /dev/null +++ b/2a.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +declare -A A B C + +A['X']=3 +A['Y']=6 +A['Z']=0 +B['X']=0 +B['Y']=3 +B['Z']=6 +C['X']=6 +C['Y']=0 +C['Z']=3 +X=1 +Y=2 +Z=3 +s=0 + +while read a b; do + declare -n elf=$a + declare -n me=$b + let s+=$(( ${elf[$b]} + $me )) +done + +echo $s diff --git a/2b.sh b/2b.sh new file mode 100755 index 0000000..3299b0d --- /dev/null +++ b/2b.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +declare -A A B C + +A['X']=3 +A['Y']=4 +A['Z']=8 + +B['X']=1 +B['Y']=5 +B['Z']=9 + +C['X']=2 +C['Y']=6 +C['Z']=7 +s=0 + +while read a b; do + declare -n elf=$a + declare -n me=$b + let s+=$(( ${elf[$b]} )) +done + +echo $s diff --git a/3a.sh b/3a.sh new file mode 100755 index 0000000..a5330d7 --- /dev/null +++ b/3a.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +declare -A P + +p=0 + +for i in {a..z} {A..Z}; do + let p++ + P[$i]=$p +done + +s=0 + +while read a; do + l=${#a} + ll=$(( $l / 2 )) + x=${a:0:$ll} + y=${a:$ll} + + c='' + + for i in `seq 0 $(( $ll - 1 ))`; do + if grep -q ${x:$i:1} <<< ${y}; then + c=${x:$i:1} + break + fi + done + + let s+=P[$c] + +done + +echo $s + + diff --git a/3b.sh b/3b.sh new file mode 100755 index 0000000..791887a --- /dev/null +++ b/3b.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash + + +function lowi() { + low=${#1} + i=0 + li=0 + shift + + for l in "$@"; do + let i++ + if [ ${#l} -lt $low ]; then + li=$i + low=${#l} + fi + done + + echo $li +} + +declare -A P + +p=0 + +for i in {a..z} {A..Z}; do + let p++ + P[$i]=$p +done + + +sum=0 + +while read a; read b; read c; do + bags=( $a $b $c ) + + li=$(lowi $a $b $c) + + x=${bags[li]} + y=${bags[$(( (li + 1) % 3 ))]} + z=${bags[$(( (li + 2) % 3 ))]} + c1='' + + for i in `seq 0 $(( ${#bags[$li]} - 1 ))`; do + if [[ $y == *${x:$i:1}* ]]; then + c1="${c1}${x:$i:1}" + fi + done + + for i in `seq 0 $(( ${#c1} - 1 ))`; do + if [[ $z == *${c1:$i:1}* ]]; then + c=${c1:$i:1} + break + fi + done + + let sum+=P[$c] + c='' + +done + +echo $sum + + diff --git a/4a.sh b/4a.sh new file mode 100755 index 0000000..b1fb1b1 --- /dev/null +++ b/4a.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +sum=0 + +while read a; do + IFS=, read x y <<< $a + IFS=- read x1 x2 <<< $x + IFS=- read y1 y2 <<< $y + + if [[ $(( (x1 - y1) * (x2 - y2) )) -le 0 ]]; then + let sum++ + fi + +done + +echo $sum + + diff --git a/4b.sh b/4b.sh new file mode 100755 index 0000000..c9fa6e7 --- /dev/null +++ b/4b.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +sum=0 + +while read a; do + IFS=, read x y <<< $a + IFS=- read x1 x2 <<< $x + IFS=- read y1 y2 <<< $y + + if [[ $(( (x1 - y2) * (x2 - y1) )) -le 0 ]]; then + let sum++ + fi + +done + +echo $sum + diff --git a/5a.sh b/5a.sh new file mode 100755 index 0000000..c0f96ba --- /dev/null +++ b/5a.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +sum=0 + +declare -a C +popped='' + +function push() { + C[$1]="${C[$1]}${2}" +} + +function pop() { + l=$(( ${#C[$1]} - 1 )) + popped=${C[$1]:$l:1} + C[$1]="${C[$1]:0:$l}" +} + +while read a; do + if [[ "$a" == "1 2 3 4 5 6 7 8 9" ]]; then + break + fi + for i in {0..8}; do + c=${a:$(( 4 * $i + 1)):1} + if [[ "$c" == ' ' ]]; then + c='' + fi + + C[$i]="${c}${C[$i]}" + done +done + +read + +while read x1 n x2 f x3 t; do + let f-- + let t-- + + for i in `seq 1 $n`; do + pop $f + push $t $popped + done +done + +out="" +for i in {0..8}; do + pop $i + echo -n "$popped" +done +echo diff --git a/5b.sh b/5b.sh new file mode 100755 index 0000000..22982c9 --- /dev/null +++ b/5b.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +sum=0 + +declare -a C +popped='' + +function push() { + C[$1]="${C[$1]}${2}" +} + +function pop() { + a=${2:-1} + l=$(( ${#C[$1]} - $a )) + popped=${C[$1]:$l:$a} + C[$1]="${C[$1]:0:$l}" +} + +while read a; do + if [[ "$a" == "1 2 3 4 5 6 7 8 9" ]]; then + break + fi + for i in {0..8}; do + c=${a:$(( 4 * $i + 1)):1} + if [[ "$c" == ' ' ]]; then + c='' + fi + + C[$i]="${c}${C[$i]}" + done +done + +read + +while read x1 n x2 f x3 t; do + let f-- + let t-- + + pop $f $n + push $t $popped +done + +out="" +for i in {0..8}; do + pop $i + echo -n "$popped" +done +echo diff --git a/6a.sh b/6a.sh new file mode 100755 index 0000000..69535a2 --- /dev/null +++ b/6a.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +winl=4 +win='' + +function add() { + win="${win:1}${1}" +} + +function contains() { + # $1 what + # $2 where + local l=${#2} + local ret=1 + + while (( l-- )); do + [[ "$1" = "${2:$l:1}" ]] && ret=0 && break + done + + return $ret +} + + +function unq() { + local l=${#1} + + while (( --l )); do + contains ${1:$l:1} ${1:0:$l} && break + done + + return $l +} + +i=$(( $winl - 1 )) + +read -n $i win + +win=${win:0:1}${win} + +while read -n 1 a; do + let i++ + add $a + unq "$win" && break +done + +echo $i diff --git a/6b.sh b/6b.sh new file mode 100755 index 0000000..ee85be5 --- /dev/null +++ b/6b.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +winl=14 +win='' + +function add() { + win="${win:1}${1}" +} + +function contains() { + # $1 what, $2 where + local l=${#2} + local ret=1 + + while (( l-- )); do + [[ "$1" = "${2:$l:1}" ]] && ret=0 && break + done + + return $ret +} + +function unq() { + local l=${#1} + + while (( --l )); do + contains ${1:$l:1} ${1:0:$l} && break + done + + return $l +} + +i=$(( $winl - 1 )) + +read -n $i win + +win=${win:0:1}${win} + +while read -n 1 a; do + let i++ + add $a + unq "$win" && break +done + +echo $i diff --git a/LICENSE b/LICENSE index c7ffc1a..a407550 100644 --- a/LICENSE +++ b/LICENSE @@ -1 +1,14 @@ -"THE BEER-WARE LICENSE" (Revision 42): wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp +/* + * ---------------------------------------------------------------------------- + * "THE BEERWARE NO-WARRANTY LICENCE v1" (based on BEERWARE rev 42) + * + * made this stuff. + * + * As long as you retain this notice you can do whatever you want with this + * stuff. Use this stuff at your own risk however - no warranty at all. + * + * If we meet some day, and you think this stuff is worth it, you can buy me + * a beer in return. + * copyright (c) 2023, jficz + * ---------------------------------------------------------------------------- + */