Add Problem

Add getinput on aoc
This commit is contained in:
Brian Buller 2023-12-07 10:41:03 -06:00
parent 36e1cdae59
commit 0ab1fbfc55
2 changed files with 237 additions and 7 deletions

181
2023/day07/problem Normal file
View File

@ -0,0 +1,181 @@
[1]Advent of Code
br0xen [7](AoC++) 14*
--- Day 7: Camel Cards ---
Your all-expenses-paid trip turns out to be a one-way, five-minute ride in
an [16]airship. (At least it's a cool airship!) It drops you off at the
edge of a vast desert and descends back to Island Island.
"Did you bring the parts?"
You turn around to see an Elf completely covered in white clothing,
wearing goggles, and riding a large [17]camel.
"Did you bring the parts?" she asks again, louder this time. You aren't
sure what parts she's looking for; you're here to figure out why the sand
stopped.
"The parts! For the sand, yes! Come with me; I will show you." She beckons
you onto the camel.
After riding a bit across the sands of Desert Island, you can see what
look like very large rocks covering half of the horizon. The Elf explains
that the rocks are all along the part of Desert Island that is directly
above Island Island, making it hard to even get there. Normally, they use
big machines to move the rocks and filter the sand, but the machines have
broken down because Desert Island recently stopped receiving the parts
they need to fix the machines.
You've already assumed it'll be your job to figure out why the parts
stopped when she asks if you can help. You agree automatically.
Because the journey will take a few days, she offers to teach you the game
of Camel Cards. Camel Cards is sort of similar to [18]poker except it's
designed to be easier to play while riding a camel.
In Camel Cards, you get a list of hands, and your goal is to order them
based on the strength of each hand. A hand consists of five cards labeled
one of A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, or 2. The relative strength of
each card follows this order, where A is the highest and 2 is the lowest.
Every hand is exactly one type. From strongest to weakest, they are:
 Five of a kind, where all five cards have the same label: AAAAA
 Four of a kind, where four cards have the same label and one card has
a different label: AA8AA
 Full house, where three cards have the same label, and the remaining
two cards share a different label: 23332
 Three of a kind, where three cards have the same label, and the
remaining two cards are each different from any other card in the
hand: TTT98
 Two pair, where two cards share one label, two other cards share a
second label, and the remaining card has a third label: 23432
 One pair, where two cards share one label, and the other three cards
have a different label from the pair and each other: A23A4
 High card, where all cards' labels are distinct: 23456
Hands are primarily ordered based on type; for example, every full house
is stronger than any three of a kind.
If two hands have the same type, a second ordering rule takes effect.
Start by comparing the first card in each hand. If these cards are
different, the hand with the stronger first card is considered stronger.
If the first card in each hand have the same label, however, then move on
to considering the second card in each hand. If they differ, the hand with
the higher second card wins; otherwise, continue with the third card in
each hand, then the fourth, then the fifth.
So, 33332 and 2AAAA are both four of a kind hands, but 33332 is stronger
because its first card is stronger. Similarly, 77888 and 77788 are both a
full house, but 77888 is stronger because its third card is stronger (and
both hands have the same first and second card).
To play Camel Cards, you are given a list of hands and their corresponding
bid (your puzzle input). For example:
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
This example shows five hands; each hand is followed by its bid amount.
Each hand wins an amount equal to its bid multiplied by its rank, where
the weakest hand gets rank 1, the second-weakest hand gets rank 2, and so
on up to the strongest hand. Because there are five hands in this example,
the strongest hand will have rank 5 and its bid will be multiplied by 5.
So, the first step is to put the hands in order of strength:
 32T3K is the only one pair and the other hands are all a stronger
type, so it gets rank 1.
 KK677 and KTJJT are both two pair. Their first cards both have the
same label, but the second card of KK677 is stronger (K vs T), so
KTJJT gets rank 2 and KK677 gets rank 3.
 T55J5 and QQQJA are both three of a kind. QQQJA has a stronger first
card, so it gets rank 5 and T55J5 gets rank 4.
Now, you can determine the total winnings of this set of hands by adding
up the result of multiplying each hand's bid with its rank (765 * 1 + 220
* 2 + 28 * 3 + 684 * 4 + 483 * 5). So the total winnings in this example
are 6440.
Find the rank of every hand in your set. What are the total winnings?
Your puzzle answer was 251287184.
--- Part Two ---
To make things a little more interesting, the Elf introduces one
additional rule. Now, J cards are [19]jokers - wildcards that can act like
whatever card would make the hand the strongest type possible.
To balance this, J cards are now the weakest individual cards, weaker even
than 2. The other cards stay in the same order: A, K, Q, T, 9, 8, 7, 6, 5,
4, 3, 2, J.
J cards can pretend to be whatever card is best for the purpose of
determining hand type; for example, QJJQ2 is now considered four of a
kind. However, for the purpose of breaking ties between two hands of the
same type, J is always treated as J, not the card it's pretending to be:
JKKK2 is weaker than QQQQ2 because J is weaker than Q.
Now, the above example goes very differently:
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
 32T3K is still the only one pair; it doesn't contain any jokers, so
