Advent of Code Day Two
Paired with: Ngina
Code we ended up with after 50 minutes
Experience: Reminded that my string manipulation abilities in TS have room for improvement. Regex could have been helpful.
Typescript surprised us with something, which is that the return type of a function wasn’t enforced, which is exactly what I thought it should do.
Turns out I had an initial logic issue, was that for each game, I incremented the number of cubes of that color I drew as I went. But of course, there could be three total blue squares and I could draw 3 squares each time—that doesn’t meant there are 9 blue tiles total! So I changed this to only update the max draw to be the largest number drawn like this:
Not DRY at all, but it worked!
My answer of 149 increased to 2278, which ended up being the correct one for me.
I asked perplexity to refactor my code for me and got this:
‘Plex broke things down into a bunch of smaller, functional pieces. I particularly like this:
function parseGames(data: string): Game[] {
return data.split('\n').map(parseGame);
}
Using map, we have a function that returns a list of lines of games right off of the data.split function, and then uses parseGame to process each of those lines. Very fancy, but nice.
parseGame looks like it splits each game into a first and second half. I like saving this value, and the way it is de-structured. It’s just very clear to see okay, we’re not done cleaning the data, but at least we have these two sections. Good clear string manipulation.
I think at around line 3 though, things are getting a bit complicated to have these successively nested functions. I think I would maybe skip this.
My takeaway from this exercise it’s nice to have less nesting, and to be saving values at each step of the string manipulation in an increasingly better form until everything is structured exactly how you’d want it.
Okay with those takeaways, let’s move on to part II.