2022 day 3 (bash, working version before cleanup)

This commit is contained in:
2022-12-07 18:45:52 +01:00
parent d1026e8f59
commit ea4967bfcd
2 changed files with 50 additions and 22 deletions

View File

@@ -60,8 +60,6 @@ Find the item type that appears in both compartments of each rucksack.
Your puzzle answer was =7878=. Your puzzle answer was =7878=.
The first half of this puzzle is complete! It provides one gold star: *
** --- Part Two --- ** --- Part Two ---
As you finish identifying the misplaced items, the Elves come to you As you finish identifying the misplaced items, the Elves come to you
with another issue. with another issue.
@@ -111,11 +109,6 @@ attachment efforts: here, they are 18 (=r=) for the first group and 52
Find the item type that corresponds to the badges of each three-Elf Find the item type that corresponds to the badges of each three-Elf
group. /What is the sum of the priorities of those item types?/ group. /What is the sum of the priorities of those item types?/
Answer: Your puzzle answer was =2760=.
Although it hasn't changed, you can still [[file:3/input][get your Both parts of this puzzle are complete! They provide two gold stars: **
puzzle input]].
You can also [Shareon
[[https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Rucksack+Reorganization%22+%2D+Day+3+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F3&related=ericwastl&hashtags=AdventOfCode][Twitter]]
[[javascript:void(0);][Mastodon]]] this puzzle.

View File

@@ -15,6 +15,7 @@
export LANG=C export LANG=C
declare -a tot=(1 2) declare -a tot=(1 2)
declare -a input
# get priority value for an item # get priority value for an item
# $1: character # $1: character
@@ -31,16 +32,22 @@ prio() {
} }
parse() { parse() {
local input half1 half2 result="" readarray -t input
local -i len half i j=1 prio part1=0 line=1 }
while read -r input; do part1() {
(( len = ${#input}, half = len/2 )) local line half1 half2 result=""
half1="${input:0:half}" local -i len half i prio
half2="${input:half}"
res=0
for line in "${input[@]}"; do
echo "$line"
(( len = ${#line}, half = len/2 ))
half1="${line:0:half}"
half2="${line:half}"
result="" result=""
#printf "[%d] l=%d h=%d [%s / %s]\n" "$line" "$len" "$half" "$half1" "$half2" printf "[%d] l=%d h=%d [%s / %s]\n" "$num" "$len" "$half" "$half1" "$half2"
#c="${half1//[^${half2}]}" #c="${half1//[^${half2}]}"
#prio prio "${c:0:1}" #prio prio "${c:0:1}"
@@ -52,18 +59,46 @@ parse() {
echo "found $c" echo "found $c"
result=$result$c result=$result$c
prio prio "${c:0:1}" prio prio "${c:0:1}"
(( part1 += prio )) (( res += prio ))
#printf "%d prio(%c)=%d tot=%d\n" "$line" "$c" "$prio" "$part1" #printf "%d prio(%c)=%d tot=%d\n" "$line" "$c" "$prio" "$part1"
fi fi
done done
((j++, line++))
echo
done done
echo "result=$part1" }
part2() {
local common one two three
local -i len half i prio
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}"
(( res += prio ))
done
} }
solve() { solve() {
res="${tot[$1]}" if (($1 == 1)); then
part1
else
part2
fi
#res="${tot[$1]}"
} }
main "$@" main "$@"