Advent of Code 2020 - Day 22

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right welcome everyone to day 22 of infinite code the final few days remain we are live currently doing this on twitch over twitch.tv turkey dev so feel free to check me out there i've been doing all of the ones that coincide with my streams on stream as they come out so we got 30 oh just over 30 seconds to go fingers crossed again that say goes like yesterday i can actually solve it i i would be very very happy if i get at least one star in every day um i don't know if i'll go back to day 19 or 20. um 28 i don't know maybe it could do 1920 i don't know if i want to spend more time doing those but i don't know at any rates code is all set up here we got our base code going day 22 the text input here and then our base code just waiting for the question to drop here in three two one let's do it it only takes a few hours of sailing on the ocean raft for boredom to sink in unfortunately you brought a small deck of space cards you'd like to play a game of combat and there is even an opponent available a small crab that climbed aboard your raft before you left fortunately it doesn't take long to teach the crowd the rules before the game starts split the cards so that each player has their own deck your puzzle inputs then the game consists of a series of rounds both players draw the top card and the player with a higher value card wins the round the winner keeps both cards place them at the bottom of their own deck so the winners cards is it the winners card is above the other card um this is this this is war uh if this causes a player to have all the cards they win the game ends okay so this is just more this arrangement means that the players one's deck contains five cards with the nine on top all right players one deck okay it's not on top and one at the bottom place two deck also contains five cards with five on the top ten the bottom okay um okay um once the game ends you can calculate the winning player's score the bottom card in their deck is worth the value of the card multiplied by one the second card from bomb is worth oh three times ten plus two times nine plus okay so once game ends the winning card is um play the small crabbing game combating to x um just dealt what is the winning players score okay so it's legit this is war this is war okay so let's start this up so let's do for let's load this up by just streaming by line by line on input strings okay and what we need to do is we need to know we need a list of uh integers this is going to be the players deck players deck a new arraylist here and then we're gonna have another list here this is going to be the crabs deck okay and then we need to do a boolean load player cards equals true so we just need to do a check here if um this is not really needed but if s can starts with this we just set load players cards equal true um otherwise it starts with player two we set this to false and then otherwise we do a check here uh we do an integer parseint on s and we get and card and then we just do a check here if we are loading the players cards in we do players deck add card card otherwise we do the krabs deck ad card okay so now we've loaded all the cards in we have the two decks so now we just need to do a while loop while um players deck dot size is greater than zero um and krabs deck dot size is great they're working through the brute force method i don't there might be a special math way to do this quicker whatever okay so we're gonna do end car our end player card is gonna equal um players deck remove zero so we're gonna take the top cards off the deck grabs card crabs deck so we're taking copies of top cards off the deck and we're going to do if players card is greater than the crabs card do one thing otherwise the other so if the player's card is greater we put we did undo the player's deck add the player's card back and then players cart players deck and the crabs card back otherwise we do it the opposite we do the crab deck crab deck but we do it flipped because we always add the higher card back first this will go until one of the decks is empty and then we'll do here as well if we'll do if the player's deck dot size is greater than zero um we just need a list of integer this is me listens to me cards two counts a new arraylist i supposed to do cards to counts add all of the players deck else i don't know i copied that else we do cards to counts at all of the crabs deck and now we need to do is we need what the question is asking which is this logic which is you can calculate the winning score the bottom card is worth one okay all right so we just need to do four and i equals zero all i is greater or why i is less than cards to count dot size i plus plus and so we just need to have a long total zero we're gonna do total plus equals cursed accounts get i times cars accounts size um times our are minus i is it plus one one two three four five six seven eight nine ten no minus i there we go and then we can just do lap lap total and that should be part one right please be right build uh oh else if s is empty um if it is empty actually we just do else if it is not empty do this logic run again we get this is our answer be right yes i'm happy with that that was really quick cool all right so there's part one part two now you also see the small crab forge of the crabs aren't very good at recursion to defend your honor as ref you challenge a small crab to a game of recursive combat rehearsal combat still starts by splitting the cards of two decks um then the game says uh yeah after the same k then the game says of a starting a series of rounds with a few changes before either player deals a card if there was a previous round in this game that that had exactly the same cards in the same order in the same player's deck the game instantly ends in a win for player one previous rounds from other games are not considered um wait if there was a previous round in this game that had exactly the same cards in the same order the same players decks oh otherwise this round's cars must be in a new configuration uh the player begins the round by by each drawing the top card of their deck as normal if both players have at least as many cards remaining in their deck as the value of cards they just drew but if both players have at least as many cards remaining in their deck as the value of the card they just drew the winner of the round is determined by playing a new game or recursive combats whole my otherwise at least one player must not have enough cards left in their deck to recurse the winner of this round is the player with a higher value card chat chat what as in regular combat the winner of the round even if they won the round by winning a sub game takes the two cards dealt at the beginning of the round this just went from zero to 100 really quick during a round of recursive combat both players have at least as many cards in their own deck as the number on the card they just dealt the winner of the game is turned over the curse player one draws the three card and player two draws the seven card this would occur if player one has it sorry if player one draws there's no three card good jobing they missed the three card wait during a round of christmas if both ways has at least as many cards in their deck as the number they just dealt for example player one draws the three card player two draws the seven card this would occur if player one has at least three cards left and player two is all the same cards left not counting the three and seven cards they were just drawn uh this okay what [Music] wait wait wait wait so nines they don't have nine so they need they both need to have that many cards three and seven one and ten nine and eight five and two one okay so it's not there so six and four four and three so then you play a sub game and determine the winner oh and we need to keep a hash map of all of the previous scene bro okay i sort of get this now um okay so need to do a public um boolean play um play game sure we're going to take this put this into here so it's going to be who wants we need the um list of integers this would be the players deck with then a list of integers of the crabs deck i don't i'm like i'm trying to figure out how to do this in my head too this is not working out at all uh okay so we call play game okay um we can just return players deck size is greater than zero this is gonna be boolean okay so we're gonna do um boolean player 1 and it's going to equal play den or play game new arraylist players deck new arraylist crabs deck and we're gonna check this um we can't count all the cards here um oh yeah we need to be able to count all the cards we get back list integer wow integer player part one player part one and then we're gonna need crab part one okay so there's that so now what we need to do is we need to play this out until we get the end uh after the game the winning player scores copied from the cards they have in their original deck using the winning player scores calculated from the cards they have in their original deck using the same rules as regular combat and above game the winner is 291 they have in their original deck is that 291 um well okay so we're going to use this as our input chat like literally this is on the next level um also i didn't switch uh sorry and switch this over real quick i meant to be on this screen for people to actually see my stuff um so play game part one and then we're gonna need a play game part two okay play game part one is easy um so now we're gonna need player part two crab part two so now we're gonna need to play game part two okay so the way this is gonna work is it's gonna be the exact same thing um but we need a new nap what we need to do now is we need to check if players card is less than or equal to player's deck dot size and pull uh crabs card is less than or equal to krabs dot size if this is true we need to recurse down and play a game um i start getting determined okay we need to then recurse into a subgame using um you just need to know boolean player one is going to be equal to play game part two new arraylist of the players deck new arraylist of the crabs deck um so the player wins hey what happens wins round nine the player won one oh so they keep the cards oh i see okay so we do here is we do if the player one i see i see i see if the player won me to do this so either way it's the player here otherwise we do this but this is now the crabs deck okay does this work for input okay i see icse does this work for the example input out of curiosity okay so we need to do is then oops don't close that i mean all sorts of confused here okay so we play part two we do the player one uh we do this exact same thing cards count is new total there lap okay i got 306. 306 306. oh we needed to be on the original 306 93 on the 291 of the original deck oh cause i'm doing still doing part one part two part two part two 286 291 what seven five seven five six two seven five six two oh because it's not in the right order okay um um all right did we just put this i don't wanna i'm like torx i don't want to but at the same time all right what's what's the format here all right so it's going to be so we need to know our depth in this we're going to need to know ins depth here somebody call this we're going to start it at game one so we're going to do a system ounce print line you do this depth okay and then you have a nice break here let's just mimic what he what eric has for us in the debug all right [Music] [Music] [Music] [Music] [Music] i'm just trying to get these dips right it's fine though um so this is saying four three instead of three four oh so it doesn't matter so it always adds the winner in first i thought it added in i'm okay so the player wins it always adds the players in first does that fix it we need to get what three oh i thought it okay i thought it would say the higher card i don't know oh maybe it's referring to anyways i think there's part one said the higher card and i just interpreted that there there we go okay back to our input uh this probably won't get our inputs we don't have the check for infinite recursion rights okay yeah well it is it's the winner's card so the winner's card is always higher in part one but part two yeah so that's why okay anyways all right we can get rid of these uh i don't want to get rid of them let's just comment them all out this is going to get messy in case i need to come back to these in a second so now the only thing left is to keep track of all the decks and prevent our infinite recursion um so we need a what is it the case the case is before the game uh before the player deals a card if if there was a previous round in this game that had exactly the same cards in the same order the same players decks the game instantly ends in a win for player one so it's only this game so what we can do here is we're going to need [Music] a oh gosh um we can just stringify this yeah ty goes up which means a tie which goes to player one it's not a hash map it's um oh no this hash maps hatchback we're gonna do a hash map here string and we're just string i'm gonna do what i did last time when i did a hash map which is just me just stringify these lists and dump them into here um uh this is gonna be just the cash or the previous decks decks new hash map and so we're gonna do is every time after we or every time before we pull a card out we're gonna do previous decks um nope not here if previous decks i didn't spell deck right nice dots contains key which is going to be the player deck 2 string nice and easy and then and the previous decks gets player deck to string equals the crab deck to string nice and easy way to store a dull a deck into our system for caching um so if either of these occurs it goes for player one right we just return true because the player wins um otherwise we do previous decks puts player deck to string crab deck 2 string let's see if this fixes our problem and no does not seem to fix our problem um because it's only this game right hold on before if either before either player deals a card if there was a previous round in this game that had exactly the same cards in the same order the same players decks the game instantly ends in a win for player one right okay so either it's we're running too slowly more likely something's wrong so unfortunately this is not a part of the example output is this code here so i can't figure this out from that um [Music] shorten them up a bit but also just prevent a bunch of two strings okay so [Applause] okay so give me something's in the win for player one let me just make sure this is being called how about this let's just make sure that we're getting a tie because if it's never i'm putting a tie then there's a problem oh hello what hi that doesn't sound right though uh oh it was right oh i was just doing so many two strings i guess it got really slow hey we did it that was very anticlimactic okay um let me clean this up good all right all right so for day 22 what we did is for part one essentially we get the reading all our inputs um just doing some parsing to get which person is the cards are being loaded for load each person's cards into their deck into a stored list here and then for part one we go through as we play a game basically the the game or the the problem today is essentially just war if you've ever played war they call it combat here but uh the game is war where you just flip over your top card of each deck whoever has the highest card wins those cards they go back into the winner's deck at the very bottom and you just keep playing keep doing that until one player wins so in this case we do that exact same thing while either or while both decks have a card size of greater than zero we just pull the top cards off each deck compare them together whoever wins gets those two cards making sure we always put the winner's card on first um or putting it into the bottom first before the loser card and we just keep repeating that process over and over and over until we get one of the decks having a size of zero in which cases this round ends we do some um we just do the kind of conversion math here that asks for that for the results or the inputs in for our finished problem and then we're good there so that's part one and then for part two it gets a little more complicated because it's the same game but there's a recursive step in the fact that whenever you flip over a card or whenever both you flip over card that is lower or equal to the size of their current deck size the game goes recursive and goes into it using the deck of that is currently there the deck minus the one that's flipped over goes into our cursive game that gets played out and then whoever wins that is the one that she wins the cards not the normal cards higher so that's the recursive step to it um the biggest thing is to get a recursion working but then also have the um the anti-recursion detection system where if you get a deck that is already or if you get two decks that have already been seen before in their order all the same then you need to stop the game is not tied it's gonna recurse forever and you need to return true for the player winning because the player wins all ties um and that's probably the only tricky part about part two uh beyond comprehending the question because recursion just by nature is difficult at least for me at least it's difficult for me to understand at first um but beyond that it's again the same thing as war just with an added recursion step um and yeah so there you go day 22 of adventure code hope you guys enjoyed it i'll see you guys tomorrow for day 23 [Music] [Applause] [Music] peace [Music] you
Info
Channel: TurkeyDev
Views: 168
Rating: undefined out of 5
Keywords: advent, advent of code, advent calendar, advent calendar 2020, programming, computer science, coding, java, advent of code java, coding advent calendar, programming advent calendar, advent of code 2020, christmas, aoc, java programming, elves, 2020, advent of code day 22, day 22
Id: 8-8b9-qSCIo
Channel Id: undefined
Length: 31min 15sec (1875 seconds)
Published: Tue Dec 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.