Check out a free preview of the full Complete Intro to Computer Science course

The "Pathfinding Solution: b neighbors" Lesson is part of the full, Complete Intro to Computer Science course featured in this preview video. Here's what you'd learn in this lesson:

Brian live codes the solution to the second half of the Pathfinding exercise part b. A student's question regarding how to go about solving this type of problem at the beginning of your computer science journey is also covered in this segment.

Preview
Close

Transcript from the "Pathfinding Solution: b neighbors" Lesson

[00:00:00]
>> Okay, so this does all of A. And actually, this would probably eventually end up working. Because if you have the A will just continue to spiral out, eventually it's gonna find B, but we want to do it on both sides. So we're gonna basically mirror this logic but for B.

[00:00:19]
You might be able to break this into a function that does it for both. I didn't, so I'm actually just gonna copy this. We're gonna do this same thing for B neighbors. So bNeighbors and then we're gonna have our own little bQueue. So in here we'll have bQueue.

[00:00:49]
Okay and then instead of BY_A, this will be BY_B and this one will be BY_A. And yeah, so you could totally do this by extracting this into a function and then just passing it into both. That would work. This is process bNeighbors. Okay. I think this should probably work.

[00:01:42]
Maybe it didn't. But let's check. So receive negative one and expected we've six. Same thing with this one. So both of these they're coming back as never finding anything. Which is not good. It should. It should find things. Thank you that shift aNeighbors, aNeighbors concat. So let's console log here.

[00:02:28]
Well, let's put on our log, may this function. So down here, We're gonna put logMaze(visited) and also up here, log it as well. So log maze(visited) lets try that and we'll try running it again, see what happens. All right. So we're getting, nothing visited. Okay, so this is not what you want.

[00:03:12]
We would like to visit things. So aQueue, this should have something in it. So there's probably something wrong with our get neighbours here. Check that out. So it's never coming back with anything. Which is problem, okay, so let's check out why that might be. Why that might be happening.

[00:04:06]
Coordinate. And let's click play. So that's a thing, got that. But it doesn't have coordinate that x and coordinate y. Y does it not have x and y. That's y. So up here in our generate visited maze, we just need to put x and y in here. We also need the coordinate to know exactly what its coordinates are.

[00:04:47]
Otherwise they won't be able to find that. So let's try that again. And there we go, that was it. So let's go and skip the rest of our tests here. We did six by six we want to do eight by eight and 15 by 15. There we go, spiraling out there.

[00:05:26]
You can see here it's meeting here at 38 38. Cool, yeah, I'm happy with the solution. This ended up being a bit more verbose than what I have in here in my course notes. I'll leave both of them so you can take a look at them. This one I used without doing any maps or reduce, whereas in my other one I used a couple of maps and a couple reduces as well.

[00:05:53]
So both are totally valid. I'm gonna go ahead and assert that this might be a little bit clearer, just cuz it's easy to read line by line. Okay, any questions about our solutions here? Actually while someone's thinking. Just one second. I'll let you ask. I'm the worst. I asked too many questions and I tell you to shut up.

[00:06:19]
Anyway, so [LAUGH] This was the bug here on line 32 and 31, is I forgot to put the x and y. If you haven't seen that before, it's x, x. Y, y right? But if you just if it's called the same thing, you can just do that and we'll put it.

[00:06:36]
Yeah, those two things are equivalent. But yeah, that was the bug. You got to put those there. Okay, sorry. Go ahead with your question.
>> Yeah. My question was, it gets to a point that it's so abstract. It's basically impossible to solve it even in a day. So how would you go about solving a problem like this?

[00:06:52]
From the very beginning, I guess.
>> Yeah, at the beginning of this course I definitely tried to warn you that colleges will spread this kinda stuff out over a couple of years. And I'm just throwing it all at you in an afternoon, right? So one, I am quite sorry.

[00:07:09]
[LAUGH] This is really difficult to process all at once. Really, for someone taking this course, I would invite you to give this, the time that it needs to kinda sink into your brain. I probably would try and do these like, one at a time. Maybe one a day or something like that.

[00:07:30]
And the reason why I feel that's fair to say is it probably took me two or three months to prepare all this course material. And then I'm just expecting you to learn it all in a day. And I already knew how to do this stuff, okay? So, one, I'm just giving you permission to feel this is super hard, right?

[00:07:46]
And two, there's really no substitute for just walking away from the computer. Taking a walk. Thinking about it. Exercising. Unwinding, playing with your family, whatever that means to you. That's what you need to do. Because you won't be able to sit down and do this entire course in one sitting.

[00:08:06]
Or you're just about 100 times smarter than I am, both of which are total possibilities. So, does that help any?
>> Yeah, but I guess also. Trying to solve it on paper before you start writing code will be also helpful.
>> For sure, yep. I mean, this is why when I, in college I, the first thing I bought when I was very poor in college was a giant whiteboard.

[00:08:31]
And that was the most helpful thing that I had the entire time. It's just diagramming it out reasoning through all these kinda things. I'm actually getting a big one installed in my new office that I just moved into, it's a whiteboards are great. But also if you don't have that pen and paper or Microsoft Visio or Google draw.

[00:08:50]
Any one of those things can definitely help you with just putting your very, very abstract thoughts on the paper.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now