its strength doesn't increase.
 KK677 is now the only two pair, making it the second-weakest hand.
 T55J5, KTJJT, and QQQJA are now all four of a kind! T55J5 gets rank 3,
QQQJA gets rank 4, and KTJJT gets rank 5.
With the new joker rule, the total winnings in this example are 5905.
Using the new joker rule, find the rank of every hand in your set. What
are the new total winnings?
Your puzzle answer was 250757288.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should [20]return to your Advent calendar and try
another puzzle.
If you still want to see it, you can [21]get your puzzle input.
You can also [Shareon [22]Twitter [23]Mastodon] this puzzle.
References
Visible links
1. https://adventofcode.com/
2. https://adventofcode.com/2023/about
3. https://adventofcode.com/2023/events
4. https://teespring.com/stores/advent-of-code
5. https://adventofcode.com/2023/settings
6. https://adventofcode.com/2023/auth/logout
7. Advent of Code Supporter
https://adventofcode.com/2023/support
8. https://adventofcode.com/2023
9. https://adventofcode.com/2023
10. https://adventofcode.com/2023/support
11. https://adventofcode.com/2023/sponsors
12. https://adventofcode.com/2023/leaderboard
13. https://adventofcode.com/2023/stats
14. https://adventofcode.com/2023/sponsors
15. https://shopify.engineering/
16. https://en.wikipedia.org/wiki/Airship
17. https://en.wikipedia.org/wiki/Dromedary
18. https://en.wikipedia.org/wiki/List_of_poker_hands
19. https://en.wikipedia.org/wiki/Joker_(playing_card)
20. https://adventofcode.com/2023
21. https://adventofcode.com/2023/day/7/input
22. https://twitter.com/intent/tweet?text=I%27ve+completed+%22Camel+Cards%22+%2D+Day+7+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F7&related=ericwastl&hashtags=AdventOfCode
23. javascript:void(0);

View File

