Advent of Code Day 19 was one of those light, easy challenges. Just hard enough to make you think a little, easy enough to solve in 20 minutes.
This could make a great interview question 🤔
The gist of the problem is this:
- you are given an ASCII drawing of a path
- you have to follow it to the end
- collect letters as you pass them
- print them in sequence
For a map like this 👇
|| +--+A | CF---|----E|--+| | | D+B-+ +--+
Your result is
The real map is… bigger. 201x201 characters. Or at least mine was.
Far too large to trace with your finger. I mean, you could if you had the patience of a gnat, but that's why the 2nd part of today's puzzle asks you to count the number of steps.
Ain't nobody got the patience to count the number of steps by hand.
Luckily coding up a solution in Python is pretty simple.
You take a current position,
pos, that starts at the only
| in top row – your entry point. And a vector
(vy, vx) pointing downwards,
(1, 0), and you travel through the map.
If you encounter a corner,
input[y][x] == '+', you change direction – the movement vector. If so far you were traveling vertically, then you go either left or right depending on which direction has inputs. If you were traveling horizontally, then you switch to vertical.
if input[y][x] == "+":if vy != 0:# go left or rightif input[y][x-1] == "-" or input[y][x-1] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":vy = 0vx = -1else:vy = 0vx = 1else:# go up or downif input[y-1][x] == "|" or input[y-1][x] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":vy = -1vx = 0else:vy = 1vx = 0
vy being different than
0 tells you that you've been moving vertically. If there's something to the left, you go there; otherwise, you go the opposite direction.
Same approach for the horizontal change.
When you encounter a letter, you add it to the list of letters.
elif input[y][x] not in ["-", "|"]:letters.append(input[y][x])
In the end, you make sure to update your current position and steps counter.
steps += 1pos = (y + vy, x + vx)
That applies the movement vector to our position
pos and increments the
steps counter. Easy 👌
When we put it all together and add a loop, the path following implementation looks like this 👇
def followpath(input):linelen = max(len(line) for line in input.split("\n"))input = [line.ljust(linelen, " ") for line in input.split("\n") if len(line) > 0]pos = (0, input.index("|"))vx = 0vy = 1letters = steps = 0print linelen, len(input)while True:y, x = posif y < 0 or y >= len(input) or x < 0 or x >= linelen or input[y][x] == " ":return steps, lettersif input[y][x] == "+":if vy != 0:# go left or rightif input[y][x-1] == "-" or input[y][x-1] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":vy = 0vx = -1else:vy = 0vx = 1else:# go up or downif input[y-1][x] == "|" or input[y-1][x] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":vy = -1vx = 0else:vy = 1vx = 0elif input[y][x] not in ["-", "|"]:letters.append(input[y][x])steps += 1pos = (y + vy, x + vx)
We prep the input, start an infinite loop, break out of the loop when we fall outside the
input field or hit an empty spot (meaning line is over), and use our path-following logic to move around.
Eventually, we reach the end of the line and return our result: The number of steps and the sequence of letters encountered.
You can see everything on Github.
That was fun 🤙
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companiesLearn more
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️