bash: 2022 day 3 (final 2 parts)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# aoc.bash: Advent of Code 2022, day 2
|
||||
# aoc.bash: Advent of Code 2022, day 3
|
||||
#
|
||||
# Copyright (C) 2022 Bruno Raoult ("br")
|
||||
# Licensed under the GNU General Public License v3.0 or later.
|
||||
@@ -14,14 +14,14 @@
|
||||
. common.bash
|
||||
|
||||
export LANG=C
|
||||
declare -a tot=(1 2)
|
||||
declare -a input
|
||||
|
||||
# get priority value for an item
|
||||
# $1: character
|
||||
# prio - get priority value for an item
|
||||
# $1: reference of variable for result
|
||||
# $2: string
|
||||
prio() {
|
||||
local -n _val="$1"
|
||||
local _char="$2"
|
||||
local _char="${2:0:1}"
|
||||
|
||||
printf -v _val "%d" "'$_char"
|
||||
if [[ $_char > "Z" ]]; then # a-z
|
||||
@@ -31,63 +31,46 @@ prio() {
|
||||
fi
|
||||
}
|
||||
|
||||
# common=chars - get common characters in two strings
|
||||
# $1: reference of variable for result
|
||||
# $2, $3: The two strings
|
||||
common_chars() {
|
||||
local -n _res="$1"
|
||||
|
||||
_res="${2//[^$3]}"
|
||||
}
|
||||
|
||||
parse() {
|
||||
readarray -t input
|
||||
}
|
||||
|
||||
part1() {
|
||||
local line half1 half2 result=""
|
||||
local -i len half i prio
|
||||
local line half1 half2 common
|
||||
local -i half i prio
|
||||
declare -ig res=0
|
||||
|
||||
res=0
|
||||
for line in "${input[@]}"; do
|
||||
echo "$line"
|
||||
(( len = ${#line}, half = len/2 ))
|
||||
(( half = ${#line} / 2 ))
|
||||
half1="${line:0:half}"
|
||||
half2="${line:half}"
|
||||
result=""
|
||||
|
||||
printf "[%d] l=%d h=%d [%s / %s]\n" "$num" "$len" "$half" "$half1" "$half2"
|
||||
|
||||
#c="${half1//[^${half2}]}"
|
||||
#prio prio "${c:0:1}"
|
||||
#(( part1 += prio ))
|
||||
|
||||
for (( i = 0; i < half; ++i )); do
|
||||
c=${half1:$i:1}
|
||||
if [[ $result != *$c* && $half2 == *$c* ]]; then
|
||||
echo "found $c"
|
||||
result=$result$c
|
||||
prio prio "${c:0:1}"
|
||||
(( res += prio ))
|
||||
#printf "%d prio(%c)=%d tot=%d\n" "$line" "$c" "$prio" "$part1"
|
||||
fi
|
||||
done
|
||||
common="${half1//[^${half2}]}"
|
||||
prio prio "${common}"
|
||||
(( res += prio ))
|
||||
done
|
||||
}
|
||||
|
||||
part2() {
|
||||
local common one two three
|
||||
local -i len half i prio
|
||||
local one two three common
|
||||
local -i i prio
|
||||
declare -ig res=0
|
||||
|
||||
res=0
|
||||
for (( i = ${#input[@]} - 1; i > 1; i -= 3)); do
|
||||
three=${input[i]}
|
||||
two=${input[i - 1]}
|
||||
one=${input[i - 2]}
|
||||
common=${three//[^${two}]}
|
||||
common=${common//[^${one}]}
|
||||
#common=${}
|
||||
#echo "$line"
|
||||
#(( len = ${#line}, half = len/2 ))
|
||||
#half1="${line:0:half}"
|
||||
#half2="${line:half}"
|
||||
#result=""
|
||||
|
||||
printf "[%d] 1=%s 2=%s 3=%s c=%s\n" "$i" "$one" "$two" "$three" "$common"
|
||||
|
||||
#c="${half1//[^${half2}]}"
|
||||
prio prio "${common:0:1}"
|
||||
common_chars common "$three" "$two"
|
||||
common_chars common "$common" "$one"
|
||||
prio prio "${common}"
|
||||
(( res += prio ))
|
||||
done
|
||||
}
|
||||
@@ -98,7 +81,6 @@ solve() {
|
||||
else
|
||||
part2
|
||||
fi
|
||||
#res="${tot[$1]}"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
Reference in New Issue
Block a user