2020 day 23 part 1 (bash). Ooops. This won't do part 2 :-(
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,7 +11,6 @@ lib/
|
||||
GPATH
|
||||
GRTAGS
|
||||
GTAGS
|
||||
2020/day23/
|
||||
FOO*
|
||||
BAR*
|
||||
QUAX*
|
||||
|
1
2020/day23/EXAMPLE.txt
Normal file
1
2020/day23/EXAMPLE.txt
Normal file
@@ -0,0 +1 @@
|
||||
389125467
|
1
2020/day23/INPUT.txt
Normal file
1
2020/day23/INPUT.txt
Normal file
@@ -0,0 +1 @@
|
||||
974618352
|
@@ -74,7 +74,19 @@ 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 input is 974618352.
|
||||
Your puzzle answer was 75893264.
|
||||
|
||||
Answer:
|
||||
You can also [Share] this puzzle.
|
||||
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.
|
||||
|
||||
Your labeling is still correct for the first few cups; after that, the remaining cups are just numbered in an increasing fashion starting from the number after the highest number in your list and proceeding one by one until one million is reached. (For example, if your labeling were 54321, the cups would be numbered 5, 4, 3, 2, 1, and then start counting up from 6 until one million is reached.) In this way, every number from one through one million is used exactly once.
|
||||
|
||||
After discovering where you made the mistake in translating Crab Numbers, you realize the small crab isn't going to do merely 100 moves; the crab is going to do ten million (10000000) moves!
|
||||
|
||||
The crab is going to hide your stars - one each - under the two cups that will end up immediately clockwise of cup 1. You can have them if you predict what the labels on those cups will be when the crab is finished.
|
||||
|
||||
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?
|
||||
|
176
2020/day23/ex1.bash
Executable file
176
2020/day23/ex1.bash
Executable file
@@ -0,0 +1,176 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# ex1.bash: Advent2020 game, day 23/game 1.
|
||||
|
||||
CMD=${0##*/}
|
||||
shopt -s extglob
|
||||
set -o noglob
|
||||
|
||||
#declare -a INPUT
|
||||
declare rev="98765432198765"
|
||||
runs=${1:-10}
|
||||
read -r str
|
||||
#[[ "$str" =~ ${str//?/(.)} ]]
|
||||
#INPUT=("${BASH_REMATCH[@]:1}")
|
||||
#printf "INPUT=%s \n" "${INPUT[*]}"
|
||||
|
||||
pos() {
|
||||
local -n _pos=$1
|
||||
local c=$2 s=$3
|
||||
local -i i
|
||||
for ((i=0; i<9; ++i)); do
|
||||
[[ ${s:i:1} = "$c" ]] && break
|
||||
done
|
||||
printf "pos(%s, %s)=%d\n" "$s" "$c" "$i"
|
||||
_pos=$((i+1))
|
||||
}
|
||||
|
||||
# charpos - get cup position in a string
|
||||
# $1 reference of variable to store the position
|
||||
# $2: The string
|
||||
# $3: The cup to find
|
||||
#
|
||||
# @Return: cup position
|
||||
# example: charpos "987654321" "9" will return 0
|
||||
charpos() {
|
||||
local -n _res="$1"
|
||||
local left="${2%%"$3"*}"
|
||||
# printf " charpos(%s,%s)=%d " "$2" "$3" "${#left}"
|
||||
[[ "$left" = "$2" ]] && _res=-1 || _res=${#left}
|
||||
}
|
||||
|
||||
# destcup - get insertion position
|
||||
# $1: reference of variable to store the cup position in string
|
||||
# $2: current cup
|
||||
# $3: The string
|
||||
#
|
||||
# @Return: the next cup
|
||||
# example: nextchar "987654321" "9" will return 8
|
||||
destcup() {
|
||||
local -n _pos="$1"
|
||||
local _cup="$2" _str="$3"
|
||||
#(( _cup-- ))
|
||||
printf "destcup str=%s _pos=%d cup=%d\n" "$_str" "$_pos" "$_cup"
|
||||
while (( _cup = (_cup + 8) % 9))
|
||||
(( _cup == 0 )) && (( _cup = 9 ))
|
||||
printf "trying %d in %s ..." "$_cup" "$_str"
|
||||
charpos _pos "$_str" "$_cup"
|
||||
printf " pos=%d " "$_pos"
|
||||
(( _pos == -1 )); do
|
||||
echo fail
|
||||
done
|
||||
echo ok
|
||||
}
|
||||
|
||||
# nextcup - get next cup in string
|
||||
# $1: reference of current cup
|
||||
# $2: The string
|
||||
#
|
||||
# @Return: the next cup
|
||||
nextcup() {
|
||||
local -n _cup="$1"
|
||||
local _str="$2$2"
|
||||
_str=${_str#*"$_cup"}
|
||||
# printf "str=%s\n" "$_str"
|
||||
_cup=${_str:0:1}
|
||||
}
|
||||
|
||||
# pickup - get the pickup string after current one
|
||||
# $1 reference of variable to store the pickup string
|
||||
# $2: reference of string string
|
||||
# $3: The current cup
|
||||
#
|
||||
# @Return: the pickup string
|
||||
# example: charpos "987654321" "2" will return "198"
|
||||
pickup() {
|
||||
local -n _pick="$1" _str="$2"
|
||||
local -i _pos
|
||||
local tmp="$_str$_str"
|
||||
#printf "STR2= %s %s\n" "$2" "$_str"
|
||||
charpos _pos "$_str" "$3"
|
||||
printf " str=%s _pos(%s)=%d->" "$_str" "$3" "$_pos"
|
||||
(( _pos = (_pos + 1) % 9 ))
|
||||
_pick="${tmp:_pos:3}"
|
||||
tmp=${tmp//[$_pick]}
|
||||
_str=${tmp:0:6}
|
||||
printf "%d = pick=%s str=%s\n" "$_pos" "$_pick" "$_str"
|
||||
}
|
||||
|
||||
declare -i res pos=0 cup=${str:0:1}
|
||||
declare pick
|
||||
#printf "BEFORE str=%s cup=%s pos=%d\n" "$str" "$cup" "$pos"
|
||||
#nextcup cup pos "$str"
|
||||
#printf "AFTER str=%s cup=%s pos=%d\n" "$str" "$cup" "$pos"
|
||||
|
||||
for ((i = 1; i <= 100; ++i)); do
|
||||
printf "\nloop=%d str=%s cup=%s pos=%d\n" "$i" "$str" "$cup" "$pos"
|
||||
pickup pick str "$cup"
|
||||
printf "AFTER1 str=%s pick=%s cup=%s pos=%d\n" "$str" "$pick" "$cup" "$pos"
|
||||
destcup pos "$cup" "$str"
|
||||
printf "AFTER2 str=%s pick=%s cup=%s pos=%d\n" "$str" "$pick" "$cup" "$pos"
|
||||
#insert str "$pick" "$pos"
|
||||
str="${str:0:pos+1}$pick${str:pos+1}"
|
||||
printf "newstr=%s pick=%s\n" "$str" "$pick"
|
||||
nextcup cup "$str"
|
||||
printf "next cup=%s\n" "$cup"
|
||||
#charpos res "$str" "$i"
|
||||
#nextchar next "$str" "$i"
|
||||
#printf "a(%s, %d)=%d next=%s\n" "$str" "$i" "$res" "$next"
|
||||
|
||||
done
|
||||
res="${str#*1}${str%1*}"
|
||||
printf "%s: res=%d\n" "$CMD" "$res"
|
||||
exit 0
|
||||
|
||||
# move - rune a move
|
||||
# $1: The input string (by address)
|
||||
# $2: The current cup
|
||||
#
|
||||
# @Return: The next cup
|
||||
move() {
|
||||
|
||||
:
|
||||
}
|
||||
|
||||
run() {
|
||||
local -i l="$1" p="$2" pos
|
||||
local str="$3"
|
||||
local str2 o c
|
||||
|
||||
while ((--l)); do
|
||||
printf "%s move %d --\n" "--" $(($1 - l))
|
||||
printf "cups=%s\n" "$str"
|
||||
printf "pos=%s\n" "$p"
|
||||
#
|
||||
#printf "cups:"
|
||||
#for ((j=0; j<9; ++j)); do
|
||||
|
||||
#printf "str2=%s\n" "$str2"
|
||||
|
||||
o=${str2:p+1:3}
|
||||
c=${str2:p:1}
|
||||
# remove the 3 cups
|
||||
str2=${str2//$o/}
|
||||
rev1=${rev#*$c}
|
||||
printf "c=%s\npick up=%s\nstr2=%s\n" "$c" "$o" "$str2"
|
||||
printf "rev1=%s\n" "$rev1"
|
||||
|
||||
# find destination cup
|
||||
for ((i=0; i<4; ++i)); do
|
||||
c2=${rev1:i:1}
|
||||
printf "testing %c in %s\n" "$c2" "$o"
|
||||
[[ $o =~ .*$c2.* ]] || break
|
||||
done
|
||||
printf "destination=%s\n" "$c2"
|
||||
pos pos "$c2" "$str2"
|
||||
str2=${str2:0:$pos}$o${str2:$pos}
|
||||
#str2=${str2/$c2/$c2$o}
|
||||
#str2=${str2/$c2/$c2$o}
|
||||
|
||||
printf "str2=%s\n" "$str2"
|
||||
str=${str2:0:9}
|
||||
|
||||
((++p==9)) && p=0
|
||||
printf "new str=%s\n\n" "$str"
|
||||
done
|
||||
}
|
Reference in New Issue
Block a user