About this video
What You'll Learn
- Map bag-color containment rules for Advent of Code Day 7 and model parent-to-child relationships.
- Implement input parsing by splitting each rule into container and contained bags with counts.
- Build recursive functions that count outer containers for shiny gold and total nested bag quantities.
David and Ian George (Orlando Go meetup organizer) tackle Advent of Code 2020 Day 7, the bag rules puzzle, live in Go. They cover the philosophy of the puzzles, input parsing with fmt.Sscanf, and recursive logic for counting containing and contained bags.
Jump to a chapter
- 0:00 <Untitled Chapter 1>
- 0:54 Introduction and Guest Welcome
- 1:47 Guest Background (Competitive Programming, Go Community)
- 2:18 Orlando Golang Meetup
- 7:51 What is Advent of Code? (Overview and Philosophy)
- 1:00:11 Introduction to Day 7: Bag Rules Problem
- 1:03:01 Discussing and Implementing Input Parsing
- 1:11:02 Split String
- 1:15:58 Developing the Part 1 Algorithm (Counting Container Bags)
- 1:31:48 Running and Submitting Part 1
- 1:32:45 Load Test Files
- 1:37:42 Introduction to Part 2 (Counting Contained Bags)
- 1:40:17 Attempting to Solve Part 2 (Recursive Sum Logic)
- 1:56:48 Part 2 Result and Wrap Up
Full transcript
Generated from the English captions. Timestamps jump the player to that moment.
Read the full transcript
0:54 Introduction and Guest Welcome
0:54 Hello and welcome to today's episode of Rawkode live. I'm your host Rawkode. Before we get started, I just wanna take ten seconds to thank my employer, Equinix Medal. They allow me the time and energy and resources to spend my day working on these streams and producing content so we can all learn together. Today, I am joined by a former colleague of mine, Ian George. Hey, Ian. How are you? Hey. I'm doing great. How about yourself? Yeah. I'm doing alright. Thank you. So you we work together at Infox Data. You worked on InfoxDB. You're also the organizer of the Orlando Go
1:26 user group. That's right. We meet every Wednesday. Every Wednesday. That keeps you busy as well then. And and previous conversations, you've mentioned to me that you used to do competitive programming. So let's just quickly summarize all of those things then in the next few minutes. So feel free to give a quick introduction, and then we'll kinda tackle them one at a time. Sure. So just like you said, my name is Ian George. We sort together at Inplux. Frankly, unfortunately, today is kinda like my first day not Inplux. I'm kind of like in between, what you call it, adventures.
1:47 Guest Background (Competitive Programming, Go Community)
1:59 And but it was genuinely probably the best place I've ever worked because of people like you. Anyway, it's it's a wonderful place to work, and I'm kinda missing my team or the team that I was part of already today. But this freaks me up to do cool stuff like this. Yes. I run the Orlando Golang meetup, and I'm sorry. Said we do it every Wednesday. We actually were doing every Wednesday, but I kinda tapered it off. Frankly, I I find that I pretty pretty quickly run out of, like, ideas and speakers. But for me, it's mostly social now. Like,
2:18 Orlando Golang Meetup
2:34 I I don't get a chance to get out, so I enjoy just, like, hanging around like my people. And, yes, my first exposure to kind of competitive programming was in high school. I used to I was in a program where we went to technical schools half of the day. And at technical school, I was at the IT program, and they had a an organization called Phi Beta Lambda. And there's basically I don't remember the I don't actually remember, like, how I fell into that, but within that had awards that they would give. And one and
3:13 one of them was for kind of, like, the IT track and required a bit of competitive programming where we you know the the the first one I did was local, then I made it to the to the national level in Washington, and we we we competed there where we were given a problem and had to code it up. And I've somehow the other one won one at the national level at the time. And then in college, as part of the ACM, we used to do the regional programming competition. We made it we made it as far as, I think,
3:44 Southeast Regional competition. So so, yeah, that that was that that was kinda like my introduction to competitive programming. But more importantly, it kind of introduced me to these sorts of problems that we're about to tackle today. Right? Kind of I can try problems that you have to sort of complete under time constraints. So it's like a little bit of pressure. And yeah. That that's it. Nice. I mean, during competitive programming, it's just damn. And I I I can't even think about doing that kind of those problems and trying to worry about all the constraints and
4:18 and being timed and competing. Too stressful for me. Yeah. But but, unfortunately, it's gonna mean it's gonna work out quite well for you. I mean, you're now well, you worked on a database, which is not necessarily the easiest software to be working on for a start. Right? Like, that is that is tricky stuff that involves optimizations and a whole bunch of other things. And then there's advent of code. So, I mean, no pressure. But this must be this must be easy for you. Right? That's you're just gonna smash it at the park in fifteen minutes, and that is done. Right. Right.
4:45 Right. So so yeah. That's the thing about especially at end of COVID, the way I've been tackling it is I wake up in the morning, kinda check it, and, you know, sort of in the kind of like a lot of these things we do kind of the we we kinda do in our in the privacy of our own home and and without any, like, pressure. And so this does so doing this in front of an audience does kinda add add quite a bit of pressure. But one of the cool things one of the things I
5:10 like about it is that what I find is that you tend to kinda bring your own toolbox to these problems. You kinda bring the things, you know, whatever algorithms, like language, constructs, stuff like that that you know to a particular problem. And and once you solve the problem, that's that's neat and interesting, but I think where it becomes fun is to compare what you've done with other people and see how they tackle the problem in a different way because what you find is that like, I have a good friend who solved the previous was it previous wrong? Anyway, in particular, I
5:45 have friends that I hang out with on on Git I mean, on Slack. And almost every day, we kinda start with, like, hey. Have you done the new admin or code? And what's always fun is to find out, like, what things he did differently to solve it that that I didn't that I didn't do. And in in a way, I kind of, like, learned to kind of expand my my toolbox. I I I do that too. I mean, time I do an advent of of code problem, the first thing I do when I finished it is go to get help search
6:11 for advent of code, filter it by what was updated recently. Absolutely. Just look at what other people did and not only what other people did. I like investigating how other languages solve the problem as well and just kinda compare and how that works. Like, I I find that more fun that well, I enjoy solving the problems when I do solve them. But I find the more socially bits around that pretty cool as well. So Yeah. Yeah. Yeah. Yeah, today's you know, when when you mentioned this today, was like, oh, yeah. Yeah. This is this is good. You know, I have no problem
6:39 with this. And then I looked at today's and I'm like, oh, we're screwed. Right? So we'll see. What so I'd like to do is these problems are typically, like, broken down into two parts. Right? You usually have, like, you know, an initial problem, and then once you solve that, you get sort of an expansion of that problem with the same dataset. And it's usually kind of like some sort of refinement or or maybe even, like, a complete change of the original original problem. But and so what I'd like to do is after looking at this one, frankly, I have no idea how
7:14 to, like like, solve it. And so, personally, I just like to get through the first half, and let's see how quickly we could do that and efficiently we could do that. And if we could get through the first half, then maybe we could tech kinda tackle the second half. Or if we can't, maybe we could, like, save that for, like, the part two or something like that. Oh, yeah. Definitely. Always up for a part two. I I've I pulled up the challenge this morning as well, and I was like, we we picked the worst day.
7:37 Right. Yeah. Yeah. Why you doing in fact, do you have the advent of code screen up, or shall I pull up a couple? I can pull Sorry. I'll I'll I'll do it. Let's see. Alright. So I'll share my screen first. And forgive me if it looks like I'm staring out out of space. I have two monitors, and so I have you on, like Yeah. I I did that too. And I I I I think I've got it tweaked just enough that when I look at each one, it's actually not too bad. Although that's why it's screen goes right into here and
7:51 What is Advent of Code? (Overview and Philosophy)
8:12 I probably don't wanna look at that. But it also means I can't share the screen, which is also really annoying. Anyway, not important. So this is advent of code here. If you're not familiar with it, every December, someone goes out of their way. I can't remember their name. Let me find. Eric Wastel Wastel. I'm not sure. Puts a lot of effort into producing all of these problems and they publish one a day, two parts. You can go and have a bit of fun with it, share your solutions with other people. It's just a good way to experiment.
8:40 I use it as a way of playing with new languages generally. I think that's quite a fun approach to it. And it's good. And I like what it what he says here is that, you know, you don't need to have a computer science history to be able to tackle these problems as well. Like, they're they're not there to stump you or challenge you too much. So they're to kind of let you to explore and have a bit of fun with it as well, which I think is really cool. Do you have a computer science back? And
9:08 you do. Right? Yeah. I I went to school for computer science. I have, you know, full full disclosure. I never completed my degree, but I got really close. But I sort of and once you start working, it's hard to, you know, like, go to school and work at the same time. So I moved to Orlando with the intention of finishing my degree, but I'm still working on that, I guess. And well, yeah, I I never even started college or university for computer programming. Like, I just I I don't know. I I spent so much of my youth kind of hacking and
9:37 coding and c and I just got my first role. And I never really felt that it was holding me back until maybe five or six years ago when I started to look at these problems and realized, oh, I don't know graph theory. Oh, I don't know how to infer a binary tree and all these other things. And it wasn't until like, only in the last five years I started experimenting with that stuff in my spare time. So and I guess the moral of that is you can actually get a career in computer science without any of this stuff or at
10:01 least a program without any of this stuff. Absolutely. Absolutely. I mean, I also like to spend my time just building CMSs and websites, but that was fun too. So today's problem is a good one. I'm not gonna sit and and read this all out. Maybe I can try and summarize it the best as I can. So the problem is is that we're gonna be given a dataset, an input set, which is a collection of bags. And there are rules that dictate which bags can fit instead of which bags. And our goal is to establish how many potential combination or what bags we
10:38 can get our golden bag into. Is that a do wanna go into that in any more detail? Or Well, so I think our goal is to count how many bags in your dataset can we fit our our golden was about to scroll your screen. Let me pull it up too. Oh, yeah. Yeah. We could swap over. There's no there's no point in me. Gonna send it back. So if it's okay, I'd like to share my screen. Go for it. Yeah. I'll pop mine off. Okay. Can you see that okay? Oh, perfect. Alright. So let's see.
11:23 So, yes, we have, like, a series of rules that dictate, like, what bags can fit in another. Like, in a light red bag, we could it can contain one bright white bag or two muted yellow bags, etcetera. Some can contain other bags. So for this problem, how many bag colors can eventually contain at least one shiny gold bag? There's a couple other constraints there that I think kinda stand out. Let's go well, there's several there's lots lot of stuff that stands out. But let's talk about the fact that let's see. Where is it? Ah, yeah.
12:01 You have a shiny gold bag. If you wanted to carry it in at least one other bag, how many different bag colors can you I guess my point is I can't find the the section right now, but there is a notion of depth like oh god. Where is it? These are all specified required contents. Shiny gold bag if you wanted to carry it. Well, let's let's let's get that part for now. Here's some other things that stand out is that the first problem only asks us to count how many bags we could ultimately fit our
12:40 shiny gold bag into. But our constraints have numbers in them. And for the first solution, I don't I don't see how we're gonna use that number. But I what I find is typical with these admin of code things is that that's probably gonna be in or, you know, that number is probably gonna be involved in the next half of this half of this problem. There's a a term I kinda learned I learned a while ago called Chekhov's gun. It's like a old, like like, theater concept that if you put a a gun as a prop or hang a
13:14 gun on a as a prop on a wall, you better use it in the next in the next scene. And so I think that that's the thing here. Like, we we have Chekhov's gun right here in that. We have one bright white bag, two muted yellow. So keep that in mind. So what we'll what we'll try to do, I guess, is keep track of that as we as we solve this. Does that make sense? Or Yeah. Otherwise, I can have a slightly different understanding from you, which has got me worried now. So Okay. Like, the way I read this is that
13:42 the light red bag can contain one bright white bag and two muted yellow bags. Uh-huh. And then the number there is for the depth that I think you're talking about. So if they can hold two muted yellow bags and a muted yellow bag can hold two plum bags, then we need to know that that's four plum bags that can be held in two muted bags and say the one light red bag. Oh, so one more time. So what what do you mean again? So let's go over the example and see if it makes a bit more sense then because
14:09 my understanding is if we have a yeah. Where is it? Yeah. So in this example, like, so if I have a bright white bag that can hold that can hold away. Oh, yeah. That's not the rules. Let's see if I got it here. Oh, yeah. The buffer oh, that was the rules. Okay. Yeah. So if we have a light red bag, then that can hold one bright white and two muted yellow. Plus we know from the fourth line that the muted yellow bag can actually hold two shiny gold bags plus nine faded blue bags. Mhmm. So
14:44 that means we could if then if there's any combination of nine faded blue bags that can hold other bags, does that is this not recursive in that approach or if I just misread that completely? I think it is recursive, but I don't quite understand what you mean by or how we use that that that quantity or that volume right there. Okay. So let's see if I have an example. So if we've got a muted yellow bag, right, we know that can hold two shiny gold bags and nine faded blue bags. Now the two is important because we know we can hold
15:14 two gold bags there. But at the same time, the nine is important on the faded blue because we know that faded oh, no. That was the worst example because faded blue bags contain no other bags. Oh, I see what you're saying. So what you're saying is that if we're gonna sum up how many shiny gold bags we can contain let's say, for example, that faded blue bags could contain shiny gold bags, then you're saying our total would mean that we could contain 11 bags. 20 gotcha. Gotcha. So let's see. So you have a shiny a shiny gold bag.
15:48 If you wanted to carry it in at least one other bag, how many different bag colors would be valid for outermost bag? So what I get from that is that we have one bag. So regardless if we if, you know, like, say, a sub sub bag can store nine of them, it doesn't matter because what we wanna find out is so imagine imagine a graph. Right? And so we have lot lots of bags or we have bags that are connected to other bags that could that could potentially be connected to other bags. And so I think what we wanna
16:23 do is find out we wanna and so each of these lines but it basically indicates kind of a root or kind of starting point, you know, of our search. Right? So what I'm imagining is kind of searching through this this space. We're looking for we're looking to count how many shiny gold bags we could or I'm sorry. We're looking for how many distinct bags that we could store shiny gold bags in ultimately. Right? Oh, that's great. And so I'm saying I think we it already say light red bag and then traversing graph described by the these
17:04 these sort of adjacent nodes that that these parts describe. Right? It's like so light red bag contains, and then this part, in my mind, describes the the edges of of the graph. Right? And so we walk the edges until and count the the edges that end in a shiny what were a shiny gold bag node regardless of the the count. But you might be right. Like, I what I'm afraid of is the next part. Right? Yeah. Yeah. Whatever that is, I think those numbers are gonna become important. But for this, I don't think the numbers matter.
17:43 Okay. Yeah. I misread it. So, yeah, we only have to identify the colors that can eventually hold a shiny gold bag, return that number, and I bet you part two is how many gold shiny gold bags can be stored in this combination. Right. Right. Right. So, typically, what I like to do is maybe sometimes to the annoyance of people I I work with is come up with, like, really strong metaphors for what we're doing. I like to I break this down into kinda like nouns and verbs. Like, what what what do we have? What what kind of things
18:13 we're doing with it? But, also, in solving these, just know that we might go down we might go down paths that don't work. What what we'll try to do is break it down into small enough chunks that if we get to a point where we're of paying ourselves in the corner, maybe we could pop pop a few things off and then kinda kinda restart. Hopefully, we'll see. But so what I'd like to do is start with well, we're gonna do this and go. And I'm gonna move this to the side of the screen. So if you see me kind
18:42 of, like, stare off in space, I'm looking at the the instructions. Okay. Let me k. So I'm gonna open up the terminal. So we also got our first comment from John. Hey, John. How's going? Yes. Sadly, Santa Claus does not create the advent of code. I'm sorry. Burst that bubble, mate. Yeah. Yeah. Also, please, if you're in the chat, please don't, like, spoil it. I we honestly haven't, like, figured this out. So I'm I'm open to help, but don't don't give away the answer. That would be like, that completely ruined ruined everything for me. So let's do this.
19:23 Use zoom in on the front of it. Awesome. Thank you. Alright. So we're gonna do this in Go, which is I already oh, I'm sorry. Let's let's talk about this program. I mean, this problem is some more because this is another thing I see, which is that we have the task of traversing this graph, which is one problem in itself. Then we have the task of parsing this stuff. And let's take a look at the puzzle input. And so hey. This is super long. Right? This is pretty pretty lengthy. And the language is a little bit complicated. Right? So we
20:11 have these kinda like adjective noun bags contains. So what what I'm seeing is, like, a little language emerge. Right? So we have you should have these these two things, an adjective and noun, bags contains, then some number, and then another adjective and noun, bags or bag. That's so that's, you know, complicated. Right? Depending on the quantity. But then the edges of the graph, which I think we described here, is actually a comma delimited thing or could be. Right? Or even worse, it could be like where was it? This one there was one that was, like,
20:58 no other bags or something like The rules are just above you there. Yeah. Yeah. Yeah. Yeah. So it contains no other bags. So what I'd like to do is separate this into two completely prop two completely distinct problems. One is the act of, like, following the rules and and traversing the graph, and then the second part would be parsing this. And so let's let's see how that works for us. Alright. So alright. So what I'm feeling is so we're gonna write this in Go. Let's create Go modules. So Go mod with GitHub.comag64a0cseven. Okay. Alright. So I have a go mod file,
21:42 so now we could kinda start kinda hacking away and stuff. So what I feel when I look at this is that what we're modeling is rules. And so I consider this kind of an entire rule set. Maybe each one of these is a rule. And so, like, literally, when we're talking about it. Right? Like, some process would be iterating over this. And for each rule in our rule set, try to walk walk the graph until we find some target. So what we wanna do is sort of build a graph, iterate over each of the nodes or in
22:31 in the graph, and using that as a route, see if we could trace a path to a desired target, I think. So how do we how do we model that? Let's see. Alright. Here we go. So let me start with Google.com. Do you write tests when you do this stuff? Yes. So this is so in fact, that is really the only I like none of my event of code attempts have, like, a main function. So what we'll do at some point is split and then start start running running a test. So let's start laying down some code first.
23:17 So I think the first thing we need is a rule, and looks like a rule is gonna need a name and a maybe a quantity. Right? Alright. So quantity is an int. Name is string. Alright. Alright. And so what we're modeling is sort of rule sets. Right? So at some point, we're gonna need a slice of rules. So that would be something that encodes this part right here. Right? So we're gonna need, like, one bright white, two muted yellow. Right? And so that feels like a a slice. So maybe you could say, like, type rules.
24:15 There's a slice of rule. And let's start right there. And so here, let's start a test. Right? So package, o c seven. And let's okay. So one of the things I wanna do is search for a target within some rules. Does that sound like a reasonable place to start? Yeah. Yeah. For sure. Alright. So rules. I'm gonna say r rules, and we're gonna contrive some test data here. But maybe we'll pull some directly from this input. So I'm gonna copy it from the admin code page and paste it here. K. I'm gonna drop my font size a little
25:17 bit. Is that still okay? Yeah. It's alright. Yeah. I'll tell you what. So let's see. So the first rule, have light red bags. Alright. So here's something else I'm feeling. We're gonna need I feel like we're gonna need a rule set, and we're gonna make that a map of strings to rules. And I feel like that I could use to kinda stuff all the statements. Right? So let's say articles rule set. And the first rule would be for light red. In fact, what I'll do is just copy off this. Alright. So what I'm gonna do is
26:27 I'm gonna try to translate all of this code for now. So let's say, light, red, and the rules for that are one and bright white. And the rules for the second thing is to when needed. So you're building out this rule set so that you can see have an assertion of when I parse a string. This is a rule set I get back. Is that So what I'm doing is so, again, I'm not I'm not worried about actually parsing this input right now. What I wanna do is build my data structure based on this. And so
27:16 we're gonna we're gonna kind of, like, parse it in our heads to start with and make sure that we're traversing our our tree our our graph correctly. And then the second part of that will be, I feel like, parsing this this text. And so I feel like that's a completely separate thing, and I feel like if we try to tackle both at the same time, it'll get confusing and and end of this call. So, actually, let's just put this way. Yeah. There we go. Alright. So, in fact, I'll just do this. Make that. Place it here.
27:57 And let's see if we can kind of I don't know if we could do this programmatically, but we could certainly do this. Alright. I guess we could maybe from this slide, that's line 21 to here, replace bags with colon to get a map syntax. Alright. So, also, let's do something similar and get rid of the word contain. And let's get rid of the words bags over the same range. A bag with potentially an s. There we go. Alright. So now we can kinda It's done. Thank you. Let's see. I feel like I'm trying to see. So,
29:32 I mean, that curly braces are off here. Oh, yeah. They definitely are. Sorry. This is a boring part of So what I'm doing now is, again, we're basically kinda hand hand parsing that input so that we have some some data we can use to ensure that our our algorithm is is is right. K. Oh, man. I imagine Versus Code probably has, like, something that would make this slide easier to do. Yeah. Multi link cursors would made it a little bit easier, but still it's still fetching. Yeah. I I think I think I'd probably do that with the I'm but now we're
30:38 we're bumped into our like, the limits of my VIM skills here. Alright. So both of these, they have no address, so we could just say, like, empty. I guess, these both empty slices. Alright. Let's see if that that works. Nope. Something's wrong there. You've not got quotes around your bright white music yellow, etcetera. Why didn't you tell me earlier? Okay. Yeah. I don't want to tell you at the time either, but your rejects replacing bag or bags was missing an optional param. So you've got a couple of bag in there too. Right. Okay. Alright. We'll get rid of that.
31:13 We'll try to at least see the back to you at this point. If if you're watching the recording, fast forward through this part. I do appreciate the relative line numbers, though. I I I don't think I could quote without them anymore either. Yeah. Yeah. Alright. Here we go. And we'll get rid of that. Although, one of the things I worry about is having the keyword bag within a color. I haven't looked, but, it's been my experience that these, admin code problems, haven't, like, tried to throw you off by putting in, like, data. That is that was
31:49 weird. It usually it left you to sort of deal with the the task task at hand. So alright. So I feel like we we have our rule set encoded. So now is a matter of traversing it. So what I feel like I wanna do is maybe let's do something like our rule set find maybe, and we have a string that we're looking for. And oh, so what we're doing is accounting. Right? So we don't account. So we wanna try okay. Alright. So the impression I get is for every node, we wanna see if our target
32:46 is reachable from a particular node. So I'm gonna iterate over each node then. So for let's say, n is range over r. Let's return zero for that. And so now for each node, what I wanna do is see if I if I could find a node rooted at that node. And I feel like at this point, I need another another function. And this is a part that feels recursive. So given given a node, what I wanna do is walk each edge and see if I could ultimately recursively land at my target. And so I think here is where something
33:41 I find is important. So what all I wanna know is whether or not this target is findable, root rooted from a particular from a particular node. So I guess also what I need is so the first thing I'll do is well, before we do any of this, let's just see what this does. Alright. So this let's just run our our test because I don't wanna get too far before I realize I have some, like, major, like, error or anything like that hiding in there. So let's just do, like, a t dot log f of r
34:49 find. So the node we wanna find is our both the shiny gold. Yeah. Shiny gold. And let's see if we can find it where we do that. Right. Okay. Alright. So for now, we're just gonna say go test run all tests or the reverse way. And, of course, now I have to you know, the the program's completely going up. Alright. So can I use one type int, type string, and field value? Do you not have to import log as well in your your code or just pump to print it? So when I save oh, that that's one
35:33 problem. I I I save. I'm running GoBIM, and that runs Go imports and GoFund and all that stuff. And so Oh, nice. Yeah. Alright. So I think that was oh, no. It wasn't. So rule test by 21. Can I use one untyped in as type string? So I think I just have, like, the the types in my thing over yeah. Yeah. So I have name first and quantity second. So this should be quantity first name second. Yeah. Of course. That's great. Alright. So no major no major problems to to overcome at this point. Alright. So
36:08 how do you say we how do you think we should handle this part? Right? So we have our we have our graph properly encoded. We have our rule set properly encoded. So how do we interpret in a way that we we find find our kind of, like, target note? So I think and I'm just dripping here. What I'd like to do is I think so okay. So find should be a recursive function. I think one of the times that we bail out is if, for example, let's say our rules and whether or it exists is
37:00 basically, let's look to see if our current route is even in our in our map. So you could say if rules exist, route. And so if not exist so that is to say, if we're looking for, for example oh, here. Shiny gold, yeah, rooted at light red. If light red didn't exist, we wanna bail out right away. Yep. I I think. Right? So that exists. Return false. K. Alright. Okay. So now the next thing is to see if to see if our rule exists within the the nodes that we share. So how would we how would we do that?
37:56 So okay. I think a good way to do that what I wanna ask is if rules contains s. Right? So what I wanna see is if my target, you know, s is within the sub slide within the slice that that that we've that we've now looked up. Then we just return true. Right? Well And if it doesn't, then we would we would then search on the the notes that it does have recursively. Well right. So if it if it doesn't, then what we probably wanna do is search on the note. So, you know so yeah.
38:56 Well, if it I think what we wanna do is iterate over this list. If it's in there, then we return true. If it isn't in there, then we run find on each one that that that that that in in in which it doesn't exist. In fact, at this point, don't know, but we may wanna run it on everyone regardless because there may be anyway, let's let's let's let's get there. Let's get there. So alright. So what I'm what I'm asking now is if the rules contain my target, but I don't have Do have do we have
39:38 to implement the contains logic? Because would that work on our structure? Correct. So we what I'm gonna do is actually implement that now. So in the lower right hand quadrant, we're gonna say, you know, funk r rules contains s string, and then we'll try to boop. And what we'll do is we'll iterate over r. So let's say the r oops. Range of r. And so if v equals s, then return true. Otherwise, return false. Okay. So let's see what that looks like so far. So if rules contains that, then we return true. And so for each of these so for
40:47 each item in our rule set, we could say count equals zero. And then they if r find x rooted at n. So if I could find this node rooted at n, open that account. Oops. We'll see everything in there. In fact, what I think the way I'd like to structure this is if I can't find it, then continue. And that's that's just like a kind of personal thing. I like to think of these like, I like to think of this as sort of throwing out state until I would've went down to the state I want here.
41:46 And at that point, I I increment increment count. Right? And then we return count. Okay. So in our test, let's see what that looks like. So now we're gonna count shiny gold. And what that'll do is just print what we're doing here. So we'll log print f. Searching for for value in our our rule set in our in our rules. Oops. Sorry. I'm having notifications pop up. I don't know if you see on the screen, but please please ignore it. So here, what I'll do is I'll print n and r. Alright. It's gonna run my test.
42:56 Oops. I actually kinda use n type rules as type string and argument. Oh, I'm sorry. What I mean is s in n. Yeah. I think the next lane is wrong too. That n there. Yeah. Because I think what you pass into find is the let's take a look at that. Yeah. It's a string, and we're passing in an integer right now, I think. Okay. I'm gonna go outside. Hold on one sec. Yes, sweetie. I'm gonna go outside. Yeah. That's cool. Joys of at home schooling. So where was I? So we wanna find our target rooted at
43:51 n. And so I'm sorry. So r is a rule set, which is this thing. And so n Is n a map? Oh, yeah. So I'm sorry. Excuse me. Excuse Yeah. So our root and our rules poof. Alright. So we iterate iterate over our map. We're gonna extract the root or root node that we wanna start our search and then the subsequent rules associated with that. And in our nope. Nope. I'm sorry. I've kinda lost my place here. Just give me one sec of I'll do your tip. Alright. So we wanna count. So count involves finding.
45:03 So for each rule. We wanna find so we wanna find yeah. No. This is it. So, yeah, we wanna find s. I'll read that and okay. So Okay. Can I ask a question? Because, I mean, I've I've got something in my head that's kinda confusing me, and I'm hoping you can explain it. So Okay. When we do the range on r, right, we're getting two parameters back. One is the key, which is light red, and then the n is the rules on this right, which is a map of tuple. Was that right? Yeah. Yeah. So this is where I think this is
45:50 where I was confusing myself. So what I want is the what I want is the name. I want I want the name of the the node here. So I wanna find s sub n. I think that's what I want. But, also, same rules. Right? So let's let's not do that here. Right. Let's not do that here. K. So let's just see what we get. I'm gonna find it strings. Let's say up. Told string. Roles line 20. What am doing here? So s dot name. Yep. Oops. I'm sorry. V dot name. Alright. So what we've done now is
46:57 so for each of these, let's let's let's print out some, like, output to kinda help us understand what we're doing what we're doing here. So let's say so I'm kind of searching for some target, which is RS rooted at some other strength. So our target is s. The name that we're we're the name of the root node that we're gonna start the traversal at is in. And so what this should what we should see is that we should have output saying searching for shiny gold rooted at light red. Searching for shiny gold rooted at dark orange, hopefully.
47:48 Okay. Alright. So now the actual traversal comes into play. So here's where we actually do the find. And let's see then, like, log for that. Percent q not found at percent q. So that's n. And then otherwise, percent q was found. Great. So, of course, shiny gold was found because I think Viberplum oh, no. Yeah. So Viberplum Oh, wait. Searching for shiny gold. We'll do that. Light red. Shiny gold was found at Light Red. Why was that? Have we continued our recursion? No. Oh, no. No. So I'm just I'm just I'm returning I'm returning true. So Oh. Default.
48:52 Let's do that. Great. Great. So yes. Great. So not found not found not found. But shiny gold was found rooted at muted muted yellow. Right? And so why was that? Because muted yellow actually contains shiny gold. Okay. I feel like we're getting somewhere. Are we good so far? We are. Okay. Alright. Okay. Alright. So what should we do if these rules don't contain contain my value. I think what we should do is iterate over my subsequent rules. So for a rule of is range over rules. In fact, I just say rule. And so now each one of these nodes becomes the root
49:45 at which we wanna begin our search. Right? So if we can't find it so let's say we start with LightRed, and we can't find it within LightRed. Right? So bright white isn't part of its rule set nor is it I mean I'm sorry. So, LightRed doesn't contain it as part of its as part of its rule set. So what I wanna do is see if BrightWrite has it as part of its rule set, which is here. So it means now I wanna call find rooted at BrightWrite, but for the same same target. Are we is that reasonable?
50:19 Yeah. That makes sense. K. So I think what we wanna say is if our find oops. Our target, which is s, rooted at a rule name. If we found it, return true. Otherwise, just keep iterating. And I think That's your part. Okay. So what we have look at that. Alright. So what we have here is we search for tiny gold, we did a bright white. I'm sorry. Shiny gold, we did a bright white. Shiny gold was found at bright white. So presumably, that that returned true. So now we're searching for shiny gold rooted at dotted black.
51:23 Shiny shiny gold was not found rooted at dotted black. So now we're looking for shy shiny gold rooted at fiber plum. It was not found. So then we're searching for well, let's I think we need to, like, improve our output here. So let's kinda log what we're doing. So Let's see. How can we do this? So first thing we do is we check to see if that that node even exists. If it doesn't exist, return false. Then we check to see if our target is contained in that rule set. If it is, then good. We return fall
52:20 true. Otherwise, let's say the map find within and let's just print the map at this point. I mean, the the the the size. So s rules. So I think I think that might be more informative. Okay. So let's scroll back a little bit. Alright. So searching for shiny gold, we're doing dark orange. Cannot find any shiny gold within bright white, muted yellow. So now we'll oh, I see. I feel like we're missing something here. So we could not find shiny gold within this slice. What I think we did next is we searched for shiny gold within either bright white
53:24 or muted yellow, but I don't see those I don't see that being put in. But we can confirm, I think, that muted yellow does contain shiny gold. Oh, I see. We're just not I see. We're not we're not printing anything before we actually do that search. So yeah. Here we go. Searching for queue within RV. So key rules. Yeah. I think that'll give us more data. Alright. So searching for shiny gold, rooted at bright white. So that's at a rule set level. Now for a particular rule, we're saying searching for shiny gold within quantity one shiny gold. Of course, it found
54:28 shiny gold. It's gonna find itself. We're gonna have to fix that. Alright. Now we're also searching for shiny gold. Oh, search for shiny gold. We're doing a bright white. Yeah. I think that's fine. Okay. I I'm feeling a little bit of, like, live coding anxiety, so I'm I'm drawing a little bit of a blank. But, yeah, I think I think that I think what we have probably does the traversal. What was supposed to be the the actual answer? Let's see. So Four, which is right. Okay. Alright. Alright. So cool. So let's I feel good about this, but I don't
55:14 feel like I've really, like, demonstrated that it works. And I guess this is how this is how a lot of this kinda coding kinda kinda coding goes. We got lucky. We didn't run into any major problems. And I think, conceptually, like, we've we've sort of, like, laid out the task that we want wanted to do when we and we've executed it. I think one of the things I could've done better is maybe printed more debug output in the right location. But so let's let's get rid of debug output. So in the meantime, is there any any
55:44 way you think we could sort of like, any test cases that we could use to to kind of expand on this. Oh, well, oh, I think one of the things we probably should be doing is we probably shouldn't find ourself. Right? So if we or we we probably shouldn't start rooted at ourself. So as we're iterating through this, I don't think I think we should exclude this note. Right? So as we're iterating through our nodes, if But but we just return true, don't we? So Also so that was so that was in the portion where a a
56:31 node contains a rule that that contains that that matches. But what I'm talking about is as we iterate over our rules set, each of the roots. Well, actually, don't know. Should should so I guess what would could potentially happen is, let's say shiny gold points to black black olive. And I think in this case, it just works out where the where where this has happened. But, like, shiny gold points to both black dark olive and vibrant plum. And and I don't think either because of the way this works out, neither of those point back to shiny gold.
57:13 But if it did let's see. So shiny gold, dark olive, Where's dark olive? Dark olive points to faded blue. Faded blue points nowhere. Dark olive points to dotted black. Dot of black points nowhere. Alright. So where else can shiny gold go to? You can go to vibrant plum. So vibrant plum goes to faded blue, which goes nowhere as well as the so what I'm saying is if, for example, faded blue instead can contain five shiny gold. So I think the the assumption I made, this is this is directed because you couldn't if a shiny gold can fit a faded
57:55 blue, then a faded blue can't fit a shiny gold. Like, if we're talking about the actual physical dimensions of the bag. Right. Right. So but I don't know. Nothing's preventing the dataset, the actual dataset from contains. That's true. Yeah. And so what I expect will happen is that this might even this should cause an infinite loop, and this might even, like, cause my my machine to crash, and I might have to rejoin the stream. But I'm interested in seeing what what's gonna happen. Right? Oh, no. It actually didn't didn't crash. But we did get double the
58:28 the number. We got an eight back instead of four. Alright. Okay. Yeah. Yeah. Yeah. Alright. So So as you're cursing on itself and then finding shiny gold and then returning true. Right. Right. Right. And so oh, right. And every node that we okay. Yes. Alright. So I think the way to mitigate that would be to say if n not equal or n equals s or I find it, then I think oh, seven. If n equal so n is a name. Well, let's just say if n equals s, continue. Oh, I see. There's other nodes that lead
59:29 to faded blue. So I think this is counting it. But so my point is I think we should exclude the node we're looking for from the roots that that we're looking for. Maybe. I I might be wrong about that. But okay. I think we solved it. I think that is I typically, what I found is that when doing these advent of code problems, pretty much solving the example leads to solving the the rest of the the problems. We open that file again? Sure. The what what do you wanna look at? The same function. Okay. There you go. Let me let me get
1:00:11 Introduction to Day 7: Bag Rules Problem
1:00:14 rid of some of this. So can we just do if s equals root there and then return instead of looking at the contains? Is that what you're trying to do there? I think so. So I think what you're asking is if s equals If s equals root, then it just return true. Do you wanna say true? No? Or would it be false? I don't know. Good question. I think I'm trying to solve a problem that apparently doesn't exist yet. Maybe I'm thinking too far ahead. But I was Well, add add the recursion back to our test. I think that's the
1:00:50 s equal can drill with the fix for that. Right? I'm I'm saying as a okay. In my head, it makes sense. We have to make that shiny gold, then we run our test, and we should still get four. No. And I think that's because well, let's let's let's do this. How about this? Let's print our know what, and let's see what it's called. Oh, no. The find should be returned false. If s equals root, then it should be a false because it means that we are in a nested find, so we would be counting that twice.
1:01:30 No. It's just and so what we're doing is there's another path. There's another path to shiny gold. And so let's do something like this. Say, yep. It's neat. And what we could say is log f percent s searching for some quoted stream related at some quoted string. And we'll set up strings repeat tab. Yep. Okay. And so when we when we go to find, we'll say depth plus one. And maybe, like, here, we'll start depth with zero. Alright. So searching for shiny gold with light red. I search for shiny gold with a red dot bright white.
1:02:35 K? So it must have been terminated, you know, so you have found it or not. Search for shiny gold rooted dark orange, shiny gold rooted at bright white. And so what I expect is that we'll see searching for I don't know you know what? I feel like we've gone down a tangent here that may may not be necessary. Okay. And so what we let's let's revisit this later because the part that's giving me anxiety is parsing the input. And so at this point, I'm gonna ask for, you know, as always, but I'm I'm interested in your help.
1:03:01 Discussing and Implementing Input Parsing
1:03:11 I was wondering, like, what did you think was the best way to parse this this data? So shall we look at the input string again so we can break it down? Absolutely. And so I have a I have a notion, but this is one of those cases where this is in my toolbox. It may be something that is dumb. I mean and what I found is there's times when I've pulled this out of my toolbox, somebody else would go, oh, no. I just use a request for this or whatever, and it worked out just fine.
1:03:41 But so I guess I guess I'll ask you your for your input. And I I'm also gonna have to take, like, a two second break, so I'll be right back. Okay? Yeah. Sure. No worries. I guess I'm gonna have to work out how to process text. So I'll just I'll talk about how I think it it should be done, and then you can all tell me if I'm wrong before Ian gets back. And then I'll look, like, really smart, which should be cool. And to me, it kinda breaks down into two two different parts. There's the color bags contain, which is quite
1:04:32 easy to extract out. I don't see any challenges there. And then on the right hand side, what we have is some numeric identifier followed by name, followed by bag or bags with a comma there. So we can split that right away on a comma. Yeah. So we could we could do this with, like, a recursive strength, but or we can even just regex it out. Although, I was trying to avoid the regex because you know what to say. You know, if you use regex, you've never got an extra problem or whatever that weird thing is.
1:05:00 Yeah. So yeah. Did did you miss all that? Because I I just told everybody watching how to fix it. Oh, really? Well, great. Yeah. So yeah. I was just talking to the problem in my head to anyone that was listening. But when it comes to parsing the stringers, I see some I I don't know if the right word is lexical. But, you know, we can split right away on we can do a very basic string split on bags contained, which is consistent. And that will give us a the name of the bag on the left, and then something on
1:05:37 the right that we can then start to tokenize. Now my next thought after that is that we can then string split on the comma to get all the different bags on the right hand side. And then we could probably get away with just rejects and out the number and the name of the bag from that. So I I think what we're doing is we're saying, okay. Let's loop over each line, split it on bags contain. Left hand side is the name. The right hand side is our loop, and then we loop over the comma splits and then rejects
1:06:03 out the number and the bag color. Okay. Alright. Was that what you were thinking? No. Which is which is good because if I told you what I was thinking so what was wanting to do is tokenize the entire thing. Right? Like So I I parser for it. But I actually like what you said better. So which which route do you wanna take? We could we can we can go either way and We we we could write Alexa and tokenizer. I I I think it's a little bit overkill just because I can see so much consistency here
1:06:36 that all we really need to do is the first split on bags contain is consistent. Right? Really, really easy. We then get the comma to do the split on the number of items on the right. I mean, I'm comfortable with the rejects at that point because it's trivial to get the numeric identifier and then anything up to a space bag. I mean, that's it really is really simple reject. But if you've got a nicer way of handling that, then we can go that way. I know. Let's see. Let's see what we could do here. Oh, let's try that.
1:07:14 Sorry. I'll keep you right. We'll be fine. Alright. So well, let's call the scan that go. Right? Okay. So I'll do it with a pause, I guess, but let's start with a scanner. Maybe it'll take a a room scanner. We'll take a take a reader, IO reader. The first thing you'll do is, let's say, we'll create a line oriented Yeah. Pop. Io new scanner. Right? So let's say, like, standardtools.i0newscanner r. And by default, that that splits it on lines. And so while we scan, you could say, just for fun for right now, log for that.
1:08:19 Scan text. Right? And let's start test. Alright. So yeah. So what we could do is let's paste our test input into here as an example data. And that did not work as planned. There we go. And so we'll say, like, input equals and lose the back ticking go, which gives us sort of an uninterpreted string kinda literal. So no escaping or anything like that. And so now since we're passing scan and IO reader, we could say, like, scan strings, new reader input. Alright. So now we can say go test, run, test, scan, and what we should see is
1:09:40 what we should see is each one. Yep. Okay. So now we wanna split on bags. Alright? So I don't know bags contain. Okay. Alright. So strings index, so that gives us the index of bags. You know? So that gives us the index of a of a substring. So for for each of those, we could say oops. So the first parameter would be scanner text, and the second one would be should we split on space bags contain? Yeah. Alright. So now we could maybe say log for now. Root is something, and rest is something else. Right?
1:10:38 And so our route would be well, let's let's stash our our scanner text because it's gonna get cumbersome calling scan text all the time. So let's just say text equals scan text. Could we just use strings dot split on the bags container? Like, I don't know if there's a reason we do the index or you're just doing that for the debug output? So I guess we could do on the split chain string. Oh, yeah. Strings dot split? Oh, yeah. This is string. It gives us a slice. Well, I was just gonna reslice it. So I was just gonna say, like,
1:11:02 Split String
1:11:13 text, you know, up to index and then test I'm sorry. Up to index and then text via from index and beyond. Will it have to be will it have to be index plus the number of characters and space bags contained? Yeah. Yeah. So we're we're kinda capturing capturing that. So you're right. I think yeah. So split is better. Alright. So we're gonna split on bags contained, and we're gonna say maybe parts. And let's print oh, they'll call it part. Zero and part one. Let's see what we get. Good old go. Yeah. I know. Right? Alright.
1:12:10 There you go. Alright. So the root is, red. The rest is one bright white bag. Oh, I love it. That's awesome. Good deal. Alright. So And then we can do the same on a comma to get all of the items and then the magic of projects. Alright. So rules equals strings split part one on On a comma. Comma. Yeah. Alright. It's supposed to be rules. Okay. Just for now. Alright. So our rules, bright white bag. Oh, wait a sec. Something is not quite right about that because That doesn't look like that was split. Am I doing wrong here?
1:13:41 So rules, red white bags. I'm good at yellow bags. Roles split. So strings up. So see where you've got so we've got the split and base container, which gives us part which gives us part zero and part one. Why is it there I I don't know what the real assignment is. Can that just be removed? Well, so in our hypothetical, like, rules struck the root is gonna be here, and then we're gonna have you know, that's gonna be the root of our map, and then we're gonna have a slice of rules after that. So I guess I don't have to stash
1:14:42 the root just yet. I was just trying to give it kind of a name beforehand. Alright. Okay. Can I see the output again? Oops. And Okay. So it was able to extract the root, which is, like, red. Mhmm. And then the rest, I'm sorry, the rules here. So this looks like a a slice of length instead of like, I don't see a comma here. Maybe I'm just reading the output wrong. Let's let's print it in a way that go That's it. Oh, okay. That's right. I'm sorry. So Yeah. Yeah. So we we did it. Alright. Well, we still have to reject each
1:15:29 of the rules now to get the quantity and the name, but I think that should be pretty trivial. Yeah. And in fact, I think we could avoid using rejects by using scan f. Right? So I think we could do something like let's see. So count zero word one is word one and word two is the string. And we might be able to simply say, font s scan f rules. Oh, I'm sorry. Well, first, for each rule rules, I'll just scan f. So we want an integer. So one string string, and we're gonna pass this the address of
1:15:58 Developing the Part 1 Algorithm (Counting Container Bags)
1:16:28 count. Address of word one and address of word two. And we Not sure if that'll work because the name of the bag oh, I guess it's two strings. Right? It's always two strings. We're not we're not gonna get, like, a pale, like, red in our test data, are we? I don't know. So what I'm hoping is that so what we just did was we gave so we're getting a string in the form of, like I'm just gonna copy this and paste this into the into the so we're doing in the string in, like, this form. Alright? So that's
1:17:02 an integer. So that's covered by a percent d a word, percent covered by percent s, another word covered by percent s. And I'm hoping that we just completely ignore that part in our s k n f verbs. Does that make sense? Yeah. What we also need to handle like, what happens if that scan f fails for the new other bags? Can we handle that? Yes. Exactly. Yeah. So what we could say is, I think it returns the count. So n error equals that. I think. Go to the front scan scan. Yeah. It returns an integer. That's number of
1:17:45 patterns that that it matches and then an error. So I imagine the error it gets when whatever data it's reading go to whatever whatever whatever pattern it gets doesn't match the type of the value that you're writing into. Okay. So what we could just say here is, basically, if n does equal three or error does equal nil, discontinue. But let's also just for our own own satisfaction, indicate that we failed to scan this line. Yeah. And, hopefully, the only things that pop up are the, like, no no. Which one? Alright. Think that's it. Looks good.
1:18:53 I feel like my friends are now. So I don't where where I go wrong. Alright. So we have let me see what the range for the rules. Alright. So let's see what that does. Nice. I think all the fails are in the other bags. Yep. Two of them. I hate to admit it. I'm like I said, I'm suffering from a little bit of live coding pressure. I don't even see the failures right now. There's just the the bottom six lines are the two failures because the last two bags are Yeah. Great. Perfect. And so barring that failure,
1:19:37 let's print out our account. Oh, let's say our node name is word one plus space word two. And then let's reconstruct our node here. So found rule. B is an s. My dog's under my desk right now trying to convince me to throw a ball with her right now. It's my heart. And so count mode. Okay. Great. Cool. So we prep for found. Awesome. So we found rule one, bright white. Rule two, muted yellow. And so if we look at our input, light red bags contain white, muted yellow. I'm gonna go with this for now. Are
1:21:00 you okay with it? Uh-huh. Okay. Alright. So let's construct our graph using this now. So each one of those are rules. So we start with let's start with a rule set, and that's gonna be initialized to rule set or a new rule set. And for each rule we read, we have a rule name. That would be part one. Let's see what else. And so for each role name in our rule set, we could say we wanna append a rule. So, actually, let's start the new rules. So rules equals an empty rules size. And so for each rule, we wanna say
1:22:14 rules equals append rules, and the value we wanna pin to that is a rule containing do you remember our our rule value? I'm gonna split this. I'm sorry. We're kinda getting into It was quantity and name, if that's what you're looking for. Yeah. Thank you. Quantity would be count. I said it's called account, but I should probably call it quantity just to keep everything keep all my terminology consistent. That's the p is quantity. And Name is. Sweet. And then once all of this is done, we could say role set. Your name equals rules. And at the very end, let's
1:23:38 let's return rule set. Yeah. Then we're start against that one we built up earlier. Right? Yes. Exactly. And so I'm feeling All coming together. It's all it's all coming. So and, again, I apologize for, like, the terminology. And as you could as you're as you're aware, all this is kind of evolving. And in an ideal world, maybe some of the variable names and other things that we use might be better than this or more better or better thought out, but it is what it is for now. Alright. Oops. Okay. So I feel like I went wrong. So
1:24:23 It doesn't like that redefinition of rules on test on 13 because it's defined 21 above. Sorry. Down a bit. Underneath the second blue comment, you've got a redefinition of rules. Okay. Thank you. So how should I mitigate that? I think the first one should be, like, rule list or rule string or something like that because it's they're not really rules yet. Gotcha. What's the rule list? And then the range and two two down is rule list. K. It's still very unhappy. I need to play that. Oh, role list. We want that to be rules list. So
1:25:36 rule list rules list. Undefined quantity, p u a n t I t y, p u a n alright. Let me do it. Learn how to spell. And count. Okay. Great. So now we're logging. So now now, theoretically, our result of scan should be a proper, like, rule set. And so we should be able to say, like, t dot log. I thought I just put a ball right, like, on my lap. No. And so we should be able to print this, and this is what we get. Alright. So map, one bright bag. Okay. So this is actually a problem.
1:26:48 Oh, okay. So the problem is the index of my rule. That should be well, I was calling my root. So that's part zero. What I'm putting in there now, rule name. Next rule. It's part one. So, yeah, what I want here is part, oops, part zero, which I think is out of scope. I know. I said real name is is defined and unused. Oh, okay. I just did it on that. Sorry. Oh, no. I don't think that was a problem. Oh, yeah. Okay. Okay. Go for that. That's fine. Alright. Okay. Great. Alright. So we have our map. This is bright
1:27:50 white, and it points to So should we should we copy the map definition from our other test into that one? It just assert against it. Totally. That's a great idea. So where is that? It's the rules test. So I should be able to you wanna call that out, and we'll paste it here. I'm just gonna get rid of this other clutter. Okay. So and so maybe we could stash this in, like, parsed rule set. And now let's just visually compare it because what I'm afraid of is so what I'm thinking is we could use, like, reflect deep
1:28:48 equal to see if they're actually the same. I don't know yet. But just to get a sense of satisfaction, what I'd like to is just print into the screen and see if we could visually compare that. Are you okay with that? Or you think I Yeah. Can you do a search first for d p o t? One more time? Do a search for d p o t. It's just a typo that you were you must have then command. Yeah. So that should be dotted. You were trying to paste in there. Got it. Thank you. Alright.
1:29:20 And so just t dot log f percent v r and then t dot log f cost rule set. Alright. And so at a glance, they look identical. Quantity six. I'm willing that that was a typo somewhere, maybe, but we'll see. Alright. So we have a map, bright white, quantity one, shiny gold, dark olive, quantity three, faded blue, 24. I think the ordering is just different. Alright. Well, so No. Maybe not. Where did they deviate? What's that again? I'm just trying to see where the the deviate after faded blue into light red. Interesting. The end of the the end of the
1:30:28 second line after faded blue. So the faded blue on the parsed lines doesn't have any rules, which I actually think is correct. So did we modify the test case? Oh, yeah. Yeah. Yeah. We did. We did. Yeah. Yeah. Sabotaging ourselves. Yeah. Oops. Alright. So that looks right. In fact, they ended at the right place. Yeah. In fact, I think we could do something like let's see. Photoshop reflects equal. Alright. So give two things. It turns a bull. Alright. So let's check to see if reflect deep equal r parsed rule set. If that equals false, then
1:31:25 t dot fatal Ours data that doesn't match expected. Whatever. This isn't yeah. This isn't productive code. Thank you. Alright. So where are we? So let's run this test and see if it fails or passes. Hey. It passed. Cool. Alright. So how far are are we into this? So an hour and twenty four minutes in. Actually, maybe a little bit more than that. If you just add kinda, like, random stuff. We've successfully parsed the data. We think what we have successfully traverses the the the graph. So now what I'm ready to do is just try this on the test data that's
1:31:48 Running and Submitting Part 1
1:32:10 included. Alright. Are you good with that, or do you see anything else that we need to do? No. I think we smashed it. Okay. I think so. But whatever whatever definition to smash, one of these. Right? Alright. So let's just copy all the data out. And so something nice we could do is we could say, tap this input dot text and paste it. Alright. So now we have this nice input dot text. And so within GoTest, we can actually load local or load test files, and and and the test is run with the current working directory,
1:32:45 Load Test Files
1:32:54 the same as, like, your package. And so I could refer to any, like, test files just using, you know, sort of, like, the current current path. So I could do something like so let's go back to our rule test profile. I mean, we could we just we could just create, like, a main dot goal, scan that file with an IO reader, and then call finder. Right? And then open a value. Yeah. Or we could do it within a test. Right? So, like, we don't have a main package here. So we could simply say, like, funk test.
1:33:35 I don't I can't think of a good name for this now. I'm feeling a little bit embarrassed by not naming this well, but this is kind of like our final test. Right? So let's just call this a o c seven part one t testing dot t. And so what we could do is we could say, like, in the file, error equals OS open, and what do we name it? We put that text. I guess we'll find out soon enough. If error does equal nil, then we encountered some problems. So t dot fail or t dot fail,
1:34:10 and then out of the error. Otherwise, let's defer posing our our input. Oops. And then now we could say rule set our rules equals oh, let's call it rule set equals scan I don't remember if we chose to return an error. So I guess, typically, what I'd like to do is return an error of some kind, but where, yeah. And so we'll set count, and let's look at our test here. So we wanna count shiny gold. Right? So and let's log the results of that. Alright. Here we go. So that's it. So we we kind of modeled our data type,
1:35:20 implemented traversing our our struct our our our data structure. We wrote a parser for our input and, in fact, generated the the our graph. And so now we're gonna this is where the kind of rubber meets the road, and we're gonna run a test for a o c one part one to build a test when that verbose. Yeah. There's an error. Too many arguments called fatal. Oh, I'm sorry. It's Oh, fatal. Alright. Yeah. Alright. So it's just getting producing a lot of nice output, and it counted a 12. Is that the right answer? I don't know.
1:36:16 I'm I'm super worried about this because I've only I've only entered the wrong answer once. And I was and and so I felt like I was kinda, like, on a roll. And so I don't wanna do it twice. But if you're good with it, I'm willing to, like, type this into the into the interface and see if this works. What do you think? I alright. Here we go. So let's pull this tab over here. What what do we say again? A 12? Yeah. Oh, jeez. Okay. What do we do if this doesn't work? Like, do we still go back to drawing
1:36:57 board or do we just say, you know what? You gave me a good try. Yeah. I've gone to the pub. Alright. Here we go. Ah, yes. That was it. We did it. Good job. Nice. So I'm afraid to go on part Oh, man. My dog's, like, really harassing me. This is a good time to, like, let her out and and take a break. Is is that okay, or do you wanna do you wanna say part two for later? Yeah. Let's say part two for later. I'm I've gotta have dinner with my wife and stuff, but
1:37:25 I'll we can we can sort that out for sure. Okay. Alright. Did you do you wanna pick up later on this evening, or did you wanna just do it another time? Or do you wanna just skip it and just say, like, hey, this is how this is how I wanna answer part one of this admin of code thing. See, you never know what part two is. Sometimes it's, like, something you're gonna do in fifteen, twenty minutes, and then sometimes it's something that's, like, crazy bigger. Let's take a quick look. Let's take a look. Yeah. Okay. Alright.
1:37:42 Introduction to Part 2 (Counting Contained Bags)
1:37:52 So it's getting pretty expensive to fly these days, not because of the ticket prices, but okay. Let me let my dog out because she's what's gonna happen is she's eventually gonna unplug my computer. It looks fun. So consider getting your shiny gold bag and the rules from the above example. Contains zero other bags. Contains so a single shiny gold bag must contain one dark aloe of that. It's plus two the 11 bags within each of those. So this is exactly what you predicted. Right? Because now what we wanna see is, I think oh, so, of course, the rules have a
1:38:58 small chance of giving several levels deeper than this example. Be sure to count all of the bags even if the nesting becomes topologically impractical. So here's another example. Shiny gold bags contains two dark bags. Dark red bags contains two dark orange bags. So this example, a single shiny gold bag must contain a 26 other bags. Oh, so true. So it's okay. Not because of ticket prices, but because of the ridiculous number of bags you need to fly. Consider again your shiny gold bag and the rules from the above example. K? So a single shiny gold bag must contain
1:39:51 one dark olive bag and the seven bags within it plus two. And, of course, these actual roles have smaller chance at going through. Yeah. So we have to recursively loop over counting all the quantities to get, like, a cumulative sum of how many bags spent in all the other bags. I think that's easy. Okay. Let's let's try this. Let's see if we can knock this out in a few seconds. So let's look at where will that go. So I think all all we have to do is as we recurse, we can accumulate a a sum. So let's say sum.
1:40:17 Attempting to Solve Part 2 (Recursive Sum Logic)
1:40:35 Let's say sum is a pointer. Alright. So the base case of this is if we find a bag that this could fit in. Oof. Hold on. I need to look at this again. So for each node, like so what I'm imagining is that as we path it's a path from our root to the actual node that we're looking for. I think what we wanna do is accumulate a sum that is for each sub bag that contain our bag that we're searching for. So this oh, let's see. So so for a little shiny old bag must contain one
1:41:49 dark olive bag and the seven bags within it plus two vibrant plum bags. And so if there were and the 11 bags within each of those. Yeah. So instead of returning a true like we do now in the find, what we're actually gonna have to do is then call account on each of those trues to return the number of bags. So every time we come across a bag, we're gonna need to, a stack where we add it to it. And then we're gonna have to look over the stack during the count. K. So maybe we can return
1:42:42 minutes. I don't think we have to return a bill. We I mean, we could just return a zero. K. I'm just afraid of, like, changing everything else we've done. I'm okay with that. Oh, so the I guess that's the thing. So that's gonna change the signature of everything. So I think it's a find what we wanna do is just make another function call count. And but we could do the same thing. So we could say, like so for everything from line 10 to here, can get that. And make a new function called count, and it's gonna turn to mute.
1:43:33 Sorry. I will call it sum. Alright. So what happens is I think the base case is if we actually find well, it is if we actually or one of the base cases if we actually find our target. But, otherwise, we could return zero where we don't find it. Here's another case where we would probably return zero. If if we if the rules contain this, maybe we could have, like instead of rules contains, we could say rules I guess we could just negate that and return zero? What we wanna do is find the count of And so if it doesn't contain it,
1:44:31 we return the cumulative sum. And if it does, then we wanna look over all of those bags and then increment the account with those. Right? Yes. So here, if it doesn't contain it oh, I see what you're saying. Mhmm. Yeah. So lane where you've got f rules dot contains, if we just negate that and and so we know that it doesn't contain it, then we can just return whatever the account has at that point in time. Is that maybe that's too too naive. No. No. No. I'm sad that you've naive. So rules is a slice.
1:45:07 And so what we're doing in contains is we're iterating over that slice and seeing if we find a rule that matches that name. But instead, I think what we wanna do oh, I see what you're saying. Oh, yeah. Yeah. Yeah. Yeah. So if we don't find that, then return zero. Thank you. That's really good. Okay. So, otherwise, what we wanna say is equals roles out. And discount Oh, okay. So here are a couple so there's a chance that and I think that's what, this warning is about. Right? So, where is it? Alright. So, of course, the actual rules have a
1:46:25 small chance of going several levels deeper than deeper than this example. Be sure to count all of the bags even if the nesting becomes topologically impractical. So I think what can happen is we might have loops, but maybe not. But also, a bag might contain or a bag might contain another bag that might contain I guess I'm getting at is what I'm feeling at this point is that I should continue through this loop. But at the same time, I need to be able to note when we found, how can I say this? My field is we need to note when
1:47:10 we found an entire chain or an entire, yeah, an entire path somehow. And I'm not sure. I kinda painted myself in a corner here because we might find multiple multiple paths, but how do I how do I account for that? Unless actually, I guess I guess it's alright. Yeah. So I I don't think that's a problem. So our total count for this loop is such. Our count equals our count. I can even say maybe total plus equals that and return the total. What do you think? So now let's edit our roles test again, and let's do part two.
1:48:10 So eight here. Yank. Two. Scan. Okay. Awesome. Awesome. Okay. Hold on a sec. My wife's ringing. Okay. Alright. So I think that works. I think. Although this is complaining. Spectator. Open. Yeah. Some takes the string that are written at that. Oh, yeah. K. You're not passing in the rule set. So it would be yeah. Something's missing there. Because you're and rule set does some of your passing shiny gold, but you're not passing in the root strength. Oh, yeah. Yeah. Yeah. Oh, I see. Yeah. Alright. So we'll set some. This account. Alright. So our name we're just kinda stretching
1:49:52 our naming here. But so I guess what I could say is some here changes to lowercase some so that we're not gonna expose. And then so for each of these, we'll say sum Yeah. Right. Plus equals sign of that, and we'll return the total. And then so this Right. Right. Right. So we're good? Yep. Still complaining. Rule 87. Oh, it's still inside. I aborted that code. Okay. So too many arguments. Alright. To count so I changed oops. This Yeah. That should be sum. That should be count, and this one should be sum. Oh, I am so sorry. I'm feeling like
1:51:08 I help me out here. Yes. So and your sum function, there's a call to count, which should be sum with a capital s. Yeah. There. That should be calling some, sorry, with a small s. Yeah. Okay. Thank you. Alright. Alright. So let's test AOC seven part two. Alright. So we got zero. Oops. Yeah. Let's let's make sure that part one still works. Alright. Alright. So what do I do in Rawkode? So when we sum, we wanna call lowercase sum. Alright. So maybe so I'm in reading for this. So there there's a problem. We're we're always
1:52:39 looking looking or searching using the same route. Oh, no. No. We're not. We're searching for shiny gold rooted at each one these. Alright. That's good. And so we're calling lowercase sum with our target and our root. And what we do is we say if s equals root, return zero. If we could find the root or if we can't find the root, return. If it doesn't contain that value, then we return. But if it does contain it Actually, I think this is this logic is backwards. If it does contain it, we'll just return yeah. So here, I think, is where we
1:53:39 wanna return the value of the node. Oh, yeah. We're not doing anything with quantity yet. Right. Right. So that that's where we wanna return the value of the quantity. Yeah. So let's just say, like, q two I equals those container s minus rooms quantity. And so here in contains, we'll change this to there we go. I'm just gonna just say, t t y. If if the name equals, we'll return v dot quantity. Otherwise, we'll return zero. I'm assuming that if quantity is equal or if quantity equals zero Quantity will always be a positive integer. Well, yeah, it'll always be a positive integer.
1:55:01 But if it's zero, that means we didn't find it. Oh, yeah. Yeah. Yeah. Okay. Gotcha. And and I think but if we did find it, then we wanna return that quantity right then and there. So quantity is equal zero, then return quantity. Alright. Now for each of our rules, based on the rule, fortunately, we we we held on a depth because that could be our our factor that we multiply. Oh, maybe not. Maybe not. So total rule quantity times the sum of any rules below it. Okay. Alright. There we go. It's recursing. It's a lot of stuff. We
1:56:03 have a number. Oh, how do we do it? Fifty fifty. Yeah. So I actually feel pretty good about this. I don't I don't know the answer, but I I'm feeling good about it. I I'm gonna enter it, and I say if it if it works, it works. If not, then that's really high, though. That's a really high number. It's a big data center. It's Let's try it. Okay. Alright. Here we go. The worst that happens is it works. We celebrate. If it doesn't, when oh, well, we we'll try again another time. We're coming up to, like, two hours and
1:56:43 ten minutes now. Think this has been a Yeah. Yeah. It's way too high. Alrighty, man. Well, that was it. That was my terrible attempt. I told this. I'm sorry. I wish I I wish No. It was good. That was really good. Thanks for your help. I feel like I let you down. No. I think that was really good fun. I think, you know I mean, considering what we tried to sort of together there for that quantity thing at the end, like, I think I'm afraid. I pray solid effort. What I would suggest is that if you could push that code to
1:56:48 Part 2 Result and Wrap Up
1:57:14 get help, that would be really cool. And, you know, if you, you know, if you're available this week, we should definitely pick up another one of these and have. Because I I really enjoyed just seeing how you think and how you kinda handle this. To me, it was really cool. I really enjoyed that one. That's good. Oh, cool. Cool. Alright. Well, thanks thanks for the opportunity. I really appreciate it. I guess, yeah, in closing, it was awesome working with you at at influx. I already missed my team. I look forward to the to the next
1:57:41 gig. And I hope that the people at my next my next employer hadn't seen this and they couldn't judge me judge me by this, and I feel like reconsidering at this point. So but but yeah. That's it. Thanks for the opportunity. Had a really good time. No. It was my pleasure, honestly. Thank you for that. I'll speak to you afterwards. We'll try and get something else sorted out, and it was good. So thank you very much. You have a great day, man. Alright? Cool, man. Bye bye.
Meet the Cast
Stay ahead in cloud native
Tutorials, deep dives, and curated events. No fluff.
Comments