@ -1,14 +1,11 @@
#!/bin/bash #!/bin/bash
today() { today() {
echo "Going to today"
TODAYDIR="$AOCROOT/$CURRYEAR/day$CURRDAY" TODAYDIR="$AOCROOT/$CURRYEAR/day$CURRDAY"
if [ ! -d $TODAYDIR ]; then if [ ! -d $TODAYDIR ]; then
echo "> Doesn't exist, making"
mkdir $TODAYDIR mkdir $TODAYDIR
fi fi
echo "> cd $TODAYDIR" echo $TODAYDIR
cd $TODAYDIR
} }
getsharelink() { getsharelink() {
@ -29,6 +26,7 @@ getsharelink() {
getproblem() { getproblem() {
YR=$CURRYEAR YR=$CURRYEAR
DY=$CURRDAY DY=$CURRDAY
DIRDY=$CURRDAY
PWD=$(pwd) PWD=$(pwd)
PWD=${PWD#$AOCROOT/} PWD=${PWD#$AOCROOT/}
@ -38,7 +36,10 @@ getproblem() {
DY=${DY/day0/} DY=${DY/day0/}
DY=${DY/day/} DY=${DY/day/}
fi fi
cd "$AOCROOT/$YR/day$DY" if [[ ! -d "$AOCROOT/$YR/day$DIRDY" ]]; then
mkdir "$AOCROOT/$YR/day$DIRDY"
fi
cd "$AOCROOT/$YR/day$DIRDY"
if [[ -f "problem" ]]; then if [[ -f "problem" ]]; then
echo "Found problem file." echo "Found problem file."
if [[ $1 != "-f" ]]; then if [[ $1 != "-f" ]]; then
@ -51,10 +52,46 @@ getproblem() {
# Remove any zero padding from day # Remove any zero padding from day
DY=$(echo "$CURRDAY"|awk '$0*=1') DY=$(echo "$CURRDAY"|awk '$0*=1')
echo "Getting problem at $CURRYEAR/day/$DY" echo "Getting problem at $CURRYEAR/day/$DY"
# elinks -dump https://adventofcode.com/$CURRYEAR/day/$DY > problem elinks -dump https://adventofcode.com/$CURRYEAR/day/$DY > problem
vim problem vim problem
} }
getinput() {
YR=$CURRYEAR
DY=$CURRDAY
DIRDY=$CURRDAY
PWD=$(pwd)
PWD=${PWD#$AOCROOT/}
if [[ "$PWD" =~ ^[1-9][0-9]{3}/day[0-2][0-9]$ ]]; then
YR=${PWD:0:4}
DY=${PWD:5:5}
DY=${DY/day0/}
DY=${DY/day/}
fi
if [[ ! -d "$AOCROOT/$YR/day$DIRDY" ]]; then
mkdir "$AOCROOT/$YR/day$DIRDY"
fi
cd "$AOCROOT/$YR/day$DIRDY"
if [[ -f "input" ]]; then
echo "Found input file."
if [[ $1 != "-f" ]]; then
# Check if the file already exists
echo "Not forcing overwrite."
exit 1
fi
fi
# Remove any zero padding from day
DY=$(echo "$CURRDAY"|awk '$0*=1')
echo "Getting input for $CURRYEAR/day/$DY"
echo "https://adventofcode.com/$CURRYEAR/day/$DY/input"
echo "session=$(getsessiontoken)"
curl --cookie "session=$(getsessiontoken)" https://adventofcode.com/$CURRYEAR/day/$DY/input > input
# elinks -dump https://adventofcode.com/$CURRYEAR/day/$DY/input > input
# elinks -dump https://adventofcode.com/$CURRYEAR/day/$DY/input
}
getsessiontoken() { getsessiontoken() {
FULLCOOKIE=$(grep "adventofcode" $HOME/.elinks/cookies) FULLCOOKIE=$(grep "adventofcode" $HOME/.elinks/cookies)
if [[ ! -n "$FULLCOOKIE" ]]; then if [[ ! -n "$FULLCOOKIE" ]]; then
@ -68,10 +105,16 @@ help() {
echo "Usage: aoc [option]" echo "Usage: aoc [option]"
echo "Available Options:" echo "Available Options:"
echo "" echo ""
echo " today - Echo the current day's directory, creating it if it doesn't exist"
echo ""
echo " getproblem - Retrieve the problem for the day associated with the current directory" echo " getproblem - Retrieve the problem for the day associated with the current directory"
echo " If the current directory isn't a specific problem (\d\d\d\d/day\d\d/)" echo " If the current directory isn't a specific problem (\d\d\d\d/day\d\d/)"
echo " then it changes to today's directory, making it if needed" echo " then it changes to today's directory, making it if needed"
echo "" echo ""
echo " getinput - Retrieve the input for the day associated with the current directory"
echo " If the current directory isn't a specific problem (\d\d\d\d/day\d\d/)"
echo " then it changes to today's directory, making it if needed"
echo ""
echo " getsessiontoken - Echo the session token from the elinks cookie file" echo " getsessiontoken - Echo the session token from the elinks cookie file"
echo "" echo ""
echo " getsharelink - Get the repo remote path for the main.go file in the current directory" echo " getsharelink - Get the repo remote path for the main.go file in the current directory"
@ -83,8 +126,14 @@ help() {
} }
case $1 in case $1 in
"today")
today
;;
"getproblem") "getproblem")
getproblem $2 getproblem
;;
"getinput")
getinput
;; ;;
"getsessiontoken") "getsessiontoken")
getsessiontoken getsessiontoken