From d9a1e9fce3835cf3befc3e20e9701f0b931dd981 Mon Sep 17 00:00:00 2001 From: Clement Date: Wed, 11 Dec 2024 15:04:49 +0800 Subject: [PATCH] Day 11 Part 1 working --- 2024/day-11/input | 1 + 2024/day-11/solution-1.sh | 66 +++++++++++++++++++++++++++++++++++++++ 2024/day-11/solution-2.sh | 66 +++++++++++++++++++++++++++++++++++++++ 2024/day-11/test-input-1 | 1 + 2024/day-11/test-input-2 | 1 + 5 files changed, 135 insertions(+) create mode 100644 2024/day-11/input create mode 100644 2024/day-11/solution-1.sh create mode 100644 2024/day-11/solution-2.sh create mode 100644 2024/day-11/test-input-1 create mode 100644 2024/day-11/test-input-2 diff --git a/2024/day-11/input b/2024/day-11/input new file mode 100644 index 0000000..d9da788 --- /dev/null +++ b/2024/day-11/input @@ -0,0 +1 @@ +2 54 992917 5270417 2514 28561 0 990 diff --git a/2024/day-11/solution-1.sh b/2024/day-11/solution-1.sh new file mode 100644 index 0000000..1e2df16 --- /dev/null +++ b/2024/day-11/solution-1.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +read -r -a ROCK_LIST <<< "$( cat input )" +BLINK_NUM=25 + +ROCK_RETURN=() +ROCK_NUM=0 +blink_rock () { + + #printf "Blinking rock : %s\n" "$ROCK_NUM" >&2 + + # Remove leading zeroes + ROCK_NUM=$(( 10#$ROCK_NUM )) + # Rule 1 : Convert 0 to 1 + if [[ $ROCK_NUM -eq 0 ]] + then + ROCK_RETURN=( 1 ) + return + fi + + # Rule 2 : If even number of digits, split half half + DIGIT_COUNT=${#ROCK_NUM} + if [[ $(( DIGIT_COUNT % 2 )) -eq 0 ]] && [[ $DIGIT_COUNT -gt 0 ]] + then + HALF=$(( DIGIT_COUNT / 2 )) + read -r -a ROCK_RETURN <<< "$( printf "%s %s " "${ROCK_NUM:0:$HALF}" "${ROCK_NUM:$HALF}" )" + return + fi + + # Rule 3 : Multiply by 2024 + ROCK_RETURN=( $(( ROCK_NUM * 2024 )) ) + return +} + +ROCKS_TO_BLINK=() +BLINKED_ROCKS=() +# Updates the global array +blink_rocks () { + + BLINKED_ROCKS=() + for ROCK in "${ROCKS_TO_BLINK[@]}" + do + ROCK_NUM=$ROCK + blink_rock + BLINKED_ROCKS+=( "${ROCK_RETURN[@]}" ) + done + #printf "BLINKED_ROCKS: " >&2 + #printf "%s " "${BLINKED_ROCKS[@]}" >&2 + #printf "\n" >&2 +} + +FINAL_LIST=() +for ROCK_NUM in "${ROCK_LIST[@]}" +do + printf "Rock: %s\n" "$ROCK_NUM" + ROCKS_TO_BLINK=( "$ROCK_NUM" ) + for (( i=0 ; i< BLINK_NUM; i++ )) + do + blink_rocks + ROCKS_TO_BLINK=( "${BLINKED_ROCKS[@]}" ) + done + FINAL_LIST+=( "${BLINKED_ROCKS[@]}" ) +done +printf "%s " "${FINAL_LIST[@]}" +printf "\n" +printf "%s\n" "${#FINAL_LIST[@]}" diff --git a/2024/day-11/solution-2.sh b/2024/day-11/solution-2.sh new file mode 100644 index 0000000..1e2df16 --- /dev/null +++ b/2024/day-11/solution-2.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +read -r -a ROCK_LIST <<< "$( cat input )" +BLINK_NUM=25 + +ROCK_RETURN=() +ROCK_NUM=0 +blink_rock () { + + #printf "Blinking rock : %s\n" "$ROCK_NUM" >&2 + + # Remove leading zeroes + ROCK_NUM=$(( 10#$ROCK_NUM )) + # Rule 1 : Convert 0 to 1 + if [[ $ROCK_NUM -eq 0 ]] + then + ROCK_RETURN=( 1 ) + return + fi + + # Rule 2 : If even number of digits, split half half + DIGIT_COUNT=${#ROCK_NUM} + if [[ $(( DIGIT_COUNT % 2 )) -eq 0 ]] && [[ $DIGIT_COUNT -gt 0 ]] + then + HALF=$(( DIGIT_COUNT / 2 )) + read -r -a ROCK_RETURN <<< "$( printf "%s %s " "${ROCK_NUM:0:$HALF}" "${ROCK_NUM:$HALF}" )" + return + fi + + # Rule 3 : Multiply by 2024 + ROCK_RETURN=( $(( ROCK_NUM * 2024 )) ) + return +} + +ROCKS_TO_BLINK=() +BLINKED_ROCKS=() +# Updates the global array +blink_rocks () { + + BLINKED_ROCKS=() + for ROCK in "${ROCKS_TO_BLINK[@]}" + do + ROCK_NUM=$ROCK + blink_rock + BLINKED_ROCKS+=( "${ROCK_RETURN[@]}" ) + done + #printf "BLINKED_ROCKS: " >&2 + #printf "%s " "${BLINKED_ROCKS[@]}" >&2 + #printf "\n" >&2 +} + +FINAL_LIST=() +for ROCK_NUM in "${ROCK_LIST[@]}" +do + printf "Rock: %s\n" "$ROCK_NUM" + ROCKS_TO_BLINK=( "$ROCK_NUM" ) + for (( i=0 ; i< BLINK_NUM; i++ )) + do + blink_rocks + ROCKS_TO_BLINK=( "${BLINKED_ROCKS[@]}" ) + done + FINAL_LIST+=( "${BLINKED_ROCKS[@]}" ) +done +printf "%s " "${FINAL_LIST[@]}" +printf "\n" +printf "%s\n" "${#FINAL_LIST[@]}" diff --git a/2024/day-11/test-input-1 b/2024/day-11/test-input-1 new file mode 100644 index 0000000..9990375 --- /dev/null +++ b/2024/day-11/test-input-1 @@ -0,0 +1 @@ +0 1 10 99 999 diff --git a/2024/day-11/test-input-2 b/2024/day-11/test-input-2 new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/2024/day-11/test-input-2 @@ -0,0 +1 @@ +125 17