From 13728a2b4712c95279e5b80d03e1882b511f23ce Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sat, 24 Jul 2021 19:09:22 +0200 Subject: [PATCH] Day 20 part 1, 1st bash version (with tons of debug output) --- day20/README | 105 ++++++++++++++++++++++++++++++++++++++++++++++++- day20/ex1.bash | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+), 1 deletion(-) create mode 100755 day20/ex1.bash diff --git a/day20/README b/day20/README index 455717f..d650241 100644 --- a/day20/README +++ b/day20/README @@ -165,7 +165,110 @@ To check that you've assembled the image correctly, multiply the IDs of the four Assemble the tiles into an image. What do you get if you multiply together the IDs of the four corner tiles? -To begin, get your puzzle input. +Your puzzle answer was 5966506063747. + +The first half of this puzzle is complete! It provides one gold star: * +--- Part Two --- + +Now, you're ready to check the image for sea monsters. + +The borders of each tile are not part of the actual image; start by removing them. + +In the example above, the tiles become: + +.#.#..#. ##...#.# #..##### +###....# .#....#. .#...... +##.##.## #.#.#..# #####... +###.#### #...#.## ###.#..# +##.#.... #.##.### #...#.## +...##### ###.#... .#####.# +....#..# ...##..# .#.###.. +.####... #..#.... .#...... + +#..#.##. .#..###. #.##.... +#.####.. #.####.# .#.###.. +###.#.#. ..#.#### ##.#..## +#.####.. ..##..## ######.# +##..##.# ...#...# .#.#.#.. +...#..#. .#.#.##. .###.### +.#.#.... #.##.#.. .###.##. +###.#... #..#.##. ######.. + +.#.#.### .##.##.# ..#.##.. +.####.## #.#...## #.#..#.# +..#.#..# ..#.#.#. ####.### +#..####. ..#.#.#. ###.###. +#####..# ####...# ##....## +#.##..#. .#...#.. ####...# +.#.###.. ##..##.. ####.##. +...###.. .##...#. ..#..### + +Remove the gaps to form the actual image: + +.#.#..#.##...#.##..##### +###....#.#....#..#...... +##.##.###.#.#..######... +###.#####...#.#####.#..# +##.#....#.##.####...#.## +...########.#....#####.# +....#..#...##..#.#.###.. +.####...#..#.....#...... +#..#.##..#..###.#.##.... +#.####..#.####.#.#.###.. +###.#.#...#.######.#..## +#.####....##..########.# +##..##.#...#...#.#.#.#.. +...#..#..#.#.##..###.### +.#.#....#.##.#...###.##. +###.#...#..#.##.######.. +.#.#.###.##.##.#..#.##.. +.####.###.#...###.#..#.# +..#.#..#..#.#.#.####.### +#..####...#.#.#.###.###. +#####..#####...###....## +#.##..#..#...#..####...# +.#.###..##..##..####.##. +...###...##...#...#..### + +Now, you're ready to search for sea monsters! Because your image is monochrome, a sea monster will look like this: + + # +# ## ## ### + # # # # # # + +When looking for this pattern in the image, the spaces can be anything; only the # need to match. Also, you might need to rotate or flip your image before it's oriented correctly to find sea monsters. In the above image, after flipping and rotating it to the appropriate orientation, there are two sea monsters (marked with O): + +.####...#####..#...###.. +#####..#..#.#.####..#.#. +.#.#...#.###...#.##.O#.. +#.O.##.OO#.#.OO.##.OOO## +..#O.#O#.O##O..O.#O##.## +...#.#..##.##...#..#..## +#.##.#..#.#..#..##.#.#.. +.###.##.....#...###.#... +#.####.#.#....##.#..#.#. +##...#..#....#..#...#### +..#.##...###..#.#####..# +....#.##.#.#####....#... +..##.##.###.....#.##..#. +#...#...###..####....##. +.#.##...#.##.#.#.###...# +#.###.#..####...##..#... +#.###...#.##...#.##O###. +.O##.#OO.###OO##..OOO##. +..O#.O..O..O.#O##O##.### +#.#..##.########..#..##. +#.#####..#.#...##..#.... +#....##..#.#########..## +#...#.....#..##...###.## +#..###....##.#...##.##.# + +Determine how rough the waters are in the sea monsters' habitat by counting the number of # that are not part of a sea monster. In the above example, the habitat's water roughness is 273. + +How many # are not part of a sea monster? Answer: + +Although it hasn't changed, you can still get your puzzle input. + You can also [Share] this puzzle. diff --git a/day20/ex1.bash b/day20/ex1.bash new file mode 100755 index 0000000..342c1f2 --- /dev/null +++ b/day20/ex1.bash @@ -0,0 +1,105 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 20/game 1. + +CMD=${0##*/} +set -o noglob +shopt -s extglob + +declare -A strings +declare -a T R B L RT RR RB RL nums + +declare -i count=-1 +while read -r line; do + case ${line:0:1} in + T*) + ((count++)) + num=${line##* } + num=${num%%:} + nums[$count]="$num" + printf "%s\n" "$num" + printf "size nums=%s\n" "${#nums[@]}" + ;; + \#|.) + strings[$count]="${strings[$count]} $line" + ;; + esac +done +((count--)) +#printf "string 2311=[%s]\n" "${strings[2311]}" + +for key in "${!nums[@]}"; do + # shellcheck disable=SC2206 + str=(${strings[$key]}) + printf "str=%d\n" "${#str[@]}" + top="${str[0]}" + bottom="${str[9]}" + T+=("$top") + B+=("$bottom") + # find out right and bottom + unset left right + for ((i=0; i<10; ++i)); do + left+=${str[$i]:0:1} + right+=${str[$i]: -1:1} + done + R+=("$right") + L+=("$left") + unset fliptop flipbottom flipleft flipright + for ((i=9; i>=0; --i)); do + fliptop+=${top:$i:1} + flipbottom+=${bottom:$i:1} + flipleft+=${left:$i:1} + flipright+=${right:$i:1} + done + RT+=("$fliptop") + RR+=("$flipright") + RB+=("$flipbottom") + RL+=("$flipleft") + + printf "%d: top=%s right=%s bottom=%s left=%s\n" "$key" "$top" "$right" "$bottom" "$left" + printf "%d: fliptop=%s flipright=%s flipbottom=%s flipleft=%s\n" "$key" "$fliptop" "$flipright" "$flipbottom" "$flipleft" + +done + +ALL=("${T[@]}" "${R[@]}" "${B[@]}" "${L[@]}" "${RT[@]}" "${RR[@]}" "${RB[@]}" "${RL[@]}") +#ALL=("${T[@]}" "${R[@]}" "${B[@]}" "${L[@]}" "${RT[@]}" "${RL[@]}") +printf "%d %s\n" "${#ALL[@]}" "${ALL[0]}" +declare -i res=1 +for ((i=0; i<${#nums[@]}; ++i)); do + count=0 + #printf "%d: " "$i" + #printf "%s " ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]} ${RT[$i]} ${RR[$i]} ${RB[$i]} ${RL[$i]} + #echo + + for ((j=0; j<${#nums[@]}; ++j)); do + if ((j != i)); then + #printf "i=%d j=%d\n" "$i" "$j" + ALL=("${T[$j]}" "${R[$j]}" "${B[$j]}" "${L[$j]}" "${RT[$j]}" \ + "${RR[$j]}" "${RB[$j]}" "${RL[$j]}") + for s in "${ALL[@]}"; do + #[[ $a == "$s" ]] && ((count++)) + for t in ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]} #\ + #${RT[$i]} ${RR[$i]} ${RB[$i]} ${RL[$i]}; do + do printf "match %d %s with %d %s: " "$i" "$s" "$j" "$t" + if [[ $t == "$s" ]]; then + printf "yes\n" + ((count++)) + else + : + printf "no\n" + fi + done + done + + fi + done + printf "COUNT(%d)=%d key=%s\n" "$i" "$count" "${nums[$i]}" + if ((count == 2)); then + printf " --> %s\n" "${nums[$i]}" + ((res*=${nums[$i]})) + fi +done + +printf "%s : res=%d\n" "$CMD" "$res" + +exit 0