From cd41685cb52eba79f0b87c7c10c623b1a85a0c3f Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sat, 8 Oct 2022 21:29:47 +0200 Subject: [PATCH] Day 23 (bash) part 2 (~7mn run) --- 2020/RESULTS.txt | 12 +++++++ 2020/day21/OUTPUT | 8 ----- 2020/day23/README | 6 ++-- 2020/day23/{EXAMPLE.txt => TEST.txt} | 0 2020/day23/ex2.bash | 50 ++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 10 deletions(-) delete mode 100644 2020/day21/OUTPUT rename 2020/day23/{EXAMPLE.txt => TEST.txt} (100%) create mode 100755 2020/day23/ex2.bash diff --git a/2020/RESULTS.txt b/2020/RESULTS.txt index e3a0e93..2b321fb 100644 --- a/2020/RESULTS.txt +++ b/2020/RESULTS.txt @@ -485,3 +485,15 @@ ex1.bash: res=31314 ex2.bash: res=32760 time: 1:21.92 real, 81.89 user, 0.01 sys context-switch: 462+1, page-faults: 0+5135 + +========================================= +================= day23 ================= +========================================= + +ex1.bash: res=75893264 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 0+1, page-faults: 0+166 + +ex2.bash: res=38162588308 + time: 6:52.50 real, 412.30 user, 0.14 sys + context-switch: 2219+1, page-faults: 0+30233 diff --git a/2020/day21/OUTPUT b/2020/day21/OUTPUT deleted file mode 100644 index 435e18d..0000000 --- a/2020/day21/OUTPUT +++ /dev/null @@ -1,8 +0,0 @@ -ex1.bash: res=2211 - time: 0:01.72 real, 1.71 user, 0.00 sys - context-switch: 9+1, page-faults: 0+1090 - -ex2.bash: res=vv,nlxsmb,rnbhjk,bvnkk,ttxvphb,qmkz,trmzkcfg,jpvz - time: 0:01.75 real, 1.74 user, 0.00 sys - context-switch: 36+4, page-faults: 0+1388 - diff --git a/2020/day23/README b/2020/day23/README index 53a52cc..75d1580 100644 --- a/2020/day23/README +++ b/2020/day23/README @@ -75,8 +75,6 @@ After the crab is done, what order will the cups be in? Starting after the cup l Using your labeling, simulate 100 moves. What are the labels on the cups after cup 1? Your puzzle answer was 75893264. - -The first half of this puzzle is complete! It provides one gold star: * --- Part Two --- Due to what you can only assume is a mistranslation (you're not exactly fluent in Crab), you are quite surprised when the crab starts arranging many cups in a circle on your raft - one million (1000000) in total. @@ -90,3 +88,7 @@ The crab is going to hide your stars - one each - under the two cups that will e In the above example (389125467), this would be 934001 and then 159792; multiplying these together produces 149245887792. Determine which two cups will end up immediately clockwise of cup 1. What do you get if you multiply their labels together? + +Your puzzle answer was 38162588308. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/2020/day23/EXAMPLE.txt b/2020/day23/TEST.txt similarity index 100% rename from 2020/day23/EXAMPLE.txt rename to 2020/day23/TEST.txt diff --git a/2020/day23/ex2.bash b/2020/day23/ex2.bash new file mode 100755 index 0000000..5fee486 --- /dev/null +++ b/2020/day23/ex2.bash @@ -0,0 +1,50 @@ +#!/bin/bash +# +# ex2.bash: Advent2020 game, day 23/game 2. + +CMD=${0##*/} +shopt -s extglob +set -o noglob + +declare -A next # next[i] is cup right to i (ring) +declare -i end runs cup _cup + +read -r str +tmp="${str//?()/x}" +tmp=${tmp#x} +IFS=x read -ra array <<< "$tmp" + +cup=${array[0]} +end=1000000 +runs=10000000 + +_cup=$cup +# initialize the next array with input cups +for _next in "${array[@]:1}"; do + next[$_cup]=$_next + _cup=$_next +done +# initialize the next array (up to end) +for ((_next = 10; _next <= end; ++_next)); do + next[$_cup]=$_next + _cup=$_next +done +next[$_cup]=$cup # close the ring + +_cup=$cup +declare -i _1st _2nd _3rd dest +for ((i = 1; i <= runs; ++i)); do + _1st="${next[$cup]}" + _2nd="${next[$_1st]}" + _3rd="${next[$_2nd]}" + dest=$cup + while + (( --dest > 0 )) || dest=$end + (( dest == _1st || dest == _2nd || dest == _3rd )) + do :; done + (( tmp=next[$dest], next[$dest]=_1st, next[$cup]=next[$_3rd], next[$_3rd]=tmp )) + (( cup=next[$cup] )) +done + +printf "%s: res=%d\n" "$CMD" $(( next[1] * next[${next[1]}] )) +exit 0