From ea4967bfcdd02721e20ad746e32781b810fab794 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 7 Dec 2022 18:45:52 +0100 Subject: [PATCH] 2022 day 3 (bash, working version before cleanup) --- 2022/day03/README.org | 11 ++------ 2022/day03/aoc.bash | 61 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/2022/day03/README.org b/2022/day03/README.org index 0b50ed0..17baec8 100644 --- a/2022/day03/README.org +++ b/2022/day03/README.org @@ -60,8 +60,6 @@ Find the item type that appears in both compartments of each rucksack. Your puzzle answer was =7878=. -The first half of this puzzle is complete! It provides one gold star: * - ** --- Part Two --- As you finish identifying the misplaced items, the Elves come to you 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 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 -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. +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/2022/day03/aoc.bash b/2022/day03/aoc.bash index 0fdf1f1..ed1a4fe 100755 --- a/2022/day03/aoc.bash +++ b/2022/day03/aoc.bash @@ -15,6 +15,7 @@ export LANG=C declare -a tot=(1 2) +declare -a input # get priority value for an item # $1: character @@ -31,39 +32,73 @@ prio() { } parse() { - local input half1 half2 result="" - local -i len half i j=1 prio part1=0 line=1 + readarray -t input +} - while read -r input; do - (( len = ${#input}, half = len/2 )) - half1="${input:0:half}" - half2="${input:half}" +part1() { + local line half1 half2 result="" + local -i len half i prio + + res=0 + for line in "${input[@]}"; do + echo "$line" + (( len = ${#line}, half = len/2 )) + half1="${line:0:half}" + half2="${line:half}" 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}]}" #prio prio "${c:0:1}" #(( part1 += prio )) - for ((i = 0; i < half; ++i)); do + 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}" - (( part1 += prio )) + (( res += prio )) #printf "%d prio(%c)=%d tot=%d\n" "$line" "$c" "$prio" "$part1" fi done - ((j++, line++)) - echo 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() { - res="${tot[$1]}" + if (($1 == 1)); then + part1 + else + part2 + fi + #res="${tot[$1]}" } main "$@"