Solving a maze in Rust!

Transcript

English (Auto-generated)

Has today we're gonna do the part two. So you remember how we solve that maze in Python? Well, I mean we didn't solve it together. I just kind of guided you through a solution I had written, but today we're gonna do the craziest thing ever. Gonna translate that baby into rust. Let's do it. I'm pretty intimidated. So Alright, so the first thing we have is we have the same exact mais we had last time we have a starting position and what we want is the exit. Right? So the first thing is we're gonna create a trait called maze. All right, amazes Well, it's gonna be something that works like this. In fact I called a binary maze just like I did last time because of binary knees. We're gonna implement the binary Vietnamese on this vector of vectors. But yeah, for now we know that we have a trade called maize. It's gonna have a function called find exit. Can operate on the same exact piece of data. This vector of vectors and it will return a couple right? See you size and use socks, which is gonna be a pair of coordinates. So yeah. Okay, beautiful. This might have to be a two parter because this is gonna be a pretty big project in rust. But yeah, we're gonna implement it implement binary maze for so you do import. I always forget you look it up real quick rust. Temple, it's four. Somebody's copying pasting these from stack overflow. So and it needs to be pub. Right? Yeah, four. I was right, look at that. We're going to implement it on vectors of vectors. Uh so I'm calling these things, I'm saying bull because in reality in my opinion you should be like true and false values. But yeah, you get the point. I mean I guess I could do like you ate or whatever we want these to be I think by default Bu 30 queues. But yeah, I guess we could do that and then make it a trait that we could use trade bounds to say like hey, make it something that's basically like an integer. Let's see. Rust trait bound, integer. Can we say that we wanted to be numeric? Oh num traits but that's a crate. I have to use a crate. Pretty lame integral types. It's gonna want us to use Okay, I want to go ahead and say you ate Okay. So we're gonna implement it and to create the same function. Oh and it needs a starting point, Right? So let's say start, which is going to be a U size and you size which may make a coordinate type Probably, but it's okay, so add this over here And it's mad at me because I have to have to implement this. I'll return like 3. 1 for now. All right, so we have to actually find an exit. Right? Um This is depth first search. So a lot of directions we go, we won't find one at all. Let's see. So is intimidating to me there might not be an exit. So for one this should probably be an option insurance. Um Yeah 1 1 something there we go. Option type expected to pull. Alright because here is still expecting a tuple. Okay. Probably mom. There we go. Got it. Got it. Okay so we're implementing this now we're gonna do is we're just gonna do the classic thing we do every time we're going to say if we can go up, if we can go left so on and so forth, then check those out. Right okay. So we will say woof intimidated about doing this in rust but we'll try our best. So I'm gonna create a vector with all the different changes we could make I guess to our our current position. So we'll say we can go up one. This is X. Y. Right? So we can go Over one. Go to the right by one. You can go to the left by one. Excuse me. Okay we can go up one or we can go down one. Yeah so this is X. This is why so we add one to X. Nothing to why we subtract one from X. Or we add one to wise subtract one from. Why don't affect the other. We can't go diagonal. Well we can change them both at the same time. So I'll call it you know like dx and dy for delta X. Delta wife which represents the change in X. And the change in Y. Alright. So I guess that's it. Right. We have to say like if we want to see if it's a valid position. So let's say self dot valid. Um Now we have to implement another thing on binary mazes. I don't know about this. What do you guys think? Okay. So I have to do is I have to make sure it's a valid position. Yeah. Let's go for it. Let's say X and Y. I don't know. Yeah. We have to implement that now unfortunately. So say function valid position. Yes. Which is gonna be It's gonna take self obviously take a reference to self though, so, I know there's gonna be an issue. Yeah. It's gonna want to be a reference. Hmm. But okay, we'll figure it out. I can do that and then I can d reference it. I think it won't let me Okay. Right now let's meet. But we'll see if that's actually legit or not. So yeah, X and Y. And we'll just say, you know, one. so X is based on the width that's gonna be based on whether we have any elements. So, first we'll have to say why Is greater than zero and all right. So, we know why it's legit. Uh We can't have a valid position if we have nothing, right? We have no rose. So we'll see if y is greater than doing right off the bat then we'll say if why is less than um hmm. Now I would have to be less than the length. Yeah. Of our matrix. Right? As the height can't be greater than the number of rows we have. Okay, now we can say yes. So it's something but it's less than the length so we have at least one or more rows. And why is it can be a valid index of that? Then we want to do the same thing for X. Wanna say x is less than Let's get the first thing. So we'll say zero element itself. The reason we do the zeros. Um the reason we do that zero thing is because we already know it has some rows, all the rows are the same size. This is a rectangle dot length. And if X is itself um actually can be equal to zero, Zero's valid position. Same thing for X. If x is greater than equal to zero. Yeah. And we finally want to make sure it's not a one, right? Because ones are the walls. If you watch the previous video finally say itself. Um we're gonna get lied to get the current, the role we want and then X. So why is our up and down excess dark side to side X and Y axis. This does not equal one. And if all of that, we can go ahead and say true. All right. So, and then we can have a little else in here. Go ahead and say false. Okay, it's mad at me. for some reason. Who knows why this is not a trait of I have to add it up here theoretically. We could implement binary mates for things other than a vector of blah blah blah. And it's gonna want these to be valid types typing information on these sides. I have to do the same thing down here so I should have copy and paste from the beginning. But okay okay let's say we turn true. If we're down here go ahead and say return. I'll just say boss. Here we go. Okay. But it's not happy because you can't find that right? Because we don't know what X. And Y. Are going to be so will say mhm. New X equals start zero. Mhm. Plus the change in X. And I got in there and let me why equal you know the drill. And it's mad at me for some reason. Oh you size can't be negative. Oh no that's a huge deal. Just get rid of all these you sizes. Okay. Why can't be negative valid position. Takes a you sized. Uh Oh it can't be negative. This is pretty interesting. Yeah, you can try to combine them or whatever. Right? I'm gonna change this all to like what I feel to be a more same size. I'm gonna change them all to you eights, right? See if it's happening. You know that's unsigned. Okay. For one he's gonna be mad about this thing. Like it's nonsense. You said you ate so you ate means unsigned, obviously it can't be negative to say I ate so it can be signed now. And that apparently made this very angry. So let's see what this issue is now. So I found a use size. Where did find a use size. Okay. Okay. I'm gonna say as I ate and as I ate and cannot be indexed by that as you size. Mhm. And again, as you size and there you go tapping. Now I live to please compilers let's check out X. And Y. So obviously the new X is gonna be new. Y. But it's pretty happy about that. So if it's a valid position then what? That's the question. Right? Oh really? What? So it's a valid position. What we want to say is hey, are we at the edge? Because if we're at the edge then we found an exit. Right? So before we even say all this stuff about new exes, we should just say is the current position of freaking exit. So let's just see Start.0 equals zero or start 0.1 equals zero. Mhm. Okay. Or Yeah, I can do it this way or huh or Or start that zero is the exposition Yes. Or for the edge. Okay. Start that zero is uh is equal I guess it's equals self dot length and it's one, right? Because there's zero indexed or if start. Right, Okay. I guess that's that's a good start if any of those things are true then her turn start I'm gonna change this from start current position because this isn't always where we start. This thing is gonna change like crazy and return this right? We went through all this and didn't find any valid position is just returning on. Yeah but in any case. Okay so expected found you size as I ate or whatever. Wonderful. Okay. Also though we want to say hey uh if you know if current position no if self dot length. All right so start length It's greater than zero it better be uh then we have something to access there. Then we can say if we forgot to check one thing we forgot to check if we're at the edge at the right edge. Right okay. Then we can say if um self zero dot length you know -1 is greater than current position dot but uh you know what this actually is our our position. So this should be one In position.0. Then we're gonna go ahead and return that the current position is indeed legit get mad because I'm comparing something to something as I or something I don't know. Oh no because this is a youth size as you as I hate. There we go types types are beautiful thing. Okay so we're almost done. We saw that you have to do all the actual work of then saying like okay well try out these new positions blah blah blah. So um we're gonna loop through all of these. All right now we're gonna say match find exit on a one of these things I guess. So we're gonna try all of them. Let me think so. We're gonna try it. We're gonna go deep as we can. If we find an exit we're good to go. If not it'll be none. So we want to match some so find it, find eggs. It will give us a sum. Just give us the first some you get and we'll return it. We're gonna match find exit. We're gonna pass it not current position but rather a couple consisting of our new X. Their new why? In addition to that couple we got nothing. There is nothing in addition to it. Okay. So you can see that truffle once I match this now. All right. Uh Sorry. You say self dot find exit. That's either going to be some and then it will give us you know our new position or like exit. It'll give us an exit actually if we find some exit then return exit looks good to me. Yeah, if we find some exit we return some exit we get none. I mean can we just ignore none. Like you just not care about none. Is this legit? Mhm google that rust match option. Don't include none. I don't know if you do have always included none. Mhm. I guess what I'm saying is the match have to match everything. I guess. I'll just ignore it. I'm gonna find out. I think we're done. Like in theory we should be pretty done. So an equal match. What are we gonna match? We're gonna match maze, nope maze dot exit dot find something I see is dot find exit. For some reason it's not there. Dot find exit, our position is start and I'm saying match. So it wants us to do a whole operation here. Mm Okay, just wants me to cover them. So I'll say some exit if I find one. No, print it. You can just print couples. So I'm gonna just try that. Print it didn't like that because I forgot that. There we go. And you can't do it. Can I do a debug E display? Yes, I can. Does not like that because none is not covered this. I'm telling you in it down here, it's gonna complain. OMG nine. I say just do nothing. None. You just say just say none. Okay, that's cool. Just none. Just like yeah, I don't care. Mhm. Beautiful statement. Excuse me. All right. What are we missing here. What are we missing? Right. I haven't fixed this. None yet. So I still have to add like, hey if you find a nun, I just don't care at all. Alright. And unused variable because this is finding the exit, which is unnecessary. Okay. Alright. So yeah, we just match it and do stuff conditionally if I'm going to change this because this is the exact text I want up here. So cool. Rusty. It was open terminal terminal. I said Terminal terminal. Let's run it and aired aired. Why is my I. D. Usually like to text these areas long before they become an issue. Whoa. What the heck is this thing even talking about X code. Active, developer path does not exist. What on earth did they do? I think I O M G. I'm pretty sure I installed X code. Yeah, I did an install X code and it turns out, I'm guessing I can't run this without that. Okay. Here's what I try to do. So you got you all see this code, right? Let's start a new video. But let me see if podcast house podcast doing then it's podcast. Okay. I'm gonna see if I can change screens. like they have this one side. What's it called? Uh Grapple. If I can run a rough ripple. Yeah. Yeah it does happen that way I can just run it on here because I broke my computer, I didn't break it but I broke X code. So that's also bad sign up. I am signed up. I remember I think remembering chrome, I clicked apple music. Why? How do I do these things to myself? Okay, ripple it chrome. Thank you. I'm sorry, replica chrome rep it rust all of this. I just had to uninstall X code. Why why did I do these things myself? Like what's what's my thinking? They're okay. Sign o the times you're crazy. I remember telling you people just running right there. But you know what I'm just gonna, what am I doing? What am I signing up for websites right now? Yeah, let's just run this right here and see if it all doesn't want to show you a little play button. Okay. Run it. Well, certainly getting nearer. I mean nearest exit 3 1. Whoa. Wait is that what I gave it, did I give it 31 to start at? I think I did because that's not the nearest exit, that's the starting position. Okay so what's going on? Just printing the start position? That's not good. Okay so if this one equals one which it does want to check if it equals zero. Okay we don't want it to equal one. Oh that was all bad. There's one equals zero to see if we already at the edge. Okay. Okay so if it equals the length minus one. If it equals zero, if the other one equals zero, Anything is zero. If we're at the edge. If we're at the other edge. Yeah, it looks pretty like, okay. Why is it? Why is it just returning the first? Oh, okay. Okay. It's probably here. So we'll just code this from here from now on. It's going to be the same errors anyway. I just hate these lines. I don't think they'll let me change the style either. Okay. Yeah, these lines are too irritating. I'm gonna go back over here. Let's see what we got. So just returning our start position. That's the mystery we're in right now. For those of you who are wondering what's the mystery that, that is it. That's the mystery. Okay. So it should be changing, right? I mean, this is R. X. R. X. Is indeed and I think it's gonna give me each of them, hmm. Okay. Return some exit. Wait a sec. Wait a sec. Wait a sec. Wait a sec. Yeah, those are, those should be the new positions. Okay, This is, this is indeed a mystery everyone. A true mystery. So let's go in here and just print current position to get an idea of what it thinks it's doing. She's a debugging, print current position. I have to type everything cause I have auto complete in here. Okay. It doesn't do anything. So no recursive calls are even happening. Okay, let's see if this is the thing that's printing crank line. It is totally here there. Just move that somewhere else. Let's try here. Print something. Oh I know what I'm doing, you know what I'm doing? I know what I'm doing, I know what I'm doing. Okay so I don't know why this would cause this but sometimes what happens sometimes what happens is you go down and then you come back up. In other words you go to positions you've already visited and that's no good. So we need to check already visited equal to an option back there of type. Yes option of a vector of type I eight coordinates. I that's gonna have the same thing up there. In other words because you could just go up and down up and down in the same position forever. Right? Like what would prevent that? But if that were happening it would lead to I mean we still shouldn't get this is like our hair whatever. So which makes one wonder what exactly is happening cause it's not printing in either of those things. Oh it totally is here, it's here. So things are at the edge. I'm just saying that cause an infinite incursions that's also also very much a bugs. I'm gonna fix it here first. Oh okay so every time we visit a position you need to add it to that victor. Okay. It's gonna be mad that we don't have this, what do we pass it? We're gonna do a match on our about that. Got a match I'm already visited. Yeah if it is equal to some. Mhm. If it is equal we just we use plus with a vector thrust plus add two vectors, add value to victor. Okay like I must extend. Okay. That's kind of lame though. There's gotta be an easier way to just add something to a vector dynamically. Okay we'll just say let me just I'm gonna shadow it. I'm gonna let it equal you know what here let's try this equal. I'll just match it from the match already visited. If I get some dr we'll just be equal to that vector. If it's equal to none we'll create a vector for it. Here we go. So now we have a vector and now we can definitely push to that vector. So I'll say already visited. Push. And what am I gonna push my current position that way when I'm looking for new positions I can check if it's a valid position. Right? Well in addition to checking if it's a valid position, I also want to check it's not something I've already visited and not already visited. What does it Kings new X. It's new X. And knew why once a reference. There you go. It's mad about this. Why unreachable. How is this? House is unreachable? Alright let me just check if value. In fact maybe it's not contagious. How do I check the thing as a vector that contains So I guess it's like a 16. Oh yeah I already visited. It's definitely gonna contain that. Yeah about it anyway it's pretty strange right now. My mind is being born. It's gonna be like an ancient long podcast podcast. Oh okay saying I think it's saying that this can't find it. Anyway let me try and find the exit again. Yeah. Okay so it's like it's saying hey this is never gonna be a valid position. Okay maybe I don't know. This is pretty strange though. Yeah unreachable call. I think the thing that matches broken. That's okay let's let's go let's go back to where we were. Okay. I think that coach is probably fine. So we're gonna match it o match find exit match already visited. Okay that's all bad. Yeah there we go. So if we get some exit I totally forgot what I was doing. Okay. Yeah. Yeah so here they say match find exit and we just passed it already visited. That's it just gonna pass it already visited. So we're gonna here we still want to match it. So I'm gonna match it. Find some exit function. Can just return exit if I get none. Yes then we can just chill. And this is gonna be mad because I wanted to pass it already visited. Probably gonna get mad about borrowing its value. Has every right to get mad about that because we passed already visited which is like a vector and then we're gonna call and use it again. So the borrow checker might get a little mad at us which I will find out. It's giving me a red line here so let's see okay there we go. I knew it wasn't that bad. Yeah, borrow after move. That's it. That's that's exactly what we were expecting. So and when we move it I'm pretty sure he's here at this find exit. So that's why I'm gonna I'm just gonna cornet and we just create a new copy of it every time and that should somehow make this happy. I have no idea. I don't know what makes computers happy and but a man alright it's not it's not mutable. That's a huge deal that that is legit, okay this is a legit issue issue right here so let's see. Mhm. Right. Oh gonna be a mutable vector. Okay sorry that's where I put them up here. Like it doesn't need to be beautiful. So it's because I was right because I shadow it right here. So this is where I would need to make it beautiful. Let's try it again nearest. Oh I'm gonna kill I'm gonna kill it 31. I wouldn't do this to me. Clearly the wrong answer. Mhm. Right I remember this right this is our original issues that it's running here it's running it here where it says like okay if the length is greater than zero, get the length of the first one minus one and make sure that that's greater than or equal to greater than or equal to people yep. Newbie mistake that's right here because If we if we're getting -1 I'm forgetting -1 that actually could be equal to it. So that's right now. Well okay let's see if this is not the issue. I was thinking the length should definitely be greater than it. Right? So if the length wait this doesn't make sense. Okay but I should be saying of course the length is greater than current position. That's why that's dying if current positions here so X. Is somehow greater than it shouldn't be definitely can't be great. Again if it's equal to itself dot length as I. A. And it's one and that's legit and it doesn't print anything I broke it. So I'm gonna re add that print that probably should be a two parter. Like for those who are who are watching this whole thing up to this point. You are soldiers go ahead and straighten if we're doing any recursive calls or not in her current position. Okay. No recursive calls. No there should be some places where it's a valid position and it's not already visited so I'm just gonna print by saying you know hey we should make it here if we don't make it there. Maybe not. Well I'm seeing if already visited does contain it it should not contain it and now I get an infinite loop. Beautiful So we're getting 323232. So what's the issue here? The issue is that these should be already visited. Right Yeah So 32 when we pass it to to to should be a member have already visited. So it's not in our current it's not not already visited for some reason. So let's print out already visited. Can we push And we see that? Yeah it's 31 and 21 are both in there like a bazillion times. Those obviously should not be accessible positions. So let's see what's going wrong here. So already visited is definitely the problem because it does contain these things right like saying it doesn't but it clearly clearly does. Yeah yeah that's how it contains it's supposed to work but yeah but for some reason we're still getting in there mm. Well this this is definitely a mystery to me because there shouldn't even it's not even possible to have a repeat value in here. Okay so I'm clearly doing something very wrong. So let's see maybe you don't compare couples that way. Okay very possible. I'll see you comparing anything let's see if you just use an equal sign looks like we should actually be able to do this. Mhm. Okay well we have like a legit bug right here we have a bug on our hands they don't have access to the compiler because I'm a fool. Okay I'm passing the cologne have already visited. We match it. We get the vector. Oof. I won't be able to use it again. You got something else we could print is what does this actually print? Like is this true or false? Let's go ahead. Let's go ahead and try to print that. I think that's our book. Mhm. Yeah, those are just normal about these things. Point that way. Let's try it. All right, okay. It was pretty unreadable to me. So, let me just look up like rust operator. I think it's that Right, okay. Okay. This person just think, Yeah, so that is right. I don't know what I'm doing wrong here. Check it out. I'm gonna add it over here to make code. I can see what I'm doing wrong because this editor is a friendliest. Okay. Yeah. Alright, I'm just missing one of those. Okay, stop it. It's printing true saying it's not in there, even though it totally is. Okay, well, that's that's for sure an issue. Oh, check for tuple in vector. Rust. Yeah, yeah. Oh man, this is some drama. So I don't think I'm like, I think he just doesn't want me comparing like to like this that's definitely my best. You know, we can do. You already have two code over there. You just only include made and then just compare directly to pupils and see if that's what it I'm gonna make a vector vector is just gonna have like one to pull in it. Boom. two b. 1 one. Now I'm gonna print like does this have the thing I want to print line rest rest is hard. Okay I'll print maze dot contains. I have to pass the reference. I'm gonna pass the reference to 11. It does contain it. What is going on? I'm I'm flabbergasted so you can check if it contains that but for whatever reason. Mhm. I just want to make that happen right now pushing it and pushing in current position. It's pretty legitimate to me. All right let's print it just one more time just to make sure it actually makes sense. Yeah we clearly have a vector with a bunch of pupils in it. It has the nerve to say true. Yeah. Mhm. So this is gonna be our new position. Right? So okay so that works. So instead how do we try this? We'll print already visited still. And I'll also just go ahead and print the new position. X. Wait didn't do this before it's like the whole problem. Okay. Current position. Just make sure you have dx dy okay. Always include the wrong number of these things. Okay and I need to use debugging display because I'm gonna be printing a bunch of yeah so I'm going crazy here people it's printing to one even after it's checked like okay I want a printed right next to each other right so let's go down here. Do what we say? Print line just to make, just to show you how nuts this is. It's Gonna Print 3 1 the green one and then be like, oh yeah, it's totally not in there, but it's totally in there at the very beginning. It won't even let me go to the beginning cause I printed so much stuff. Oof. So new X and Y is printed at first. Right, let's just go to the bottom. So Princeton eric Snow. Why Prince things we've already visited. They're just like, yeah, totally. We can go to that position. Well, I think this podcast is getting pretty lengthy so I'm gonna end it for now. But this has been on my own and I'll be back to you. Thank you so much for your time. Bye bye.
100 Views 0 Likes 0 Comments

We've done it in Python, now let's try it in Rust!

Comment
Leave a comment (supports markdown format)