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